Solved

Help with a background job in Powershell

Posted on 2010-08-16
4
1,207 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
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A procedure for exporting installed hotfix details of remote computers using powershell
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

20 Experts available now in Live!

Get 1:1 Help Now