Posts RSS Comments RSS 249 Posts and 391 Comments till now

Birth of a Script/Function

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

function Checkhf{
   Param($srv,$ptch)
   $hf = get-WmiObject Win32_QuickfixEngineering -ComputerName $srv | where-Object{$_.HotfixID -match $ptch}
   if($hf)
   {
      return $true
   }
   else
   {
      return $false
   }
}

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

function Check-Hotfix {
    Param([string]$server,[string]$hotfix)
    Begin{
        function PingServer {
            param([string]$srv)
            $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
            if($pingresult.statuscode -eq 0) {$true} else {$false}
        }
        function Checkhf{
            Param($srv,$ptch)
            $hf = get-WmiObject Win32_QuickfixEngineering -ComputerName $srv | where-Object{$_.HotfixID -match $ptch}
            if($hf)
            {
                return $true
            }
            else
            {
                return $false
            }
        }
        Write-Host
        $process = @()
    }
    Process{
        if($_)
        {
            if($_.ServerName)
            {
                $process += $_.ServerName
            }
            else
            {
                $process += $_
            }
        }
    }
    End{
        if($Server){$Process += $Server}
        if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
        foreach($s in $process)
        {
            if(PingServer $s)
            {
                $myobj = "" | select-Object Server,Result
                $myobj.Server = $s
                $myobj.Result = Checkhf -srv $s -ptch $hotfix
                Write-Output $myObj
            }
        }
    }
}

6> Final Test

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

Template for Script

Param([string]$server,[string]$file)
Begin{
    function PingServer {
        param($srv)
        $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
        if($pingresult.statuscode -eq 0) {$true} else {$false}
    }
    function InsertFunctionHere{
    }
    Write-Host
    $process = @()
}
Process{
    if($_)
    {
        if($_.ServerName)
        {
            $process += $_.ServerName
        }
        else
        {
            $process += $_
        }
    }
}
End{
    # Check if $server is populated and add to Process Array
    if($Server){$Process += $Server}
    # Check if $file is populated and add to Process Array
    if($file)
    {
        foreach($entry in (get-content $file))
        {
            $Process += $entry
        }
    }
    # If Process Array is empty… Add self
    if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
    foreach($s in $process)
    {
        if(pingserver $s)
        {
            InsertFunctionHere $s
        }
        Write-Host
    }
}

Template for Function

function Verb-Noun {
    Param([string]$server,[string]$file)
    Begin{
        function PingServer {
            param($srv)
            $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
            if($pingresult.statuscode -eq 0) {$true} else {$false}
        }
        function InsertFunctionHere{
        }
        Write-Host
        $process = @()
    }
    Process{
        if($_)
        {
            if($_.ServerName)
            {
                $process += $_.ServerName
            }
            else
            {
                $process += $_
            }
        }
    }
    End{
        # Check if $server is populated and add to Process Array
        if($Server){$Process += $Server}
        # Check if $file is populated and add to Process Array
        if($file)
        {
            foreach($entry in (get-content $file))
            {
                $Process += $entry
            }
        }
        # If Process Array is empty… Add self
        if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
        foreach($s in $process)
        {
            if(pingserver $s)
            {
                InsertFunctionHere $s
            }
            Write-Host
        }
    }
}

No Responses to “Birth of a Script/Function”

  1. on 14 Jun 2007 at 12:26 pmhalr9000

    Hi, can you explain this construct for me?

    myobj = “” | select-Object Server,Result

Trackback this post | Feed on Comments to this post

Leave a Reply