Long weekend.

To be honest I hate those long weekends. It’s fine if you have enough days off to travel somewhere, but if you stay at home it’s usually matter of weather, and you can rely on weather in May. This year it’s really bad here in Warsaw, so there is no way I can go out with children and have some fun – we have to stay at home.

I also noticed that after last week and Scripting Games fever I’m really in a mood of writing scripts all the time. I even went for beginner events yesterday to do something. To make it almost-a-challenge I’ve done all script in VBS. And it was not that bad. Maybe I will try to do advanced in VBS too, who knows?

Two things I would like to share today: first that jobs are not that bad as I thought. It took me a while to get familiar with them, but in the end (in my opinion) it went really fine.

Second: I’ve noticed that ISE is really nice tool (misses some functionality I like in editor from PowerGUI, but is adding some other that I could not find in latter). And you can extend it using some packs that are already out there. Such as part of PowerShellPack, IsePack module. Importing it is now in first line of my Microsoft.PowerShellISE_profile.ps1. One of features it adds is “Copy-ColoredAsHTML” ([ctrl] + [alt] + [shift] +c). I guess I will use it to add code to my posts. It’s really annoying with other tools that I’ve found. And with that I have to only tweak a HTML code a bit and I get something really readable and easy to follow.

So here is function I used to ask remote PCs about their video RAM (I left only part that is using jobs):

function Get-AdapterRAM {            
            <#
                .SYNOPSIS
                    Function, that retrieves info about video memory from worstations that were provided (pipe, parameter)
                .PARAMETER _computername
                    Name of the computer which will be tested.
                .PARAMETER AsJob
                    Switch to enable background jobs.
            #>            
            param(            
                [Parameter(Mandatory=$true)]            
                [string]$_computer,                            
                [switch]$AsJob            
            )            
            <#
                * define object to be return
                * test connection
                * go for it!
            #>            
            $obj = New-Object PSObject            
            $obj | Add-Member NoteProperty _computer $null            
            $obj | Add-Member NoteProperty value 0            
                        
            if (Test-Connection -Count 1 -Quiet -ComputerName $_computer) {            
                if ($AsJob) {            
                    Start-Job -ArgumentList $_computer, $obj {param([string]$computer, [PSobject]$obj)             
                        $obj._computer=$computer            
                        $obj.value = @($(Get-WMIObject -ErrorAction SilentlyContinue -ComputerName $computer -Query "SELECT AdapterRAM FROM Win32_VideoController"))[0].AdapterRAM            
                        $obj} | out-null            
                    Write-Verbose "Started job for computer`: $_computer"            
                }                
            } else {            
                Write-Verbose "$_computer is not accessible"            
                if ($AsJob) {            
                    ConvertTo-Object $_computer $null            
                }            
            }            
        }

And here how I get results back (this is at the end, there was also some conditional receiving in the middle of the PROCESS block, but I’m not sure if it actually worked)

while (Get-Job) {            
                Get-Job | ForEach-Object {            
                    if ($_.State -eq "Completed") {             
                        Receive-Job $_ | ConvertTo-Object; Remove-Job $_             
                    } else {            
                        Wait-job $_ | Out-Null            
                    }            
                }            
            }            

Simple, and maybe not the best practice – but again, if I do something I like to first figure out a way to do that by myself. I will look at “best practices” later, once I’m familiar with task/ technology used. I also created simple function that will create output objects for me. See:

function ConvertTo-Object {            
            <#
                .SYNOPSIS
                    Simple function to generate object with compname, videomemory, calculated memory and info if comp is ready for Windows7
                .PARAMETER _computername
                    Name of the computer to which object relates to.
                .PARAMETER Value
                    Computer's video memory size.
            #>            
            
            param(            
            [parameter(ValueFromPipelineByPropertyName=$true)]            
            [string]$_computer,             
                        
            [parameter(ValueFromPipelineByPropertyName=$true)]            
            [Uint32]$value            
            )            
                        
            $ReturnObj.ComputerName = $_computer            
            if ($value) {            
                    $ReturnObj.VideoRAM = $value             
                    $ReturnObj.VideoRAMCalc = ConvertTo-Bytes $value            
            } else {            
                $ReturnObj.VideoRAM = -1            
                $ReturnObj.VideoRAMCalc = 'unknown'            
            }            
            if ($value -ge 128MB) {            
                $ReturnObj.Win7Ready = $true            
            } else {            
                $ReturnObj.Win7Ready = $false            
            }            
            $ReturnObj            
                
        }

Pipe to it object that will have required properties (computername, value – that stands for total vidoe RAM size) and you will get something really nice. Of course you have to define ConvertTo-Bytes function. But this is too simple to blog about it. 😉

See how fine code is now? I had to only specify background, overflow and font size to make it more flexible and easy to use. But this is easier than spanning all the code manually…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s