0

I want to run this on multiple servers(nearly 40-50 servers) parallely

$Username = "user"

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {c:\temp\PsExec.exe -h \\$server -u $Username -p $password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$env:userprofile\Desktop\output.txt"} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb
}

this code works fine if i remove start-job, but executes one after the other, which takes lot of time.

I cannot use PSsession or invoke-command, since it is restricted in our environment.

This code never exits. It stops at this position:

 + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
Community
  • 1
  • 1
Chand
  • 300
  • 3
  • 13
  • 2
    why use powershell to run psexec to run powershell ? you like complexity ? could you do something like `icm -ComputerName servername -scriptblock { invoke-expression c:\script.ps1} -asjob` – Loïc MICHEL Jun 05 '14 at 08:49
  • @Kayasax ## if i use the above expression i get the following error## Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is d ifferent from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. – Chand Jun 06 '14 at 06:53
  • I forgot to mention...I am working with powershell 2.0 – Chand Jun 06 '14 at 07:08
  • You will have to enalble remoting on the target computer first. see http://technet.microsoft.com/en-us/magazine/ff700227.aspx – Loïc MICHEL Jun 06 '14 at 07:12
  • @Kayasax enable remoting is restricted by our clients , so i am using psexec....PSexec works fine when i run one after the other, but if i try to run parellely its not supporting.... – Chand Jun 06 '14 at 08:14

1 Answers1

0

To Start with you aren't passing any of your variables into the job. What you need is to use the $args variable within the ScriptBlock and then pass the variables you want with -ArgumentList.

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {
  c:\temp\PsExec.exe -h \\$args[0] -u $args[1] -p $args[2] cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$args[3]\Desktop\output.txt"
} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}

I probably didn't need to pass the environment variable, but it seemed like you had a scoping problem with the variables.

Alternatively you could use a Param Block in the ScriptBlock to name your Variables, which essentially maps positionally the Arguments passed into the named variable.

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {
  Param ($Server,$UserName,$Password,$UserProfile)

  c:\temp\PsExec.exe -h \\$Server -u $UserName -p $Password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$UserProfile\Desktop\output.txt"
} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}

I hope this helps. Cheers, Chris.

dwarfsoft
  • 336
  • 2
  • 7