Good day
I have an issue with a PS script I am writing to retrieve disk space for all my servers.
Script :
-----------------------------------------------------------------------------------
$date = Get-Date
$date = $date.ToString('dd-MM-yyyy')
$servers = Get-Content c:\1\servers.txt
Foreach ($server in $servers)
{
$results += get-wmiobject win32_logicaldisk -computer $server | Where-Object { $_.DriveType -eq 3 } | Select-Object SystemName,DeviceID,@{Name="Free Space";Expression={$_.freespace / 1GB}},@{Name="Total Space";Expression={$_.size
/ 1GB}},@{Name="Percent Free";Expression={($_.freespace/$_.size)* 100}}
}
$results | export-csv -path "c:\DiskSpace_$date.csv" -notype
send-mailmessage -to "User1 <U1@domain.com>" -from "User2 <U2@domain.com>" -subject "Server HDD Space Report" -body "Please see the attached Server HDD capacity report..." -Attachments "c:\DiskSpace_$date.csv"
-smtpServer smtp.local.domain.com
remove-item "c:\DiskSpace_$date.csv" -Force
-----------------------------------------------------------------------------------
The script works 100% with all of my Windows server 2008 R2 servers but breaks as soon as I add my legacy Windows Server 2003 servers. Also when this happens, I only recieve the first server in the .csv output (The first server is a windows server 2003 server)
Error :
-----------------------------------------------------------------------------------
Method invocation failed because [System.Management.Automation.PSObject] doesn'
t contain a method named 'op_Addition'.
At C:\1\No_Creds_-_Server_Drive_Cap.ps1:6 char:16
+ $results += <<<< get-wmiobject win32_logicaldisk -computer $server | Whe
re-Object { $_.DriveType -eq 3 } | Select-Object SystemName,DeviceID,@{Name="Fr
ee Space";Expression={$_.freespace / 1GB}},@{Name="Total Space";Expression={$_.
size / 1GB}},@{Name="Percent Free";Expression={($_.freespace/$_.size)* 100}}
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], Runti
meException
+ FullyQualifiedErrorId : MethodNotFound
-----------------------------------------------------------------------------------
From what I have been able to see on the net its something to do with adding ***** = @() but I dont actually know what = @() means so I am not sure how to apply it in my script.
Any help on this one would be highly appreciated!