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.

Delonte Johnson
  • 341
  • 2
  • 4
  • 13
  • 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. – Delonte Johnson Aug 11 '15 at 15:10
  • @Colyn1337 I just tried `invoke-command {psexec \\Server1 -u Domain\Admin -p Pa$$w0rd ipconfig}` still nothing – Delonte Johnson Aug 11 '15 at 15:16
  • You use `Invoke-Command` in place of psexec. – Colyn1337 Aug 11 '15 at 15:17
  • @Colyn1337 I could use Copy-Item to copy file. Could I use Invoke-Command -ComputerName Server1 -Scriptblock {start-process C:\mybat.bat} -Credentials Admin to execute batch file? – Delonte Johnson Aug 11 '15 at 15:25
  • @Colyn1337 Wait that won't work. How am I to execute the file on the remote machine? – Delonte Johnson Aug 11 '15 at 15:27
  • Not to be pendantic, but the examples you have posted only have (1) forward slash. Have you tried the same commands with (2) hacks? – Get-HomeByFiveOClock Aug 11 '15 at 16:38
  • @Get-HomeByFiveOClock yes I have....nice catch though – Delonte Johnson Aug 11 '15 at 16:39
  • @Get-HomeByFiveOClock...hmmm...actually my posts has \\ but not showing up...must be some formatting issue. Added additional \ (for total of 3) now shows \\ – Delonte Johnson Aug 11 '15 at 16:41
  • There is a switch to accept the eula of ps exec. Could that be getting in the way? –  Aug 11 '15 at 16:47
  • @TimAlexander I've already accpeted EULA but I tried using the acceptEula switch in my command still nothing – Delonte Johnson Aug 11 '15 at 16:52
  • Running psexec from powershell needs some non obvious character escaping. I cannot write a proper answer right now, but you can easily google a couple of instructions out there detailing how to do it. – ErikE Aug 11 '15 at 17:12
  • ErikE - that was sort-of what I was alluding to as well. My guess is it's either some of the aformentioned non-obvious character escaping or it may have something to do with Powershell not handling running certain commands which will start a new executable (meant to be run within the command line prompt i.e try running WMIC via powershell_ise) – Get-HomeByFiveOClock Aug 11 '15 at 17:57
  • http://www.leeholmes.com/blog/2007/10/02/using-powershell-and-psexec-to-invoke-expressions-on-remote-computers/ – ErikE Aug 11 '15 at 18:01
  • @ErikE Update: The script works if I log in as 'admin' and remove -u [user] and -p [password]...though I would really like to be able to specify the user. – Delonte Johnson Aug 11 '15 at 18:38
  • @TimAlexander Update: The script works if I log in as 'admin' and remove -u [user] and -p [password]...though I would really like to be able to specify the user. – Delonte Johnson Aug 11 '15 at 18:38
  • @Get-HomeByFiveOClock Update: The script works if I log in as 'admin' and remove -u [user] and -p [password]...though I would really like to be able to specify the user. – Delonte Johnson Aug 11 '15 at 18:39

3 Answers3

4

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

Delonte Johnson
  • 341
  • 2
  • 4
  • 13
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.

Zach Bolinger
  • 304
  • 1
  • 6
-1

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

.\Psexe.exe \\$Computer

With the leading dot in place.

bjoster
  • 4,805
  • 5
  • 25
  • 33