0

First Post and new to scripting/powershell so apologies for any formatting or if I leave something out. Currently trying to use a script to restart a list of IP's from a text file.

When I run the script and run a constant ping on my test device, nothing happens. When I run psexec \insert IP shutdown /r /t 0 within powershell it works. Wondering why I can't get ISE to run my psexec command for me?

$user = $env:USERNAME

if(!(Test-Path -Path c:\temp\Scripts\$user))
{
mkdir c:\temp\Scripts\$user -Force
}

if(!(Test-Path -Path C:\temp\Scripts\$user\DeviceIps.txt))
{
New-Item -Path C:\temp\Scripts\$user\DeviceIps.txt
}

Else
{
#If the file exists, it clears your previous query
Clear-Content -Path C:\temp\Scripts\$user\DeviceIps.txt
}

#Invokes file to enter multiple searches/filters
Invoke-Item -Path C:\temp\Scripts\$user\DeviceIps.txt
Pause

$ip = Get-Content -Path C:\Temp\Scripts\$user\DeviceIps.txt

foreach($ip in $DeviceIps){
& psexec.exe \\$ip shutdown /r /t 0 
}
ouflak
  • 2,458
  • 10
  • 44
  • 49
  • See if [this answer](https://stackoverflow.com/a/57622826/45375) helps. – mklement0 Dec 28 '21 at 22:01
  • As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Dec 28 '21 at 22:02
  • Also, what doesn't work about it? Any errors? PowerShell has its own cmdlets that let you restart a remote computer as well: `Restart-Computer -ComputerName computer`. Then you wouldn't have to call on *shutdown.exe*; although this utility also supports remote hosts: `/m \\ip-or-name`. Any reason why you're using *PSExec.exe* to begin with? – Abraham Zinala Dec 29 '21 at 01:00

1 Answers1

0

Take another look at your PSEXEC command, you can run any command like you do in a batch script, so try from the command line and then just paste it into your code.

I don't have time to test this but this is aprx way of handling :

psexec \\$IP "cmd.exe" "/c shutdown /f /r/ /t 60"
PsExec64.exe @IPList.txt -u UserId -p Password -c "/c shutdown /f /r/ /t 60"

You don't need psexec if you have Windows Remote Management running :

Invoke-Command -ComputerName $env:Computername -ScriptBlock { do something }
Mike Q
  • 6,716
  • 5
  • 55
  • 62