[英]Issue with PowerShell script to remotely reboot session
Problem with the script is that always reboots my own PC and not the IP or session mentioned. 脚本的问题是,总是重启我自己的PC,而不是提到的IP或会话。 Line by line it should work but I am not seeing the issue.
逐行应该可以,但是我没有看到这个问题。
Any suggestions are appreciated: 任何建议表示赞赏:
#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 }
Here the Restart-Computer
executes in the local machine itself. 在这里,
Restart-Computer
在本地计算机本身中执行。
Enter-PSSession
is for interactive remoting and cannot be used in a script. Enter-PSSession
用于交互式远程处理,不能在脚本中使用。 And to reboot a remote computer, you don't require a session to be created. 并重新启动远程计算机,则不需要创建会话。 You can Use
-ComputerName
parameter of Restart-Computer
cmdlet to reboot a remote host. 您可以使用
Restart-Computer
cmdlet的-ComputerName
参数Restart-Computer
启动远程主机。
#Example
Restart-Computer -ComputerName $Computer
And if you still want to use WSMAN for this , you can use the session with Invoke-Command
而且,如果您仍然想使用WSMAN,则可以将会话与
Invoke-Command
Invoke-Command -Session $SessionObject { Restart-Computer -Force }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.