Powershell – Can’t Get PSExec to work with Powershell

powershellscripting

Last week I developed a script that would check if psremoting was enabled on specified machines. This week I began working on a script that would enable psremoting on specified machines but I can't get psexec to run in powershell (Also, yes I know psremoting can be enabled through group policy). Here is my script:

$input = Read-Host @"
Select Option
(1)Manually enter computer(s)
(2)Retrieve computer(s) from file

Option
"@

If ($input -eq 1){
    $count = Read-Host "How many computers"
    $Computers = 1..$count
    $b=0;$c=1; ForEach ($Computer in $Computers) {$Computers[$b] = Read-Host "Computer" $c; $b++; $c++}
} ElseIF ($input-eq 2) {
    $Computers = Read-Host "File" 
    $Computers = Get-Content $Computers
} Else {
    write-host "Invalid Option"
    Exit
}

cls
$User = Read-Host "Enter username"
$Pass = Read-Host "Enter password"
cls

$PSExec = "C:\Windows\System32\PSExec\PSExec.exe"

ForEach ($Computer in $Computers){

# & $PSExec \\$Computer -u $User -p $Pass -h -c "C:\Temp\mybat.bat"
& $PSExec \\$Computer -u $User -p $Pass "ipconfig"

}

I get the following error when executing script:

PSExec.exe :
At C:\MyStuff\EnablePSRemoting.ps1:34 char:1
+ & $PSExec $Computer -u $User -p $Pass "ipconfig"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

PsExec v2.11 – Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals – www.sysinternals.com
The system cannot find the file specified.

I then attempted to simply run PSExec from powershell directly, still no luck.

Best Answer

Start-Process -Filepath "$PSExec" -ArgumentList "\\$computer -u $user -p $pass $command"does exactly what I need it to do.