Reputation: 1
Get-WmiObject delay outcome when check C disk storage.
When I input my computer name and type '1' to check the C disk storage, the first time won't return the outcome, and I need to type '1' again it will return both the first and second outcome.
However, if I test the function or the line Get-WMIObject separately, it works perfect.
Anyone have any idea what's going on here?
$ComputerNumber = (read-host "Provide computer number").trim()
function Show-Storage{
Get-WmiObject -Class win32_logicaldisk -Filter "DeviceID='C:'" -ComputerName $ComputerNumber|select PSComputerName,DeviceID,@{n='size(GB)';e={$_.size/1gb -as [int]}},@{n='Free(GB)';e={$_.freespace/1gb -as [int]}}
}
function Show-Menu
{
param (
[string]$Title = 'Computer Info'
)
Write-Host "================ $Title ================"
Write-Host ""
Write-Host "1: Press '1' to get Current Computer Usage"
Write-Host "2: Press '2' to delete Local User Profile"
Write-Host "Q: Press 'Q' to quit."
}
$a=1
While ($a -eq 1)
{
write-host ""
Show-Menu
write-host ""
if ($ComputerNumber -ne $null){
write-host "Selected Computer '$ComputerNumber'"
}
else{write-host "No Computer selected"}
$selection = Read-Host "Please make a selection"
switch($selection)
{
'1'{
write-host ""
Show-Storage
write-host ""
}
'2'{
delete-profile
}
'Q'{$a=0}
}
}
I just found another interesting thing, if I leave the line $result, the result will come before the "WMI end", but if I remove that line, the result will still comes after "WMI end"
Write-Host "WMI Start"
$result = Get-WmiObject -query "SELECT * FROM Win32_logicalDisk WHERE DeviceID = 'C:'" -ComputerName $PC
$result
$result |select PSComputerName,DeviceID,@{n='size(GB)';e={$_.size/1gb -as [int]}},@{n='Free(GB)';e={$_.freespace/1gb -as [int]}}
Write-Host "WMI End"
Upvotes: 0
Views: 82
Reputation: 1
Found solution finally, use | Out-Host
can fix this issue.
Reference: Get-WMIObject returning multiple responses in a script, only one when run alone
Upvotes: 0