Hope this is the proper forum for my question.
I am working through "Learn Powershell ToolMaking In a Month of Lunchs" and have run into a problem I cannot figure out. Chapter 10, Section 5, Listing 10.2 Finishing off the error handling.
Everything was working fine with the tool up to this point. I saved the script, ran the cmdlet for my LocalHost and got the systemInfo. However after adding "$everything_ok = true" and the rest of the described code my function stopped working.
When I comment out the lines 47,52,60,78 the function works and returns data again.
I am not quite sure what I did wrong here…hoping someone can point me back in the right direction.
Aloha!
Rich
function Get-SystemInfo {
<#…#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
HelpMessage="Computer name or IP Address")]
[ValidateCount(1,10)]
[Alias('hostname')]
[string[]] $ComputerName,
[string] $ErrorLog = 'c:retry.txt',
[switch] $LogErrors
)
BEGIN {
Write-Verbose "Error log will be $ErrorLog"
} #BEGIN
PROCESS {
Write-Verbose "Beginning PROCESS block"
foreach ($computer in $ComputerName) {
Write-Verbose "Querying $computer"
Try{
47 # $everything_ok = $True
$os = Get-WmiObject -class win32_operatingsystem`
-computerName $Computer`
-erroraction Stop
} Catch{
52 # $everything_ok = $False
# Write-Warning "$computer failed"
if ($LogErrors) {
$computer | out-file $ErrorLog -append
# Write-Warning "Logged in $errorlog"
} #if $logerrors
}# Try Catch
60 #if ($everything_ok) {
$comp = Get-WmiObject -Class Win32_ComputerSystem `
-ComputerName $computer
$bios = Get-WmiObject -Class Win32_BIOS `
-ComputerName $computer
$props = @{'ComputerName' = $computer;
'OSVersion' = $os.version;
'SPVersion' = $os.servicepackmajorversion;
'BIOSSerial' = $bios.serialnumber;
'Manufacturer' = $comp.manufacturer;
'Model' = $comp.model}
Write-Verbose "WMI queries complete"
$obj = New-Object -TypeName PSObject -Property $props
#$obj.PSObject.Typenames.Insert(0,'MOL.SystemInfo')
Write-Output $obj
78 #} #if ($everything_ok)
}#foreach
} #PROCESS
END {}
}
#Get-SystemInfo -Verbose
#Write-Host "——– PIPELINE MODE ——–"
#'OKPAL434654' | Get-SystemInfo -Verbose
#Write-Host "——– PARAM MODE ——–"
#Get-SystemInfo -host localhost, OKPAL434654 -Verbose
#Get-systeminfo -host one,two,three,four,five,six,seven,eight,nine,ten,eleven
#help get-systeminfo -full
Get-SystemInfo -computername localhost -logerrors
#Get-SystemInfo -computername OKPAL434654 | Get-member