Hi All,
I'm new to PowerShell and struggling to find a way forward with using the win32_logicaldisk class within my basic script.
I need all the disk results sent out to the body.
But the letter is received blank.
one disk is not the problem.
The results are as follows in mail.
磁碟機代碼:C:
磁碟機名稱:
磁碟機大小:231.6 GB
剩餘的空間:87.6 GB
已用的空間:143.9 GB
已用的比例:62%
Two disk errors.
Method invocation failed because [System.Object[]] doesn't contai
n a method named 'op_Division'.
At C:\Users\Administrator\Desktop\test.ps1:11 char:1
+ $body = "磁碟機代碼:{0}" -f $Disks.DeviceID + "`r`n"+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Division:Str
ing) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Hope someone can help.
$TO="XXXXXXX"
$strComputer="SERVER.johny.local"
$Disks=Get-WmiObject-ClassWin32_LogicalDisk-Filter"DriveType = 3"` -ComputerName$strComputer
$SMTPServer="smtp.gmail.com"
$SMTPPort="587"
$Username="XXXX
$Password="XXXX"
$subject="XXXX"
$body="磁碟機代碼:{0}"-f$Disks.DeviceID +"`r`n"+
"磁碟機名稱:{0}"-f$Disks.VolumeName +"`r`n"+
"磁碟機大小:{0:0.0} GB"-f ($Disks.Size/ 1GB) +"`r`n"+
"剩餘的空間:{0:0.0} GB"-f ($Disks.FreeSpace/ 1GB)+"`r`n"+
"已用的空間:{0:0.0} GB"-f ($Used= ([int64]$Disks.size - [int64]$Disks.FreeSpace)/ 1GB) +"`r`n"+
"已用的比例:{0:N0}%"-f ($Used= ([int64]$Disks.size - [int64]$Disks.FreeSpace)*100.0/$Disks.Size )
foreach ($Diskin $Disks)
{
$ID="磁碟機代碼:{0}"-f$Disk.DeviceID
$Label="磁碟機名稱:{0}"-f$Disk.VolumeName
$Size="磁碟機大小:{0:0.0} GB"-f ($Disk.Size/ 1GB)
$FreeSpace="剩餘的空間:{0:0.0} GB"-f ($Disk.FreeSpace/ 1GB)
$Used= ([int64]$Disk.size- [int64]$Disk.FreeSpace)
$SpaceUsed="已用的空間:{0:0.0} GB"-f ($Used/ 1GB)
$Percent= ($Used* 100.0)/$Disk.Size
$Percent="已用的比例:{0:N0}"-f$Percent
"$ID"
"$Label"
"$Size"
"$FreeSpace"
"$SpaceUsed"
"$Percent"
$message=new-objectNet.Mail.MailMessage ($username, $to,$subject,$body)
$smtp=New-ObjectNet.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL= $true
$smtp.Credentials=New-ObjectSystem.Net.NetworkCredential($Username,$Password)
$smtp.send($message)
}