No Header on Foreach-Object

Hello,

I have the script below that provides me network shares on specific workstations using WMI, the thing is that for every single line it print a header on the result. I want the header only as first line and after that just result. Can someone help me?

$file= "C:\PowerShell\wklist.txt"
Get-Content $file |   
ForEach-Object {
    #ICMP test 4 packets
    If(Test-Connection -ComputerName $_ -count 4 -Quiet ){
        #Read network shares
        Get-WmiObject -Class Win32_Share -ComputerName $_ | Select-Object Name,Path,PSComputerName | FT -AutoSize
    }
    #If ICMP fails
    Else{echo "N/A    OFFLINE    $_"
    }
}

Also, instead of "FT -Autosize" I'm going to use "ConvertTo-CSV".

The final script is way bigger than that since it has credential handling and other stuff.

Thanks in advance!

August 19th, 2015 12:48am

Proper syntax and thinking of design yields:

$file='C:\PowerShell\wklist.txt'
Get-Content $file |   
    ForEach-Object {
        If(Test-Connection -ComputerName $_ -count 4 -Quiet ){
            Get-WmiObject -Class Win32_Share -ComputerName $_ 
        }else{
            Write-Host "N/A    OFFLINE    $_" -fore green
        }
    } | 
    Select-Object Name,Path,PSComputerName | 
    FT -AutoSize
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 1:05am

You may want to look in to hash tables (though they can be a bit confusing for me). Sounds like that is what you want. Or I've built a custom object in to an array before and then just added to the array but I don't know how that would work with convertto-csv. I'm sure someone here will be able to clarify a bit more. But at least something to look in to, to get you started. 

For my script. I did something like the following:

$OutArray = @()   

foreach () {

           $wmioutput = your gwmi code

           $OutArray += [pscustomobject][ordered]@{ 
                 'Header' = $wmioutput
            }

}

$OutArray | Export-Csv "c:\path\.csv" -NoTypeInformation -Force

Hope it helps or at least gives you something to look in to,

Hunter

August 19th, 2015 1:14am

You may want to look in to hash tables (though they can be a bit confusing for me). Sounds like that is what you want. Or I've built a custom object in to an array before and then just added to the array but I don't know how that would work with convertto-csv. I'm sure someone here will be able to clarify a bit more. But at least something to look in to, to get you started. 

For my script. I did something like the following:

$OutArray = @()   

foreach () {

           $wmioutput = your gwmi code

           $OutArray += [pscustomobject][ordered]@{ 
                 'Header' = $wmioutput
            }

}

$OutArray | Export-Csv "c:\path\.csv" -NoTypeInformation -Force

Hope it helps or at least gives you something to look in to,

Hunter

This appears to have little to do with why he is getting multiple headers.  The FT is on each selec tand each time the loop is executed it generates a header.

If we move the selection components outside of the loop then there is only one header because we are no dealing with the aggregate.

Using a custom object makes no sense here and only adds complexity. 

We can replace FT with Export-Csv and have the complete result as needed with only one header.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 1:29am

Probably why I have 5 points and you have 114,161    :P

We're all beginners at some point. I was just trying to help. 

I'd ignore my post and follow jrv    :)

August 19th, 2015 1:33am

Probably why I have 5 points and you have 114,161    :P

We're all beginners at some point. I was just trying to help. 

I'd ignore my post and fllow jrv    :)

I was not criticizing I was just correcting your attempt.  Mistakes are inevitable.  It is how we learn.

Spend time going through as many tutorials as you can find.  You will become expert quickly.

Look at the videos in MVA. http://www.microsoftvirtualacademy.com/Studies/SearchResult.aspx?q=powershell

Scroll down for a long list.  Filter by area of interest.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 1:39am

This works just fine, thanks for the programing advice!

For those workstations that fail on ICMP, how would I properly return and offline result? I ask because the way I'm doing it doesn't let me convert to csv.

else{
            Write-Host "N/A    OFFLINE    $_" -fore green
        }

When I convert it to csv it return only blank values.

August 19th, 2015 3:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics