##this code starts job but never exits or get any output###
clear
$test = 1
$Username = Read-host "Username (Example:Hosting.com\Your Username)"
""
$Pass = Read-Host -assecurestring "Password"
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
$Servers = Get-Content "c:\temp\new\Servers.txt"
foreach ($Server in $Servers)
{
$files= "c:\temp\new\new.ps1"
foreach ($file in $files)
{
xcopy $file \\$Server\C$ /Y
}
}
$CSVContent=@()
$sb = {c:\temp\new\PsExec.exe -h "\\$Server" /accepteula -u $Username -p $password -d cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\new.ps1 " > "$Test.txt"}
foreach ($Server in $Servers)
{
start-job -scriptblock $sb -ArgumentList $server
$test++
}
get-job | wait-job | receive-job
Asked
Active
Viewed 467 times
-1

Chand
- 300
- 3
- 13
-
Surprised you don't get any errors when using `\$server`. you should really be using double backslash in UNC names `\\$server\c$\ ` Also, why are you using `psexec` to run the remote command instead of PS sessions? – mrwhale Jun 04 '14 at 03:40
-
@ harry sib 1stly i am copying the script i have to run in remote machine i.e ( $files= "c:\temp\new\new.ps1") . 2ndly i cannot use the PSsessions, since it is a trusted host. – Chand Jun 05 '14 at 06:19
1 Answers
0
Here's something I just wrote up that will do what you want it to do and print all output to a file on the local PC (where you are running the script from)
$execDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$Servers = Get-Content "c:\temp\servers.txt"
foreach ($Server in $Servers)
{
#echo $Server
copy-item "C:\temp\new\new.ps1" \\$server\c$\temp\
& "$execdir\psexec.exe" \\$server powershell.exe -noprofile -executionpolicy Bypass "C:\data\new.ps1" >> "C:\temp\logs.txt"
}
If the script says it cant run powershell.exe just add the full path to where the exe is locate
I'm not sure you will be able to get this working with jobs, unless you are using powershell v4 and use foreach -Parallel
cmdlett

mrwhale
- 311
- 2
- 6
-
But this script doesn't bypass the execution policy. execution of script is restricted in our environment/ – Chand Jun 05 '14 at 04:39
-
add this in the forloop instead `copy-item "C:\temp\new\new.ps1" \\$server\c$\temp\ Invoke-Command {powershell.exe -noprofile -executionpolicy Bypass "C:\temp\new.ps1"} -ComputerName $server` – mrwhale Jun 05 '14 at 04:50
-
-
I get the following error...(So i have to use PSexec) Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authenticati on scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must b e added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenti cated. @harry sib – Chand Jun 05 '14 at 06:02
-
Changed answer to use `psexec`. I would highly recommend adding the servers you are doing this script on to the Trusted Hosts and use `Invoke-command`. would just make things easier – mrwhale Jun 05 '14 at 23:07