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
Solved

Help with a background job in Powershell

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Yes, Powershell again 5 23
Powershell GUI pull downs populated by MS SQL table 2 36
Power shell script 2 31
Office365 Bulk Enable not CSV 9 18
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

808 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