Royston
Royston

Reputation: 598

Trouble Invoking PSExec from ForEach Loop to run cmd file on remote machines

I'm having issues with error 9009 and error code 6 when trying to run the below. I can successfully run this on 1 machine without the ForEach loop so, i'm wondering what's going on with PSExec here.

$Credential = Get-Credential
$Random = Get-Random -Maximum 3
ForEach ($device in $site) { 
    New-PSDrive -Name ($SiteCode + $Random) -PSProvider FileSystem -Root ('\\' + $device + '\c$') -Description STE -Credential $Credential
    Copy-Item C:\ClientInstall\ -Destination "$($SiteCode + $Random):" -Verbose -Recurse -Force
    psexec \\$device cmd.exe /c c:\ClientInstall\clientinstall.cmd
}
Remove-PSDrive -Name ($Sitecode + '*') -Force -PSProvider FileSystem

The $device variable is just an IP of the client which is in the $site variable which is a list of IP's. These are non-domain joined devices so, hence the faff.

Thanks in advance

EDIT:

I then tried the following:

$Credential = get-credential
$Random = Get-Random -Maximum 3
ForEach ($device in $site) { 
                                    New-PSDrive -Name ($SiteCode + $Random) -PSProvider FileSystem -Root ('\\' + $device + '\c$') -Description STE -Credential $Credential
                                    Copy-Item C:\ClientInstall\ -Destination "$($SiteCode + $Random):" -Verbose -Recurse -Force
                                    psexec ("\\" + $device) cmd.exe /c c:\ClientInstall\clientinstall.cmd


                                    }
Remove-PSDrive -Name ($Sitecode + '*') -Force -PSProvider FileSystem

Main Change being:

psexec ("\\" + $device) cmd.exe /c c:\ClientInstall\clientinstall.cmd

This returns:

psexec : Connecting to 10.00.00.101...
At line:1 char:1
+ psexec ("\\" + $posdevice) cmd.exe /c c:\ClientInstall\sccmguidrepair ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Connecting to 10.00.00.101...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Starting PSEXESVC service on 10.00.00.100...Connecting with PsExec service on 10.00.00.101...Starting cmd.exe on 10.00.00.101...
cmd.exe exited on 10.00.00.101 with error code 6.

Does anyone know what's going on here? Lost.

Thanks

Upvotes: 1

Views: 889

Answers (2)

Kasum
Kasum

Reputation: 21

Thanks Royston ,

I also had exit code 6 problems with psexec.

I am using it in c# application for some remote machines updates.

I have ExecuteCommand method, which takes care of cmd.exe process on my local machine.

Problem was, that i was able to execute the command only once. After the first execution, the psexec process was stopped, but the PSEXEC service was still present in state: "marked for deletion". (this was the reason for error code 6) Only solution(known to me) was to restart the server. (service stop did not worked, also changes in registry, as removal of marked_for_deletion flag were not reflected)

This is how i called the psexec, which gave me error code 6:

ExecuteCommand(@"psexec -accepteula \\" + server.Name() + " {some_command}");

And this is solution which works for me:

ExecuteCommand(@"psexec -accepteula \\" + server.Name() + " cmd.exe /c {some_command}");

Upvotes: 2

Royston
Royston

Reputation: 598

I was able to get past the above 9009 and Error Code 6 errors by using Start-Process and calling DOS to run the PSEXEC remote sessions in then call it again to run the bat file:

$Random = Get-Random -Maximum 10
ForEach ($device in $site) { 
                                    New-PSDrive -Name ($SiteCode + $Random) -PSProvider FileSystem -Root ('\\' + $device + '\c$') -Description STE -Credential $Credential
                                    Copy-Item C:\\ClientRevert\ -Destination "$($SiteCode + $Random):" -Verbose -Recurse -Force
                                    Start-Process cmd "/c C:\psexec.exe -u admin -p P@ssword \\$device\ cmd /c c:\ClientRevert\clientinstall.cmd"


                                    }


Remove-PSDrive -Name ($Sitecode + '*') -Force -PSProvider FileSystem

Upvotes: 0

Related Questions