【问题标题】:Powershell script that looks for processes and returns process, computername and username查找进程并返回进程、计算机名和用户名的 Powershell 脚本
【发布时间】:2012-11-09 22:05:01
【问题描述】:

我想在运行特定程序的用户之后搜索十个终端服务器。我希望输出包含进程、计算机名和用户。

这个命令基本上做我想要的:

Invoke-Command -ComputerName (Get-Content .\test-servers.txt) -ScriptBlock { get-wmiobject win32_process|where{$_.name -eq "iexplore.exe" }|select name, __SERVER,@{n="owner";e={$_.getowner().user}}} | Format-Table name, PSComputerName, owner -AutoSize

name         PSComputerName owner 
----         -------------- ----- 
iexplore.exe mrdsh-test     dojo03
iexplore.exe mrdsh-test     dojo03
iexplore.exe mrdsh-test     baob12
iexplore.exe mrdsh-test     baob12

但是当我尝试创建一个将进程作为参数的脚本时,我无法让它工作。

CmdletBinding()]
Param (
[parameter(mandatory=$true)][string]$process
)

Invoke-Command -ComputerName (Get-Content .\test-servers.txt) -ScriptBlock { get-wmiobject win32_process | where{param($process)$_.name  -eq $process } | select name, __SERVER,@{n="owner";e={$_.getowner().user}}} | Format-Table name, PSComputerName, owner -AutoSize

我还没有设法将 $process 参数发送到调用该命令的服务器。我该怎么做?或者有没有更简单的方法来查找进程并返回进程、计算机名和用户名?

【问题讨论】:

  • '我无法让它工作'...你有什么错误吗?

标签: powershell


【解决方案1】:

你的 where 脚本块中有 param($process),你没有设置它,所以它是空的。

编辑:

根据要求,在我的机器上取出以下参数:

[CmdletBinding()]
Param (
[parameter(mandatory=$true)][string]$process
)

Invoke-Command -ScriptBlock { get-wmiobject win32_process | where{ $_.name  -eq $process } | select name, __SERVER,@{n="owner";e={$_.getowner().user}}} | Format-Table name, PSComputerName, owner -AutoSize

它也适用于 Powershell 版本 1:

powershell -version 1.0 -noprofile -Command ".\test.ps1 -process 'chrome.exe'"

工作得很好。

【讨论】:

  • 我以为我是在运行脚本“LookForProcess.ps1 iexplore.exe”时将其添加为参数来设置的
  • 你试过没有在 where 块中的 param($process) 吗?还有问题吗?脚本的参数与您的 where 块中的参数不同。
  • 如果我删除 where 块中的 param($process),结果没有区别。好的,这是我第一次尝试编写 powershell 脚本。边做边学:)
  • 不确定我能不能帮上忙,我在本地进行了测试,对我来说效果很好。
  • 哦,这很奇怪。你能粘贴你的工作脚本吗?也许我犯了一些你没有犯过的小错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2018-08-17
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 2014-01-07
相关资源
最近更新 更多