我在脚本块内的switch
语句中遇到错误。选项1正在执行,但是2和3不起作用。
$server = Read-Host -Prompt 'Enter the server name:'
$cred = Get-Credential
$choice = {
Param([int]$opt)
switch ($opt) {
1 {
Set-ExecutionPolicy Remotesigned -Force
$EP = Get-ExecutionPolicy
echo "Executionpolicy:$EP"
}
2 {
Restart-Service WinRM
$RM = Get-Service WinRM | Select Status
$RM = $RM.Status
$RMT = Test-WSMan
echo "WinRM status: $RM"
echo "Testing WSMan......"
$RMT
}
3 {
Set-ExecutionPolicy Remotesigned -Force
Restart-Service WinRM
$RM = Get-Service WinRM | Select Status
$RM = $RM.Status
$EP = Get-ExecutionPolicy
echo "WinRM status: $RM"
echo "Executionpolicy:$EP"
$RMT = Test-WSMan
echo "Testing WSMan......"
$RMT
}
}
Exit-PSSession
}
if (Test-Connection $server -Quiet) {
$session = New-PSSession -ComputerName $server -Authentication Default -Credential $cred
Invoke-Command -Session $session -ScriptBlock {
$EP = Get-ExecutionPolicy
$RM = Get-Service WinRM | Select Status
$RM = $RM.Status
Echo "**********************************************"
Echo "Executionpolicy:$EP"
echo "WinRM status: $RM"
Echo "**********************************************"
Exit-PSSession
}
$op = @"
====Option number for changes====
1.Execution Policy
2.WinRM restart
3.Both
==================================
"@
$op
$opt = Read-Host -Prompt 'Enter the option number for changes:'
Invoke-Command -Session $session -ArgumentList $opt -ScriptBlock $choice -
Remove-PSSession $session
} else {
echo "Server is unreachable"
}
Invoke-Command : A positional parameter cannot be found that accepts argument '-'. At C:\Users\Desktop\scrit.ps1:62 char:1 + Invoke-Command -Session $session -ArgumentList $opt -ScriptBlock $choice - + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand