Solved

Help with a background job in Powershell

Posted on 2010-08-16
4
1,221 Views
Last Modified: 2012-05-10
Hey all,

I just need some help getting my script to work. What my script does is connects to a bunch of computers on the domain and retrives a value out of their registry to make sure it is set correctly. I have about 3000 machines to loop through and all of them are spread out accross the nation. Looping through these one at a time takes forever because of the latency. What I would like to do is kick off this registry read for each computer as it's own background job so more than one can run at a time. The problem is I cannot find any examples of how to start multiple jobs and retreive their output etc. Im guessing I would have to create an array of jobs??? It probably also would be better to only run maybe 10 at a time or the processor will be hammered? Anybody have any experience with this?

Here is what I have so far but cannot quite seem to get it to work:


Function Get-Registry($Computer) {

      Trap { $Output = "$computer an error occured!"; return $Output  }  
      
      $Output = "" 
      $ping = New-Object System.Net.Networkinformation.Ping
      $Result = $ping.send($Computer)
      
      if (!($Result.status -eq "success")) { $Output = "$computer is offline!"; return $Output  }
      
      $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer)
      $Key = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
      
      $RegKey = $Reg.opensubkey($Key)

      foreach($val in $RegKey.getvaluenames()) {
            if ($val.tolower() -eq "uihost") {
                  $Temp = $RegKey.getvalue("$val")
                  
                  if ($Temp.tolower() -like "logonui.exe" -or $Temp.tolower() -like "c:\windows\system32\logonui.exe") {
                        $Output = "$Computer is set correctly! UIHost = $Temp"
                        return $Output
                  } else {
                        $Output = "$Computer is not set correctly! UIHost = $temp"
                        return $Output
                  }
                  
            }
      }
      
      

}
foreach ($computer in $script:computers) {
      Write-Host -NoNewline -ForegroundColor white "Scanning: " 
      Write-Host -ForegroundColor cyan $computer.tolower()
      $Job = Start-Job -ScriptBlock { Get-Registry $Computer }  
                     Receive-job $Job
      }
}


I need to be able to take the output from my function and write it to a file....preferrably the same file and just have it appended.

Thanks

0
Comment
Question by:WTarlton
[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
4 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33446188
I would do something like this:
$computernames = $script:computers
$script = {
	  $reg = get-itemproperty "hklm:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
	  if($reg.uihost -match "logonui\.exe"){
	  	"$($env:computername) is set correctly! UIHost = $($reg.uihost)"
	  }
	  else {
	  	"$($env:computername) is not set correctly! UIHost = $($reg.uihost)"
	  }
}

$computernames | %{
	if(Test-Connection -ComputerName $_ -Quiet){
		Invoke-Command -ComputerName $_ -ScriptBlock $script -AsJob
	}
	else{
		"$_ is offline!"
	}	
}

while($jobs = Get-Job)
{
	switch($jobs){
		{$_.state -eq "Completed"}{
			Receive-Job $_
			Remove-Job $_
		}
		{$_.state -eq "Failed"}{
			"Job for computer $($_.location) has failed"
			Remove-Job $_
		}
	}
	Start-Sleep 5
} | Out-File -Append c:\yourpath\yourfile.txt

Open in new window

0
 
LVL 3

Expert Comment

by:DesertCroc
ID: 33446242
I would make it a login script and then force reboot each machine.....
0
 
LVL 6

Expert Comment

by:rwskas
ID: 33577474
Just to make sure I understand what you trying to accomlish..
You want to run multiple instances of your script, each instance pulling results from a different machine?

If that is the case, let me know, I have a few scripts that do this already.
For collecting results, have each instance 'add-content' to a logfile. Or, to ensure stability, have each instance right output to its OWN logfile, and at the end of the process, another script to get-content from each log file and add it to a master logfile and delete the various singles.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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