Monday, April 2, 2012

Month of Lunches - Day 19


Day 19 - From Command to Script to Function

Todays chapter is all about creating functions out of commands and scripts that you have created. Functions allow for easy sharing of the tools without all the fuss of teaching everyone how to modify it for their own use.

One way of doing this is to wrap your script In a function declaration much like the parameter declaration we learned about in chapter 17. Only with a function you actually give it a cmdlet like name. Here I have converted my previous parameterized script into a function, leaving the parameter in place so that we can still specify the -computer parameter when performing the function.

       function get-manmodinfo {
                                Param (
                                                $computer = 'localhost'
                                )
                               
                                $computerSystem = Get-WmiObject -ComputerName $computer win32_computerSystem
                                                write-host -NoNewLine "Computer Manufacturer: "
                                                Write-Host -ForeGroundColor red ` $computerSystem.manufacturer
                                                write-host -NoNewLine "Computer Model: "
                                                Write-Host -ForeGroundColor red ` $computerSystem.model
                }

Once this function is created in your sessions you can now just run

                Get-ManModInfo -computername 'anything'

This will display the same settings we were getting before. Now you can use this the same as a cmdlet for remote gathering of data or piping it out to export data or formatting a table or list. You can do what ever you want with it (within reason I suppose : ).

As we have gone over numerous times and I have read all over, PowerShell does not like text. Actually I remember reading "Every time you write a script that outputs text, God kills a puppy". So how do you get all those text tables and lists into an object. Section 19.5 has you covered there. Creating PSObjects.

In my previous example we are outputting with the Write-Host which is putting text to screen. Lets turn that into an object that PowerShell can better use. The way this is done is with the New-Object cmdlet. Below is my script. We will walk through and ill see if I can explain this in the right way.

                function get-manmodinfo {
                                param (
                                $computer = 'localhost'
                                )
   
                                $computerSystem = Get-WmiObject -ComputerName $computer win32_computerSystem

                                $obj = New-Object -TypeName PSObject
                                $obj | Add-Member -MemberType Noteproperty `
                                  -name Manufacturer -Value $ComputerSystem.Manufacturer
                                $obj | Add-Member -MemberType NoteProperty `
                                  -name Model -Value $computerSystem.Model

                                Write-Output $obj
                }

                get-manmodinfo | Format-Table -auto

No comments:

Post a Comment