Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Powershell jobs

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
Dhiraj Mutha
Asked:
Dhiraj Mutha
  • 5
  • 3
  • 3
  • +2
2 Solutions
 
Dan CraciunIT ConsultantCommented:
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
 
Dhiraj MuthaAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
Dhiraj MuthaAuthor Commented:
Yes that what my question is what do I have to change in my code to make use of powershel jobs?
0
 
Justin YeungCommented:
do you have powershell remoting enabled on your servers?
0
 
Dhiraj MuthaAuthor Commented:
Yes
0
 
QlemoC++ DeveloperCommented:
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
 
becraigCommented:
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
 
Dhiraj MuthaAuthor Commented:
Great. Thanks.
I will try this and update you.
0
 
Justin YeungCommented:
$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
 
Dan CraciunIT ConsultantCommented:
@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
 
QlemoC++ DeveloperCommented:
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
 
becraigCommented:
Beat me to it, just tested and corrected my script.
0
 
Justin YeungCommented:
Qlemo, as per the author, remoting is turned on.

and yes, it can be running without UNC path.
0
 
Dhiraj MuthaAuthor Commented:
Thanks for the help here. I got the jobs working with my script.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now