Question

I am trying to provide a list of servers in our domain that lists 2003 and 2008/r2 servers. Along with this information i would like to give the current status of their individual C Drive "free space" & "size of the disk". The script below runs fine and prints a list of all the correct operating systems - But...

The free space and Size are all identical..it gives the first servers drive status and replicates this untill the script finishes. for example the script prints:

serverName1   Windows server 2003 standard   deviceid=c    freespace=40gb size=12gb
serverName2   Windows server 2008r2 standard   deviceid=c  freespace=40gb size=12gb
....
serverName100 .....                                        freespace=40gb size=12gb



Import-Module activedirectory
$2008LogPath = "e:/2008servers.txt"
$2003LogPath = "e:/2003servers.txt"
$servers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
foreach ($server in $servers) {
   if($server.OperatingSystem -match "Windows Server 2008") {
   Get-WmiObject win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
   ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2008LogPath }

   elseif($server.operatingsystem -match "Windows Server 2003") {
   Get-WmiObject win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
   ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2003LogPath }
}
Was it helpful?

Solution

You'll need to use the -ComputerName parameter of the Get-WmiObject cmdlet, to retrieve information from those remote computers. If you don't specify the -ComputerName parameter, then you're retrieving WMI data from the local computer.

To fix this, change your foreach loop to look like the following:

foreach ($server in $servers) {
   if($server.OperatingSystem -match "Windows Server 2008") {
   Get-WmiObject -ComputerName $Server.Name -Class win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
   ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2008LogPath }

   elseif($server.operatingsystem -match "Windows Server 2003") {
   Get-WmiObject -ComputerName $Server.Name -Class win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
   ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2003LogPath }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top