Solved

Powershell jobs

Posted on 2014-04-14
15
250 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
Comment Utility
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
Comment Utility
do you have powershell remoting enabled on your servers?
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
Comment Utility
Yes
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Accepted Solution

by:
becraig earned 250 total points
Comment Utility
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
Comment Utility
Great. Thanks.
I will try this and update you.
0
 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
$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
Comment Utility
@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 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
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 28

Expert Comment

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

Expert Comment

by:Justin Yeung
Comment Utility
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
Comment Utility
Thanks for the help here. I got the jobs working with my script.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now