PowerShell Tips – Multithreading a script

With the script template I normally use to adapt any scripts, the script will expect a computer parameter in string format or a computers parameters which will point to a list in text file.  It then process the list of computer(s) and output the result as PSObject.  To run through the script with thousands of computer in sequence, it takes hours sometimes.

Thanks to Mr Google, I have managed to research for a multithreaded solution for the script.


Param($ScriptFile = "get-registry.ps1",
$ComputerList = "servers.txt",
$MaxThreads = 20,
$SleepTimer = 1000,
$MaxWaitAtEnd = 1000,
$OutputType = "GridView")

$Computers = Get-Content $ComputerList
"Killing existing jobs . . ."
Get-Job | Remove-Job -Force
"Done."

$i = 0

ForEach ($Computer in $Computers){
While ($(Get-Job -state running).count -ge $MaxThreads){
Write-Progress  -Activity "Creating Server List" -Status "Waiting for threads to close" -CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($i / $Computers.count * 100)
Start-Sleep -Milliseconds $SleepTimer
}

#"Starting job - $Computer"
$i++
Start-Job -FilePath $ScriptFile -ArgumentList $Computer -Name $Computer
Write-Progress  -Activity "Creating Server List" -Status "Starting Threads" -CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($i / $Computers.count * 100)

}

$Complete = Get-date

While ($(Get-Job -State Running).count -gt 0){
$ComputersStillRunning = ""
ForEach ($System  in $(Get-Job -state running)){$ComputersStillRunning += ", $($System.name)"}
$ComputersStillRunning = $ComputersStillRunning.Substring(2)
Write-Progress  -Activity "Creating Server List" -Status "$($(Get-Job -State Running).count) threads remaining" -CurrentOperation "$ComputersStillRunning" -PercentComplete ($(Get-Job -State Completed).count / $(Get-Job).count * 100)
If ($(New-TimeSpan $Complete $(Get-Date)).totalseconds -ge $MaxWaitAtEnd){"Killing all jobs still running . . .";Get-Job -State Running | Remove-Job -Force}
Start-Sleep -Milliseconds $SleepTimer
}

"Reading all jobs"

If ($OutputType -eq "Text"){
ForEach($Job in Get-Job){
"$($Job.Name)"
"****************************************"
Receive-Job $Job
" "
}
}
ElseIf($OutputType -eq "GridView"){
Get-Job | Receive-Job | Select-Object * -ExcludeProperty RunspaceId | out-gridview

}
<div class="line number1 index0 alt2">

The multithread script calls the original scripts, with the same list of the computers specified in the text file.  It will then generate a pre-defined threads (20 in my current usage), and loop through the computer list.  Technically, it should speed up the process time by 20 times.  The trade off, is the memory usage of this script.  Be prepared to have a performance impact on the host you ran the script from.  Depending on the complexity of the script, I have so far able to run on 50 concurent threads with 4GB RAM without MAJOR problem.  However, it is advised to run this multiplethread script off peak or at a dedicated management server.

Once the script completed, it will output the result to a gridview.  Which you can filter, or copy and paste to an Excel spreadsheet for further analysis.

 

PowerShell Tips – Basic Script Template for Server Administration

After a few years of hacking other people scripts I found on Google, I came up with a basic template that I would apply everytime when I adapt a script.  There are two purposes:

  1.  ensure the input parameters are consistent amoung my scripts
  2.  ensure the output format are consistent amoung my scripts.

The sample I am using for the demostration in this post, is one of my scripts to check a registry key on one or many servers.


param
(
[Parameter(Position=0,ValuefromPipeline=$true)][string][alias("cn")]$computer,
[Parameter(Position=1,ValuefromPipeline=$false)][string]$computerlist
)

If (-not ($computer -or $computerlist))
{
$computers = $Env:COMPUTERNAME
}

If ($computer)
{
$computers = $computer
}

If ($computerlist)
{
$computers = Get-Content $computerlist
}
foreach ($system in $computers) {
$key64="software\Wow6432Node\Huntsman\patching"
$key="software\MyCompany\patching"
$keyvalue="Patch_Category"

$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$system)
$RegSubKey = $Reg.OpenSubKey($key64)
Try
{
$RegSubKey = $Reg.OpenSubKey($key64)
$value = $RegSubKey.GetValue($keyvalue)
}
catch
{
$RegSubKey = $Reg.OpenSubKey($key)
$value = $RegSubKey.GetValue($keyvalue)

}
$output = new-object PSObject
$output | add-member NoteProperty "ComputerName" -value $system
$output | add-member NoteProperty $keyvalue -value $value

$output

}

This script will take one of the two input as parameter, a computer name as string (if not default to localhost), or computers, which expect a list in text file.  If will then process the input and feed the results to a For loop.  The content of the For loop can be updated to adapt any scripts I find in Google for any purpoes.  For each of the expect output, I feed it as a PSObject, which should output nicely to a sort of table format by itself.

This script can then feed into my next script, which will allow multithreading to speed up the process.

Another Way To Run Android On Your Windows

All the while I have been using BlueStacks to run Android applications on my machines.  Looks like there is a new contender.   Just not sure if it is better.

Run Android on Windows – Fastest Android Emulator AMIDuOS™ is the fastest android emulator for windows pc, Run android apps on windows devices. It runs on nearly any Windows 7 or 8 PC or tablet device for fast, easy switching between Windows and Android environments – without the need to dual boot!

My New Ride

Yes time flies and I had been driving my Prton Perdana V6 for almost 22 months.  The ride quality was comfortable and reasonably quiet.  At times I almost forgot it was a 12 years old car.

In the recent months the car has been giving me more trouble.  Apart from the routine maintainence which I do, as my other cars in the past, there were things just started to age.

Originally my plan for the car was to get it repaint and fixed up the dents, flush out all of the fliuds again and use it for the next 6 months.  I was even planning to drive it up north for my pre-wedding photoshoot.

This had gotten me rethink of the planning four weeks ago, when my gf bff came to visit us.  The car broke down at the middle of a hill at night, raining while we were planning for a visit to Inside Scoop.  Lucky for us, we were very close to our place, and I managed to reverse the car in free flow manner to a safe spot wihtout causing more trouble.

That is it.

So I told myself, this car is not reliable to fit in my plan anymore and I have decided to change car within the next 2 weeks.  So I look at my first choice, and ended up … skipping it.

The reason, simple, it is an old car too despite how much I love to drive a E60, I had to find a reliable car to travel around with my gf.  So I went for my second choice.  It was withina similar budget range, but a newer vehicle still with remaining 3.5 years manufacturer warranty as a pre-owned car.  I present you, another Proton.

SONY ILCE-7 (35mm, f/8, 1/60 sec, ISO100)

Well, I am a petrolhead.  I read a lot about cars and I know what I want.  The fact is, what I want is not what I need this time round.  I needed a car that give me less trouble, reasonably new, within my budget and reasonaly free of trouble.  From my months or reseasrch, the Suprima S has very little problem.  Well, at least the owners at the forums are not complaining about a lot of major issue apart from the integrated multimedia screen.  I am confident that my investment will be a wise one.  And so it is.

I have been driving the Suprima S for 3 weeks.  Lack of power?  No.  Enough power for me?  No.  Sound proofing definately need to improve.  Handling I can’t comment at this point in time, as the tyres are so bad that it understeer at almost every corner I took at the speed I usually keeps.

Being a used car the previuos owner kept it reasonaly well.  There are dents on the bonnect which I think it is an easy fix.  The driver side door was out of alignment manner.  I sent to the Proton Service Center at USJ for the first service, surprise suprise they asked me to pay for a new door as the realignment didn’t fix the problem.  I choose to leave it as is.  For Now.

Overall,  I am a happy man.  Knowning that I SHOULD be having a less hassle car for the next few years with a reasonable price.

OH yes, I like the front parking sensors.  I managed to do a U turn at a very tight corner I never thought I can.  🙂

 

Upgrading Windows 7 Pro VM to Windows 10

Well as you know I like my VMware Workstation labs.  I have been running the Windows 10 preview in my lab for a while and I thought maybe it is time to properly license it now that it has been rollout for quite sometime.  I am running Windows 10 pro as my host OS which upgraded as part of the preview tester.

For the VM, I am thinking may I can install a previous non enterprise version of the Windows and upgrade to Windows 10.  Unfortunately I have lost most of my Technet subscriptions downloads (T.T) but I do have the ISO for Windows 7 Ultimate Pro with the Technet license assigned to me.  So I installed a copy of the Windows 7 on a VM and wait for the update to arrive.

Yes it did show me to notification to update but it stopped at the following screen.

 

Bugger.  But,  Google is your friend.

I came across this post which provide a fix to allow your Windows 7 in VM to upgrade to Windows 10 without the SVGA issue.

And so I have the first Windows 10 VM licensed and ready to be cloned for many more.  Thank you Google.

 

PS:  Oh yea I also force the update immediately without waiting for it by visiting this link.  The Windows 10 download should show up after you check for Windows Update again under the Optional section.

Car Hunting..

I am driving a Proton Perdana V6 (Mitsubishi Eterna).  It is 12 years old and the suspension has been giving out weird annoying noises.  I have been lookong for the next replacement, which E60 is one of the car in my shoppong list.  Given the 5 series is a long car, I am a bit concern about my ability to maneuver the car.  Today while finished a disappointing warehouse sales, there is one park right in front of my car.  Which car is longer do you think?

Redbull Air Race

Redbull Airrace was one of the must go events when I was still in Perth.  They stopped doing it in Perth from 2014 though but I did when to the Putrajaya one last year.  Lets hope it comes again this year.  Seeing the haze polution from Indonesia these few weeks, it will sure cancelled anyway.

The Fen Pang web notes.