【问题标题】:Passing text value via parameter通过参数传递文本值
【发布时间】:2017-08-11 17:02:43
【问题描述】:

我有一个命令Get-WmiObject win32_service|select name, osname|? name -match $a$a=cmd.exe。问题是当我通过$a 时,输出包含所有进程。但是当我直接传递值Get-WmiObject win32_service|select name, osname|? name -match "cmd.exe" 时效果很好。它应该如何在变量中传递进程名称?

【问题讨论】:

  • $a 设置为等于"cmd.exe"。用引号括起来使其成为字符串。

标签: powershell parameter-passing


【解决方案1】:

为什么要对作为本机 PS 命令存在的东西使用 WMI 调用?

Get-Process | ? Name -eq cmd.exe

【讨论】:

  • 对,但是如果我尝试在远程主机上执行该 cmdlet:Invoke-Command -ComputerName $ip -credential $Credential -scriptblock {Get-WmiObject win32_process |select name, osname|? name -match $a},那么输出中的所有进程都会再次出现。
  • @TraPS Invoke-Command -ComputerName $ip -credential $Credential -scriptblock {$a = 'cmd.exe' ;Get-WmiObject win32_process |select name, osname|? name -match $a}
  • 对,但是如果我想传递给$a 不仅是“cmd.exe”,而且可能取决于输入参数。我的意思是一次 $a"cmd.exe" 下一次 '"powershell.exe"` 等等。
猜你喜欢
  • 2023-03-05
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2017-12-18
  • 2022-08-23
  • 2012-05-20
  • 1970-01-01
相关资源
最近更新 更多