繁体   English   中英

PowerShell脚本问题以远程重新引导会话

[英]Issue with PowerShell script to remotely reboot session

脚本的问题是,总是重启我自己的PC,而不是提到的IP或会话。 逐行应该可以,但是我没有看到这个问题。

任何建议表示赞赏:

#Security Policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted

#Adding the range of IP address for Trading network
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -value '10.22.*'

#IP address of the target PC, hostnames doesn't seems to be working
$targetpc = Read-Host "Please enter the IP Address of the target PC"

New-PSSession $targetpc -Credential(Get-Credential)

$sessionid = Read-Host "Please enter the session ID"

Enter-PSSession -Id $sessionid

Write-Host Test

[string]$forcereboot = Read-Host "Would you like to force reboot the PC ? [y][n]"

if ($forcereboot -eq "y") {
    #Restart-Computer -Force
    Stop-Process -Name "Notepad"
}

else { Exit-PSSession }

在这里, Restart-Computer在本地计算机本身中执行。

Enter-PSSession用于交互式远程处理,不能在脚本中使用。 并重新启动远程计算机,则不需要创建会话。 您可以使用Restart-Computer cmdlet的-ComputerName参数Restart-Computer启动远程主机。

#Example
Restart-Computer -ComputerName $Computer

而且,如果您仍然想使用WSMAN,则可以将会话与Invoke-Command

 Invoke-Command -Session $SessionObject { Restart-Computer -Force }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM