5

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.

19
  • 1
    You should be using Invoke-Command and not PSExec Commented Aug 11, 2015 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? Commented Aug 11, 2015 at 15:02
  • It requires winrm..... which is configured by group policy. Commented Aug 11, 2015 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. Commented Aug 11, 2015 at 15:10
  • @Colyn1337 I just tried invoke-command {psexec \\Server1 -u Domain\Admin -p Pa$$w0rd ipconfig} still nothing Commented Aug 11, 2015 at 15:16

3 Answers 3

4

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

0

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.

1
  • Double backslash is before computer name in actual script. Updated post to reflect this. Also, 'Pa$$w0rd' is not the password Commented Aug 12, 2015 at 14:46
-1

I tried using this and it seems to work when using the Syntax

.\Psexe.exe \\$Computer 

With the leading dot in place.

1
  • Correction .\Psexe.exe \\$Computer Commented Sep 21, 2019 at 2:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.