I know my post have been few and far between, but I have been super swamped at work and will be for a few more months at least.

That said I am writing some scripts and functions for work and as I was writing one today I though maybe I should share how I go about writing a function slash script. I like step-by-step lists so I decided at that format.

1> A purpose
- All Scripts must start with a purpose. Be it install something or check something. It needs to do something.

2> I figure the best process to do what I want
- Can what I want done be remoted?
- Do I use WMI?
- Do I use .NET?
- Do I use external Commands?
- Who is going to be doing it? What rights do they need?
- Does it have potential to break stuff (BIGGY!!!)

3> Write a function or functions that do what I want done on one Server, Device, or file
Example

  1. function Checkhf{
  2.    Param($srv,$ptch)
  3.    $hf = get-WmiObject Win32_QuickfixEngineering -ComputerName $srv | where-Object{$_.HotfixID -match $ptch}
  4.    if($hf)
  5.    {
  6.       return $true
  7.    }
  8.    else
  9.    {
  10.       return $false
  11.    }
  12. }

4> Cut and Paste function into my templates (At the bottom of post)
- Add any missing parameters
- Add any extra parsing
- Add error control if needed

5> Now it looks like this

  1. function Check-Hotfix {
  2.     Param([string]$server,[string]$hotfix)
  3.     Begin{
  4.         function PingServer {
  5.             param([string]$srv)
  6.             $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
  7.             if($pingresult.statuscode -eq 0) {$true} else {$false}
  8.         }
  9.         function Checkhf{
  10.             Param($srv,$ptch)
  11.             $hf = get-WmiObject Win32_QuickfixEngineering -ComputerName $srv | where-Object{$_.HotfixID -match $ptch}
  12.             if($hf)
  13.             {
  14.                 return $true
  15.             }
  16.             else
  17.             {
  18.                 return $false
  19.             }
  20.         }
  21.         Write-Host
  22.         $process = @()
  23.     }
  24.     Process{
  25.         if($_)
  26.         {
  27.             if($_.ServerName)
  28.             {
  29.                 $process += $_.ServerName
  30.             }
  31.             else
  32.             {
  33.                 $process += $_
  34.             }
  35.         }
  36.     }
  37.     End{
  38.         if($Server){$Process += $Server}
  39.         if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
  40.         foreach($s in $process)
  41.         {
  42.             if(PingServer $s)
  43.             {
  44.                 $myobj = "" | select-Object Server,Result
  45.                 $myobj.Server = $s
  46.                 $myobj.Result = Checkhf -srv $s -ptch $hotfix
  47.                 Write-Output $myObj
  48.             }
  49.         }
  50.     }
  51. }

6> Final Test

Here are my Templates
- By default they take a Pipe, Server, or File and will process all three

Template for Script

  1. Param([string]$server,[string]$file)
  2. Begin{
  3.     function PingServer {
  4.         param($srv)
  5.         $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
  6.         if($pingresult.statuscode -eq 0) {$true} else {$false}
  7.     }
  8.     function InsertFunctionHere{
  9.     }
  10.     Write-Host
  11.     $process = @()
  12. }
  13. Process{
  14.     if($_)
  15.     {
  16.         if($_.ServerName)
  17.         {
  18.             $process += $_.ServerName
  19.         }
  20.         else
  21.         {
  22.             $process += $_
  23.         }
  24.     }
  25. }
  26. End{
  27.     # Check if $server is populated and add to Process Array
  28.     if($Server){$Process += $Server}
  29.     # Check if $file is populated and add to Process Array
  30.     if($file)
  31.     {
  32.         foreach($entry in (get-content $file))
  33.         {
  34.             $Process += $entry
  35.         }
  36.     }
  37.     # If Process Array is empty… Add self
  38.     if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
  39.     foreach($s in $process)
  40.     {
  41.         if(pingserver $s)
  42.         {
  43.             InsertFunctionHere $s
  44.         }
  45.         Write-Host
  46.     }
  47. }

Template for Function

  1. function Verb-Noun {
  2.     Param([string]$server,[string]$file)
  3.     Begin{
  4.         function PingServer {
  5.             param($srv)
  6.             $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
  7.             if($pingresult.statuscode -eq 0) {$true} else {$false}
  8.         }
  9.         function InsertFunctionHere{
  10.         }
  11.         Write-Host
  12.         $process = @()
  13.     }
  14.     Process{
  15.         if($_)
  16.         {
  17.             if($_.ServerName)
  18.             {
  19.                 $process += $_.ServerName
  20.             }
  21.             else
  22.             {
  23.                 $process += $_
  24.             }
  25.         }
  26.     }
  27.     End{
  28.         # Check if $server is populated and add to Process Array
  29.         if($Server){$Process += $Server}
  30.         # Check if $file is populated and add to Process Array
  31.         if($file)
  32.         {
  33.             foreach($entry in (get-content $file))
  34.             {
  35.                 $Process += $entry
  36.             }
  37.         }
  38.         # If Process Array is empty… Add self
  39.         if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
  40.         foreach($s in $process)
  41.         {
  42.             if(pingserver $s)
  43.             {
  44.                 InsertFunctionHere $s
  45.             }
  46.             Write-Host
  47.         }
  48.     }
  49. }