Solved

Powershell jobs

Posted on 2014-04-14
15
255 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 70

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 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 70

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

739 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