调用cmdlet时使用变量作为动词

时间:2015-09-25 23:11:58

标签: powershell

我正在尝试编写一个脚本,用于启动,停止或重新启动远程计算机上的服务。

我发现了这个:

Start-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

并开始围绕它构建我的脚本。

这就是我所拥有的:

$Action = Read-Host "Start, Stop or Restart service?"
$Serv = Read-Host "Service name?"
$Comp = Read-Host "Computer name?"
"$Action"-Service -InputObject $(Get-Service -Computer "$Comp" -Name "$Serv")

当我运行此脚本时,我收到此错误:

At U:\Serv.ps1:4 char:10
+ "$Action"-Service -InputObject $(Get-Service -Computer "$Comp" -Name "$Serv")
+          ~~~~~~~~
Unexpected token '-Service' in expression or statement.
At U:\Serv.ps1:4 char:19
+ "$Action"-Service -InputObject $(Get-Service -Computer "$Comp" -Name "$Serv")
+                   ~~~~~~~~~~~~

我试过这个:

$Action = Read-Host "Start, Stop or Restart service?"
$Action = $Action + "-Service"
$Serv = Read-Host "Service name?"
$Comp = Read-Host "Computer name?"
"$Action" -InputObject $(Get-Service -Computer "$Comp" -Name "$Serv")

但是我得到了类似的错误:

At U:\Serv.ps1:5 char:11
+ "$Action" -InputObject $(Get-Service -Computer "$Comp" -Name "$Serv")
+           ~~~~~~~~~~~~
Unexpected token '-InputObject' in expression or statement.
At U:\Serv.ps1:5 char:24
+ "$Action" -InputObject $(Get-Service -Computer "$Comp" -Name "$Serv")
+                        ~~
Unexpected token '$(' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

    Unexpected token '-InputObject' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : UnexpectedToken

我如何传递输入 - "开始","停止"或者"重启"到命令的动词部分?

2 个答案:

答案 0 :(得分:2)

这是使用PowerShell管理服务的一种相当复杂的方式。考虑这样做:

$Action = Read-Host "Start, Stop or Restart service?"
$Serv   = Read-Host "Service name?"
$Comp   = Read-Host "Computer name?"

$svc = Get-Service -Computer $Comp -Name $Serv
switch ($Action) {
  'Start'   { $svc | Start-Service }
  'Stop'    { $svc | Stop-Service }
  'Restart' { $svc | Restart-Service }
}

如果由于某种原因必须构造cmdlet名称,则必须使用调用运算符(&):

& $Action ...

如果没有该运算符,PowerShell将无法将您的字符串识别为cmdlet,但会(尝试)通过Out-Default将其打印出来。

另外,请考虑使用主机UI的PromptForChoice()方法而不是Read-Host

$message  = 'Manage Services'
$question = 'Start, Stop or Restart service?'

$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Start'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList 'Sto&p'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Restart'))

$Action = $Host.UI.PromptForChoice($message, $question, $choices, 0)

switch ($Action) {
  0 { $svc | Start-Service }
  1 { $svc | Stop-Service }
  2 { $svc | Restart-Service }
}

答案 1 :(得分:0)

以下是代码:

$message  = 'Manage Services'
$question = 'Start, Stop or Restart service?'
$message
Write-Host
$Comp = Read-Host "Computer name?"
Write-Host
$ListServ = Read-Host "List services on remote computer? y or n"
If ($ListServ -eq "y")
  {
    Get-Service -ComputerName $Comp
  }
Write-Host
$Serv = Read-Host "Service name?"
Write-Host

$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Start'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList 'Sto&p'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Restart'))

$Action = $Host.UI.PromptForChoice($message, $question, $choices, 0)
$svc = Get-Service -ComputerName $Comp -Name $Serv
switch ($Action) {
  0 { $svc | Start-Service }
  1 { $svc | Stop-Service }
  2 { $svc | Restart-Service }
}
$svc