0

Problem with the script is that always reboots my own PC and not the IP or session mentioned. 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 }

1 Answer 1

2

Here the Restart-Computer executes in the local machine itself.

Enter-PSSession is for interactive remoting and cannot be used in a script. 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.

#Example
Restart-Computer -ComputerName $Computer

And if you still want to use WSMAN for this , you can use the session with Invoke-Command

 Invoke-Command -Session $SessionObject { Restart-Computer -Force }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.