【问题标题】:PowerShell looking for duplicated processesPowerShell 寻找重复的进程
【发布时间】:2014-04-11 16:40:03
【问题描述】:

此代码确实有效.. 但是,我无法确定它是 Notepad.exe 还是 TextPad.exe.. $a.Name 变量不起作用..

$log = "D:\WORK\ps\duplicate.txt"
If (test-path -path $log) {
Remove-Item $log
}

##$query = "Select * from win32_Process where name like '%script%'"
$query = "Select * from win32_Process where name like '%pad%'"

#$a = Get-WmiObject -Query $query | select name, processID | sort name

$SERVERS = get-content -Path D:\WORK\ps\monitored_computers.txt
$(Foreach ($server in $SERVERS) {
    $a = Get-WmiObject -Query $query -ComputerName $server | select name, processID | sort name
    IF ($a.Count -gt 1 ) { $server + " count: " + $a.Count + " name: " + $a.name | Out-File -append -filepath $log -encoding ASCII }
})

If (test-path -path $log) {
    send-mailmessage -from "sender@company.com" -to "receiver@company.com" -subject "Duplicated processes" -body "Please see attached"  -Attachment $log -dno onSuccess, onFailure -smtpServer smpthost.company.com
}

【问题讨论】:

  • 您能否详细说明您要完成的工作?
  • 我正在设置一个监视器(计划任务),让我们知道我们是否有任何挂起或卡住的 Wscript/Cscript 进程..我已经在 VBScript 下完成了..

标签: powershell process wmi


【解决方案1】:

$a 是一个数组,当Get-WmiObject 返回多个结果时。虽然数组 elements 具有 Name 属性,但数组本身没有。如果您希望能够直接从数组变量访问元素属性,则需要 PowerShell v3 或更新版本。

PowerShell v2:

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

PS C:\> $qry = "SELECT * FROM Win32_Process WHERE Name LIKE '%pad%'"
PS C:\> $a = gwmi -Query $qry | select Name, ProcessId
PS C:\> $a.Name
PS C:\> $a | ft -Auto

Name        ProcessId
----        ---------
notepad.exe      1812
notepad.exe      1728

PowerShell v3:

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1

PS C:\> $qry = "SELECT * FROM Win32_Process WHERE Name LIKE '%pad%'"
PS C:\> $a = gwmi -Query $qry | select Name, ProcessId
PS C:\> $a.Name
notepad.exe
notepad.exe
PS C:\> $a | ft -Auto

Name        ProcessId
----        ---------
notepad.exe      3460
notepad.exe      3580

在 PowerShell v2 或更早版本中,如果要访问其属性,则需要获取单个数组元素:

PS C:\> $a[0].Name
notepad.exe

或者您可以选择/扩展属性:

PS C:\> $a | select -Expand Name
notepad.exe
notepad.exe

但是,我不太清楚您想要的输出是什么。您想要所有 *pad 可执行文件的名称(例如 Notepad.exe Notepad.exe Textpad.exe Notepad.exe)吗?只是唯一的名称 (Notepad.exe Textpad.exe)?每个焊盘类型的数量(3 Notepad.exe, 1 Textpad.exe)?还有什么?

另外,$a 不保证是一个数组。根据Get-WmiObject 返回的内容,它也可以是单个对象(没有Count 属性)或$null(没有结果)。更好的改变

if ( $a.Count -gt 1 ) { ... }

进入

if ( @($a).Count -gt 1 ) { ... }

【讨论】:

    【解决方案2】:

    @Ansgar Wiechers 完美地解释了它。 您也可以使用 Foreach-object 来实现这一点。

    IF ($a.Count -ge 1 ) { $a | foreach-object {" count: " + $a.Count + " name: " + $_.name| Out-File -append -filepath $log -encoding ASCII }}
    

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2016-04-15
      • 2018-04-24
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多