Solved

Powershell jobs

Posted on 2014-04-14
15
254 Views
Last Modified: 2014-04-20
Hi All,

Please help me convert this below script to run in threaded (parallel processing). I have heard that jobs in powershell can do that. Please help.

$servers=Get-Content -Path ".\computers.txt"
ForEach ($server in $servers)
{
	$PingStatus = Test-Connection -ComputerName $server -Count 1 -Quiet
   	If ($PingStatus)
	{
		if(Test-Path "\\$server\c$\scot\controls\SigPlus.ocx")
		{
			Remove-Item -Path "\\$server\c$\scot\controls\SigPlus.ocx" -Force
			Write-Host "File Deleted from $server"
		}
		Else
		{
			Write-Host "Sigplus.ocx does not exists on $server"
		}
	}
	Else
	{
		Write-Host "Unable to ping $server"
	}
}

Open in new window

0
Comment
Question by:Dhiraj Mutha
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39999047
Split your "computers.txt" file into several files of roughly the same number of computers (computer1.txt, computer2.txt etc), make them a required parameter for the script, then run the script several times, each time with a different parameter.

HTH,
Dan
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 39999056
Come on... I did not posted this question to get this kind of answer.

I am looking for paralled processing. Powershell Jobs to do that for me.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39999081
When running a powershell script, it's multi-threaded by default. You would have to specify the -Sta switch to force it to run in single-threaded mode.

The problem is that parallel processing is a function of the code written by the programmer, not of Powershell. Your code is not suited to parallel programming. All your instructions depend of the other before them. So you either refactor your code, or use more instances of PS.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 39999094
Yes that what my question is what do I have to change in my code to make use of powershel jobs?
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39999145
do you have powershell remoting enabled on your servers?
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 39999166
Yes
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39999295
Something like this should do. You won't see the results until all computers are processed, because of the Wait-Job, which will wait for all jobs to have ended.
0
 
LVL 29

Accepted Solution

by:
becraig earned 250 total points
ID: 39999378
Something like this should work:


Get-Content "ServerList.txt" | %{
  $ScriptBlock = {
   if (Test-Connection -ComputerName $_ -Count 1 -Quiet)
{
if(Test-Path "\\$_\c$\scot\controls\SigPlus.ocx")
		{
			Remove-Item -Path "\\$_\c$\scot\controls\SigPlus.ocx" -Force
			Write-Host "File Deleted from $_r"
		}
}
else {Write-Host "Unable to ping $_"}
    Start-Sleep 60
  }

  # Execute the jobs in parallel
  Start-Job $ScriptBlock
}

Get-Job
While (Get-Job -State "Running")
{
  Start-Sleep 10
}
Get-Job | Receive-Job

Open in new window

0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 39999386
Great. Thanks.
I will try this and update you.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39999396
$Error.clear()
$Contents = Get-Content .\Computers.txt'
Invoke-Command -ComputerName $Contents -ErrorAction SilentlyContinue {
$server = hostname
if(Test-Path "\\$server\c$\scot\controls\SigPlus.ocx")
		{
			Remove-Item -Path "\\$server\c$\scot\controls\SigPlus.ocx" -Force
			Write-Host "File Deleted from $server"
		}
		Else
		{
			Write-Host "Sigplus.ocx does not exists on $server"
		}
	}
$Error.ErrorDetails.Message

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39999420
@becraig: you're sure that runs multiple threads? Cause Start-Job simply starts a background job, it does not multiply it.

At least I don't see any mention of using multiple threads on the manual: http://technet.microsoft.com/en-us/library/hh849698.aspx
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 39999496
Oops, code got lost somehow in http:#a39999295
Get-Content -Path ".\computers.txt" | % {
  Start-Job -ArgumentList $_ -ScriptBlock {
    param($server)
    if (Test-Connection -ComputerName $server -Count 1 -Quiet)
    {
      if(Test-Path "\\$server\c$\scot\controls\SigPlus.ocx")
      {
	Remove-Item -Path "\\$server\c$\scot\controls\SigPlus.ocx" -Force
	Write-Host "File Deleted from $server"
      }	Else {
        Write-Host "Sigplus.ocx does not exists on $server"
      }
    } Else {
      Write-Host "Unable to ping $server"
    }
  }
} | Wait-Job | Receive-Job
Get-Job | Remove-Job

Open in new window

Dan Craciun, you are correct in that PS Jobs are not multithreaded. Concurrent jobs are emulated in PS. However, I don't think that is that important here, as you wouldn't get any advantages from real multitasking. More, effective multithreading requires a sophisticated control of worker and controler threads, adapted to the amount of virtual or real CPUs/cores.

Justin Yeung's code runs remotely, so needs to have PS Remoting (WinRM)  set up on all targets. Furthermore, the UNC path usage should be modified to local access - the process is running on the target, so no need for network paths.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39999523
Beat me to it, just tested and corrected my script.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39999544
Qlemo, as per the author, remoting is turned on.

and yes, it can be running without UNC path.
0
 
LVL 14

Author Closing Comment

by:Dhiraj Mutha
ID: 40012006
Thanks for the help here. I got the jobs working with my script.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question