atsur415
(Atsur415)
1
I am collecting information on hard drive type as well as usage and have found that I have to use two WMI objects to gather all the required information. The below script is working for me but I would like to output the information to a file and I cannot seem to figure this one out. Due to all of the formatting nothing seems to be working, any ideas would be greatly appreciated.
$Computers = "."
Clear-Host
foreach ($Computer in $Computers)
{
$computerFreeSpace = get-wmiobject Win32_logicaldisk -Computer $Computer -Filter drivetype=3
$computerHDDType = get-WmiObject win32_diskdrive -Computer $Computer
Write-host "System Information for: " $computerSystem.Name
"HDD Model: " + $computerHDDType.Model + " HDD Capacity: " + "{0:N2}" -f ($computerFreeSpace.Size/1GB) + "GB" + " HDD Space: " + "{0:P2}" -f ($computerFreeSpace.FreeSpace/$computerFreeSpace.Size) + " Free (" + "{0:N2}" -f ($computerFreeSpace.FreeSpace/1GB) + "GB)"
"-------------------------------------------------------"`
}
5 Spice ups
Neally
(Neally)
2
Instead of write-host, use write-output and pipe it to out-file :¬)
atsur415
(Atsur415)
3
Previously I had tried write-output and converted it to html, it came out with one asterisk. Trying again just using write-output to a simple text file works, guess I should have tried that first.
Thank you for your help!
jitensh
(JitenSh)
4
Have the best one
$serverList = "c:\COMPUTERS.TXT"
$outputCSV = "D:\RFV.csv"
$scriptpath = $MyInvocation.MyCommand.Path
#$dir = Split-Path $scriptpath
pushd $dir
[System.Collections.ArrayList]$sysCollection = New-Object System.Collections.ArrayList($null)
foreach ($server in (Get-Content $serverList))
{
"Collecting information from $server"
$totCores=0
try
{
[wmi]$sysInfo = get-wmiobject Win32_ComputerSystem -Namespace "root\CIMV2" -ComputerName $server -ErrorAction Stop
[wmi]$bios = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -computername $server
[wmi]$os = Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2" -Computername $server
[array]$disks = Get-WmiObject Win32_LogicalDisk -Namespace "root\CIMV2" -Filter DriveType=3 -Computername $server
[array]$disks = Get-WmiObject Win32_LogicalDisk -Namespace "root\CIMV2" -Computername $server
[array]$procs = Get-WmiObject Win32_Processor -Namespace "root\CIMV2" -Computername $server
[array]$mem = Get-WmiObject Win32_PhysicalMemory -Namespace "root\CIMV2" -ComputerName $server
[array]$nic = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -ComputerName $server | where{$_.IPEnabled -eq "True"}
$si = @{
Server = [string]$server
Manufacturer = [string]$sysInfo.Manufacturer
Model = [string]$sysInfo.Model
TotMem = "$([string]([System.Math]::Round($sysInfo.TotalPhysicalMemory/1gb,2))) GB"
BiosDesc = [string]$bios.Description
BiosVer = [string]$bios.SMBIOSBIOSVersion+"."+$bios.SMBIOSMajorVersion+"."+$bios.SMBIOSMinorVersion
BiosSerial = [string]$bios.SerialNumber
OSName = [string]$os.Name.Substring(0,$os.Name.IndexOf("|") -1)
ServicePack = (“SP {0}” -f [string]$os.ServicePackMajorVersion)
Arch = [string]$os.OSArchitecture
Processors = [string]@($procs).count
Cores = [string]$procs[0].NumberOfCores
}
$disks | foreach-object {$si."Drive$($_.Name -replace ':', '')"="$([string]([System.Math]::Round($_.Size/1gb,2))) GB"}
}
catch [Exception]
{
"Error communicating with $server, skipping to next"
$si = @{
Server = [string]$server
ErrorMessage = [string]$_.Exception.Message
ErrorItem = [string]$_.Exception.ItemName
}
Continue
}
finally
{
[void]$sysCollection.Add((New-Object PSObject -Property $si))
}
}
$sysCollection `
| select-object Server,TotMem,OSName,Arch,ServicePack,Processors,Cores,Manufacturer,Model,BiosDesc,BiosVer,BiosSerial,DriveA,DriveB,DriveC,DriveD,DriveE,DriveF,DriveG,DriveH,DriveI,DriveJ,DriveK,DriveL,DriveM,DriveN,DriveO,DriveP,DriveQ,DriveR,DriveS,DriveT,DriveU,DriveV,DriveW,DriveX,DriveY,DriveZ,ErrorMessage,ErrorItem `
| sort -Property Server `
| Export-CSV -path $outputCSV -NoTypeInformation
Write-Host "inventory completed check $outputCSV" -backgroundcolor "green"