So i found Scrip on online and ever since i am trying modify it to use it for my liking.
basically i am looking to create a function in Powershell which can list out some WMI objects like SystemName,PageFile,FreeSpace percentage... etc..
The below script runs but the second Loop of Get-WmiObject seems to be ignored.
I am very new and unable to spot the issue.
function Get-DiskInfo {param(
[String[]]$ComputerName = $ENV:ComputerName,
[Switch]$LogOffline
)
$ComputerName | ForEach-Object {
try {
$Computer = $_
$Params = @{
Namespace = 'root\cimv2'
class = 'win32_volume'
ComputerName = $Computer
ErrorAction = 'Stop'
}
$Params2 = @{
Namespace = 'root\cimv2'
class = 'win32_PhysicalMemory'
ComputerName = $Computer
ErrorAction = 'Stop'
}
Get-WmiObject @Params | ForEach-Object {
$hash=@{
ComputerName = $_.__SERVER
FreeSpace = ($_.freespace/$_.capacity * 100)
Drive = $_.Caption
Name = $_.Label
}
New-Object psobject -Property $hash
}#Foreach-Object(Adapter)
Get-WmiObject @Params2 | ForEach-Object {
$hash2=@{
ComputerName = $_.__SERVER
TotalPhysical = $_.Capacity
}
New-Object psobject -Property $hash2
}#Foreach-Object(Adapter)
}#try
catch {
Write-Warning -Message $_
if ($LogOffline)
{
"$Computer is offline or not supported" >> "$home\desktop\Offline.txt"
}
}
}#Foreach-Object(Computer)
}#Get-DiskInfo