How can I shorten/Clean up this extremely long line

This topic contains 7 replies, has 4 voices, and was last updated by  37mm 2 years, 1 month ago.

  • Author
    Posts
  • #29279

    37mm
    Participant
    $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}} 
    
  • #29284

    Don Jones
    Keymaster

    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.

  • #29292

    37mm
    Participant

    Cool, appreciate your time!

  • #29294

    Tim Curwick
    Participant

    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 } }
    
  • #29298

    Daniel Krebs
    Moderator

    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
    
  • #29299

    Daniel Krebs
    Moderator

    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
    
  • #29301

    37mm
    Participant

    Cool, exactly what i was looking for.

  • #29304

    37mm
    Participant

You must be logged in to reply to this topic.