[玩转系统] GetProc05 (VB.NET) 示例代码
作者:精品下载站 日期:2024-12-14 02:28:27 浏览:12 分类:玩电脑
GetProc05 (VB.NET) 示例代码
以下是 GetProc05 示例 cmdlet 的完整 VB.NET 代码。
Imports System
Imports System.Collections.Generic
Imports Win32Exception = System.ComponentModel.Win32Exception
Imports System.Diagnostics
Imports System.Security.Permissions
'Windows PowerShell namespace
Imports System.Management.Automation
Imports System.ComponentModel
Namespace Microsoft.Samples.PowerShell.Commands
' This sample is a complete implementation of the get-proc Cmdlet.
#Region "GetProcCommand"
''' <summary>
''' This class implements the get-proc cmdlet
''' </summary>
<Cmdlet(VerbsCommon.Get, "Proc", _
DefaultParameterSetName:="ProcessName")> _
Public Class GetProcCommand
Inherits PSCmdlet
#Region "Parameters"
''' <summary>
''' The list of process names on which this cmdlet will work
''' </summary>
<Parameter(Position:=0, ParameterSetName:="ProcessName", _
ValueFromPipeline:=True, _
ValueFromPipelineByPropertyName:=True), _
ValidateNotNullOrEmpty()> _
Public Property Name() As String()
Get
Return processNames
End Get
Set(ByVal value As String())
processNames = value
End Set
End Property
''' <summary>
''' gets/sets an array of process IDs
''' </summary>
<Parameter(ParameterSetName:="Id", _
Mandatory:=True, ValueFromPipeline:=True, _
ValueFromPipelineByPropertyName:=True, _
HelpMessage:="The unique id of the process to get.")> _
Public Property Id() As Integer()
Get
Return processIds
End Get
Set(ByVal value As Integer())
processIds = value
End Set
End Property
''' <summary>
''' If the input is a stream of [collections of] Process
''' objects, we bypass the ProcessName and Id parameters and
''' read the Process objects directly. This allows us to deal
''' with processes which have wildcard characters in their name.
''' <value>Process objects</value>
''' </summary>
<Parameter(ParameterSetName:="InputObject", _
Mandatory:=True, ValueFromPipeline:=True)> _
Public Property Input() As Process()
Get
Return inputObjects
End Get
Set(ByVal value As Process())
inputObjects = value
End Set
End Property
#End Region
#Region "Cmdlet Overrides"
''' <summary>
''' For each of the requested processnames, retrieve and write
''' the associated processes.
''' </summary>
Protected Overrides Sub ProcessRecord()
Dim matchingProcesses As List(Of Process)
WriteDebug("Obtaining list of matching process objects.")
Select Case ParameterSetName
Case "Id"
matchingProcesses = GetMatchingProcessesById()
Case "ProcessName"
matchingProcesses = GetMatchingProcessesByName()
Case "InputObject"
matchingProcesses = GetProcessesByInput()
Case Else
ThrowTerminatingError(New ErrorRecord( _
New ArgumentException("Bad ParameterSetName"), _
"UnableToAccessProcessList", _
ErrorCategory.InvalidOperation, Nothing))
Return
End Select
WriteDebug("Outputting matching process objects.")
matchingProcesses.Sort(AddressOf ProcessComparison)
Dim process As Process
For Each process In matchingProcesses
WriteObject(process)
Next process
End Sub 'ProcessRecord
#End Region
#Region "protected Methods and Data"
''' <summary>
''' Retrieves the list of all processes matching the ProcessName
''' parameter.
''' Generates a non-terminating error for each specified
''' process name which is not found even though it contains
''' no wildcards.
''' </summary>
''' <returns></returns>
Private Function GetMatchingProcessesByName() As List(Of Process)
Dim allProcesses As List(Of Process) = _
New List(Of Process)(Process.GetProcesses())
' The keys dictionary will be used for rapid lookup of
' processes already in the matchingProcesses list.
Dim keys As Dictionary(Of Integer, Byte) = _
New Dictionary(Of Integer, Byte)()
Dim matchingProcesses As List(Of Process) = New List(Of Process)()
If Nothing Is processNames Then
matchingProcesses.AddRange(allProcesses)
Else
Dim pattern As String
For Each pattern In processNames
WriteVerbose(("Finding matches for process name """ & _
pattern & """."))
' WildCard search on the available processes
Dim wildcard As New WildcardPattern(pattern, _
WildcardOptions.IgnoreCase)
Dim found As Boolean = False
Dim process As Process
For Each process In allProcesses
If Not keys.ContainsKey(process.Id) Then
Dim processName As String = _
SafeGetProcessName(process)
' Remove the process from the allProcesses list
' so that it's not tested again.
If processName.Length = 0 Then
allProcesses.Remove(process)
End If
' Perform a wildcard search on this particular
' process and check whether this matches the
' pattern specified.
If Not wildcard.IsMatch(processName) Then
GoTo ContinueForEach2
End If
WriteDebug(String.Format( _
"Found matching process id ""{0}"".", process.Id))
' We have found a match.
found = True
' Store the process ID so that we don't add the
' same one twice.
keys.Add(process.Id, 0)
' Add the process to the processes list.
matchingProcesses.Add(process)
End If
ContinueForEach2:
Next process ' foreach (Process...
If Not found AndAlso Not _
WildcardPattern.ContainsWildcardCharacters(pattern) _
Then
WriteError(New ErrorRecord( _
New ArgumentException("Cannot find process name " & _
" " & pattern & "."), "ProcessNameNotFound", _
ErrorCategory.ObjectNotFound, pattern))
End If
Next pattern
End If
Return matchingProcesses
End Function 'GetMatchingProcessesByName
''' <summary>
''' Returns the name of a process. If an error occurs, a blank
''' string will be returned.
''' </summary>
''' <param name="process">The process whose name will be
''' returned.</param>
''' <returns>The name of the process.</returns>
Protected Shared Function SafeGetProcessName(ByVal process As Process) _
As String
Dim name As String = ""
If Not (process Is Nothing) Then
Try
Return process.ProcessName
Catch e1 As Win32Exception
Catch e2 As InvalidOperationException
End Try
End If
Return name
End Function 'SafeGetProcessName
#End Region
#Region "Private Methods"
''' <summary>
''' Function to sort by ProcessName first, then by Id
''' </summary>
''' <param name="x">first Process object</param>
''' <param name="y">second Process object</param>
''' <returns>
''' returns less than zero if x less than y,
''' greater than 0 if x greater than y, 0 if x == y
''' </returns>
Private Shared Function ProcessComparison(ByVal x As Process, _
ByVal y As Process) As Integer
Dim diff As Integer = String.Compare(SafeGetProcessName(x), _
SafeGetProcessName(y), StringComparison.CurrentCultureIgnoreCase)
If 0 <> diff Then
Return diff
End If
Return x.Id - y.Id
End Function 'ProcessComparison
''' <summary>
''' Retrieves the list of all processes matching the Id
''' parameter.
''' Generates a non-terminating error for each specified
''' process ID which is not found.
''' </summary>
''' <returns>An array of processes that match the given id.
''' </returns>
Protected Function GetMatchingProcessesById() As List(Of Process)
Dim matchingProcesses As List(Of Process) = New List(Of Process)
If Not (processIds Is Nothing) Then
' The keys dictionary will be used for rapid lookup of
' processes already in the matchingProcesses list.
Dim keys As Dictionary(Of Integer, Byte) = _
New Dictionary(Of Integer, Byte)()
Dim processId As Integer
For Each processId In processIds
WriteVerbose("Finding match for process id " & _
processId & ".")
If Not keys.ContainsKey(processId) Then
Dim process As Process
Try
process = _
System.Diagnostics.Process.GetProcessById( _
processId)
Catch ex As ArgumentException
WriteError(New ErrorRecord(ex, _
"ProcessIdNotFound", _
ErrorCategory.ObjectNotFound, processId))
GoTo ContinueForEach1
End Try
WriteDebug("Found matching process.")
matchingProcesses.Add(process)
keys.Add(processId, 0)
End If
ContinueForEach1:
Next processId
End If
Return matchingProcesses
End Function 'GetMatchingProcessesById
''' <summary>
''' Retrieves the list of all processes matching the Input
''' parameter.
''' </summary>
Private Function GetProcessesByInput() As List(Of Process)
Dim matchingProcesses As List(Of Process) = New List(Of Process)()
If Not (Nothing Is Input) Then
' The keys dictionary will be used for rapid lookup of
' processes already in the matchingProcesses list.
Dim keys As Dictionary(Of Integer, Byte) = _
New Dictionary(Of Integer, Byte)()
Dim process As Process
For Each process In Input
WriteVerbose("Refreshing process object.")
If Not keys.ContainsKey(process.Id) Then
Try
process.Refresh()
Catch e1 As Win32Exception
Catch e2 As InvalidOperationException
End Try
matchingProcesses.Add(process)
End If
Next process
End If
Return matchingProcesses
End Function 'GetProcessesByInput
#End Region
#Region "Private Data"
Private processNames() As String
Private processIds() As Integer
Private inputObjects() As Process
#End Region
End Class 'GetProcCommand
#End Region
#Region "PowerShell snap-in" '
''' <summary>
''' Create this sample as a PowerShell snap-in
''' </summary>
<RunInstaller(True)> _
Public Class GetProcPSSnapIn05
Inherits PSSnapIn
''' <summary>
''' Create an instance of the GetProcPSSnapIn05
''' </summary>
Public Sub New()
End Sub 'New
''' <summary>
''' Get a name for this PowerShell snap-in. This name will
''' be used in registering
''' this PowerShell snap-in.
''' </summary>
Public Overrides ReadOnly Property Name() As String
Get
Return "GetProcPSSnapIn05"
End Get
End Property
''' <summary>
''' Vendor information for this PowerShell snap-in.
''' </summary>
Public Overrides ReadOnly Property Vendor() As String
Get
Return "Microsoft"
End Get
End Property
''' <summary>
''' Gets resource information for vendor. This is a string of format:
''' resourceBaseName,resourceName.
''' </summary>
Public Overrides ReadOnly Property VendorResource() As String
Get
Return "GetProcPSSnapIn05,Microsoft"
End Get
End Property
''' <summary>
''' Description of this PowerShell snap-in.
''' </summary>
Public Overrides ReadOnly Property Description() As String
Get
Return "This is a PowerShell snap-in that includes " & _
"the get-proc sample."
End Get
End Property
End Class 'GetProcPSSnapIn05
#End Region
End Namespace
参见
Windows PowerShell SDK
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag