Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
You should be using Invoke-Command and not PSExec – Colyn1337 Aug 11 '15 at 15:00
    
Invoke-Command requires remoting to be enabled, I believe, and if the script is to check if PS remoting is enabled, it's not exactly possible to use Invoke-Command, is it? – Davidw Aug 11 '15 at 15:02
    
It requires winrm..... which is configured by group policy. – Colyn1337 Aug 11 '15 at 15:03
    
@Colyn1337 I'm with david how can I use invoke-command on a remote machine with psremoting disabled? I'm trying to copy a batch file that will enable psremoting on the specified remote machines. – user2278281 Aug 11 '15 at 15:10
    
@Colyn1337 I just tried invoke-command {psexec \\Server1 -u Domain\Admin -p Pa$$w0rd ipconfig} still nothing – user2278281 Aug 11 '15 at 15:16
up vote 3 down vote accepted

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

share|improve this answer

In the script, you need the double backslash before the computername:
& \\$PSExec $Computer -u $User -p $Pass "ipconfig"

In the list of attempts directly in PowerShell, if that's the actual password, then the double dollar sign was interpreted as the last token of the last command.

share|improve this answer
    
Double backslash is before computer name in actual script. Updated post to reflect this. Also, 'Pa$$w0rd' is not the password – user2278281 Aug 12 '15 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.