I have a PowerShell script that reads a list of computers, gets BIOS info via WMI and exports that info to a CSV. It will stop working after a random number of iterations, no errors, the script just hangs until I kill it. Am I running into some type of limitation exporting the data to a CSV? This seems like it should work.
Thanks
Function Write-Log {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
[String]
$Level = "INFO",
[Parameter(Mandatory=$True)]
[string]
$Message,
[Parameter(Mandatory=$False)]
[string]
$logfile = "C:\Temp\GetBiosinfo\BIOSLog.log"
)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$Line = "$Stamp $Level $Message"
If($logfile) {
Add-Content $logfile -Value $Line
}
Else {
Write-Output $Line
}
}
$ComputerList = Get-Content -Path "C:\Temp\GetBiosInfo\computerlist.txt"
foreach ($computerfromlist in $ComputerList)
{
$computerinfo = "" | Select "Hostname", "Manufacturer", "Model", "BIOSVersion"
$computername = $computerfromlist
If(Test-Connection $computername -Count 1 -Quiet)
{
Start-Sleep -Seconds 5
Write-Log -Message "$computername is online"
$Model = (Get-WMIObject Win32_ComputerSystemProduct -ComputerName $computername).Name
$BIOSManuf = (Get-WmiObject -Class Win32_BIOS -ComputerName $computername).Manufacturer
$BIOSVersion = (Get-WmiObject -Class Win32_BIOS -ComputerName $computername).SMBIOSBIOSVersion
Start-Sleep -Seconds 5
Write-Log -Message "$computername WMI Info Gathered"
If(($BIOSManuf -ne $null) -or ($BIOSVersion -ne $null) -or ($Model -ne $null))
{
$computerinfo.Hostname = $computername
$computerinfo.Manufacturer = $BIOSManuf
$computerinfo.Model = $Model
$computerinfo.BIOSVersion = $BIOSVersion
$computerinfo | Export-csv -Path "C:\Temp\GetBIOSInfo\BIOSInfo.csv" -Append -NoTypeInformation
Start-Sleep -Seconds 5
Write-Log -Message "$computername online info updated in CSV"
}
Else
{
$computerinfo.Hostname = $computername
$computerinfo.Manufacturer = "Connected but no response"
$computerinfo.Model = ""
$computerinfo.BIOSVersion = ""
$computerinfo | Export-csv -Path "C:\Temp\GetBIOSInfo\BIOSInfo.csv" -Append -NoTypeInformatio
Start-Sleep -Seconds 5
Write-Log -Level WARN -Message "$computername online but no response info updated in CSV"
}
}
Else
{
Start-Sleep -Seconds 5
Write-Log -Level WARN -Message "$computername is NOT online"
$computerinfo.Hostname = $computername
$computerinfo.Manufacturer = "Can't Connect"
$computerinfo.Model = ''
$computerinfo.BIOSVersion = ''
$computerinfo | Export-csv -Path "C:\Temp\GetBIOSInfo\BIOSInfo.csv" -Append -NoTypeInformation
Start-Sleep -Seconds 5
Write-Log -Message "$computername not online info updated in CSV"
}
}