Powershell Query

Associate
Joined
2 Aug 2005
Posts
589
I am new to powershell and still leaning the basics but I've got caught out doing something really simple. What I'm trying to do is export the BIOS properties of a number of servers in a text file, and export them to excel for reporting purposes, but for some reason my head can't work it out!

I've gotten so far at the moment, but this currently doesn't work:

Code:
$array = (Get-Content c:\servers.txt)

$array | ForEach-Object {
    Get-Wmiobject -ComputerName $_ win32_bios | Select-Object -Property PSComputerName, name, serialnumber, SMBIOSMajorVersion, SMBIOSMinorVersion
       }
Export-csv -path C:\export.csv -Append
 
Last edited:
You're soooo close and yet...

1) Its actually a ForEach not a ForEach-Object, dont ask.........
2) You can only use $_ when pipelinning not as part of a ForEach
3) The Export-csv has no input object and is not pipelined.

I think this is pretty close to what your trying to do, I've collapsed the Get-Content into the ForEach parameter (I think it makes more sense that way), and moved the export-csv inside the ForEach code block.

Code:
ForEach ( $server in ( Get-Content -path servers.txt ) ) {
    Get-WmiObject -ComputerName $server -Class Win32_Bios | Select PSComputerName, name, serialnumber, SMBIOSMajorVersion, SMBIOSMinorVersion | Export-csv -Path bios.csv -Append -NoTypeInformation
}

PowerShell is great, but it can be extremely frustrating at times, especially when dealing with loops, objects and trying to write to files. I've wasted whole days figuring this **** out before.
 
Deviant-4, that worked a treat.

I have also written the following script:

Code:
# Get installed software from text file

#=====================================#

# Import contents of each line of .txt file
Get-Content c:\servers.txt | 

# Process each line and run it through Get-Wmiobject
ForEach-Object {Get-WmiObject -computername $_ Win32_Product | 

# Select each of the properties to display
Select-Object -Property PSComputerName, Name, Version, Vendor} | 

# Export to .csv file
Export-CSV -Path C:\installedSoftware.csv

If I wanted to integrate that into the script you've kindly provided, would that be easily done? I assume Export-CSV wouldn't suffice, and i'd need to use the excel cmdlet?

Thanks for your help up to now
 
We can continue to use Export-Csv for the Software report as well, but I wasn't happy with the way export-csv was wrapping everything in quotes, so after a quick Google, I've changed the script a bit to eliminate them

Code:
ForEach ( $server in ( Get-Content -path servers.txt ) ) {
    Get-WmiObject -ComputerName $server -Class Win32_Bios | Select PSComputerName, name, serialnumber, SMBIOSMajorVersion, SMBIOSMinorVersion | ConvertTo-Csv -NoTypeInformation | %{ $_ -replace '"', "" } | Out-File -FilePath BIOS.csv -Append -Encoding ascii
    Get-WmiObject -ComputerName $server -Class Win32_Product | Select PSComputerName, Name, Version, Vendor | ConvertTo-Csv -NoTypeInformation | %{ $_ -replace '"', "" } | Out-File -FilePath Software.csv -Append -Encoding ascii
}
 
Back
Top Bottom