Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell jobs

Posted on 2014-04-14
15
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 35

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 35

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 71

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 1000 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 35

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 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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