How can I shorten/Clean up this extremely long line

Welcome Forums General PowerShell Q&A How can I shorten/Clean up this extremely long line

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
3 years, 4 months ago.

  • Author
    Posts
  • #29279

    Participant
    Points: 0
    Rank: Member
    $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

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    Cool, appreciate your time!

  • #29294

    Participant
    Points: 5
    Rank: Member

    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

    Moderator
    Points: 24
    Team Member
    Rank: Member

    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

    Moderator
    Points: 24
    Team Member
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Cool, exactly what i was looking for.

  • #29304

    Participant
    Points: 0
    Rank: Member

The topic ‘How can I shorten/Clean up this extremely long line’ is closed to new replies.