Author Posts

September 3, 2015 at 4:20 am

$results += $computer | Select-Object name, dnshostname, operatingsystem,ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet }},@{ label = "Mac Address"; Expression={(Get-WmiObject win32_networkadapter -ComputerName $_.name).macaddress -ne $null}},@{ label = "HDD About to Fail"; Expression={Get-WmiObject -ComputerName $_.name -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ErrorAction Silentlycontinue |  Select  PredictFailure}} 

September 3, 2015 at 6:06 am

As-is, that's probably the best you can do. If you wanted to break that down into more script form, you could use Add-Member to add one property at a time. You can also hit Enter after each comma in the property list, if you just want it more neatly formatted.

September 3, 2015 at 6:18 am

Cool, appreciate your time!

September 3, 2015 at 6:20 am

37mm,

It can't really be made shorter, but there are many different ways to break it down and make it easier to read, troubleshoot and maintain. Here's one way. Don and others will object to my use of backticks, but to me the benefits of the readability of the script outweigh any downside.

$PingResults    = $Null
$MacAddress     = $Null
$HDDAboutToFail = $Null

$PingResults    = tnc $_.ipv4address -InformationLevel Quiet
$MacAddress     = [boolean]( Get-WmiObject win32_networkadapter -ComputerName $_.name ).macaddress
$HDDAboutToFail = ( Get-WmiObject -ComputerName $_.name `
                                  -Namespace    root\wmi `
                                  -Class        MSStorageDriver_FailurePredictStatus `
                                  -ErrorAction  Silentlycontinue `
                  ).PredictFailure

$results += $computer | Select name,
                               dnshostname,
                               operatingsystem,
                               ipv4address,
                               lastlogondate,
                               logoncount,
                               @{ label = "PingResults"      ; Expression = { $PingResults    } },
                               @{ label = "Mac Address"      ; Expression = { $MacAddress     } },
                               @{ label = "HDD About to Fail"; Expression = { $HDDAboutToFail } }

September 3, 2015 at 6:32 am

Another approach on making it better readable.

$Properties = @(
    'name'
    'dnshostname'
    'operatingsystem'
    'ipv4address'
    'lastlogondate'
    'logoncount'
    @{
        label = 'PingResults' 
        Expression = { 
            tnc $_.IPv4Address -InformationLevel Quiet }
    }
    @{
        label = 'Mac Address'
        Expression={
            (Get-WmiObject Win32_NetworkAdapter -ComputerName $_.name).MacAddress -ne $null
        }
    }
    @{
        label = 'HDD About to Fail'
        Expression={
            Get-WmiObject -ComputerName $_.name -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ErrorAction Silentlycontinue | 
                Select-Object -Property PredictFailure
        }
    }
)
$results += $computer | Select-Object -Property $Properties

September 3, 2015 at 6:52 am

Backticks could be avoided with splatting inside the scriptblock {} of the expression.

$Properties = @(
    'name'
    'dnshostname'
    'operatingsystem'
    'ipv4address'
    'lastlogondate'
    'logoncount'
    @{
        label = 'PingResults' 
        Expression = { 
            tnc $_.IPv4Address -InformationLevel Quiet }
    }
    @{
        label = 'Mac Address'
        Expression={
            $Splat = @{
                ComputerName = $_.name
                Class = 'Win32_NetworkAdapter'
            }
            (Get-WmiObject @Splat).MacAddress -ne $null
        }
    }
    @{
        label = 'HDD About to Fail'
        Expression={
            $Splat = @{
                ComputerName = $_.name
                Namespace = 'root\wmi'
                Class = 'MSStorageDriver_FailurePredictStatus'
                ErrorAction = 'Silentlycontinue'
            }
            Get-WmiObject @Splat | 
                Select-Object -Property PredictFailure
        }
    }
)
$results += $computer | Select-Object -Property $Properties

September 3, 2015 at 7:19 am

Cool, exactly what i was looking for.