I’m trying to learn PowerShell and started with a “simple” project, getting free disk space from a list of specific servers.
- I get different results, mainly the sorting by “%Free”, with the same command line depending on if I specify the FQDNs on the command line after Invoke-Command -ComputerName or if I pull the FQDNs from a text file with Get-Content. To troubleshoot this, I put both versions in the same script to compare the results. That lead to an even more mysterious issue.
- The first Invoke-Command doesn’t seem to run until the second command runs even though the commands are separated by different Write-Host messages, and a Start-Sleep 60 command. There should be some sort of output from the first Invoke-Command under the “Servers specified in the command line:”.
Each line works properly, if I just cut and paste it into PowerShell.
Currently output looks like this:
Servers specified in the command line:
Servers specified in a file:
PSComputerName DriveLetter %Free
-------------- ----------- -----
VRT-SMP-002-WAT.tst.com C 60.13 %
VRT-EXC-005-WAT.tst.com C 76.94 %
VRT-RDS-001-WMT.tst.com C 78.33 %
VRT-SMP-002-WAT.tst.com D 97.90 %
VRT-RDS-001-WMT.tst.com D 99.79 %
VRT-EXC-005-WAT.tst.com D 99.81 %
VRT-EXC-005-WAT.tst.com C 76.94 %
VRT-EXC-005-WAT.tst.com D 99.81 %
VRT-RDS-001-WMT.tst.com C 78.33 %
VRT-RDS-001-WMT.tst.com D 99.79 %
VRT-SMP-002-WAT.tst.com C 60.13 %
VRT-SMP-002-WAT.tst.com D 97.90 %
Cls
Write-Host Servers specified in the command line:
Invoke-Command -ComputerName VRT-EXC-005-WAT.tst.com, VRT-RDS-001-WMT.tst.com, VRT-SMP-002-WAT.tst.com {Get-Volume -DriveLetter C,D} |Select PSComputerName, DriveLetter, @{L='%Free';E={($_.sizeremaining/$_.size).ToString("P")}} |Sort %Free
Write-Host "`n"
Start-Sleep -Seconds 60
Write-Host Servers specified in a file:
$serverlist = Get-Content D:\Scripts\John\New\DiskSpace\TestServers.txt
ForEach ($name in $serverlist)
{
Invoke-Command -ComputerName $name {Get-Volume -DriveLetter C,D} |Select PSComputerName, DriveLetter, @{L='%Free';E={($_.sizeremaining/$_.size).ToString("P")}} |Sort %Free
}
TestServers.txt contents:
VRT-EXC-005-WAT.tst.com
VRT-RDS-001-WMT.tst.com
VRT-SMP-002-WAT.tst.com
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.14393.5127
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.5127
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1