Getting mount points/drive/disk free space available information in PowerShell, Send-MailMessage



CLEAR
# This will output specific details for mountpoints

$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

function get-mountpoints {
$volumes = Get-WmiObject win32_volume -Filter "DriveType='3'"
$volumes |
Select Name, Label, DriveLetter, FileSystem, $TotalGB, $FreeGB, $FreePerc |
Format-Table -AutoSize
}
get-mountpoints


------another query we can use the below one

clear
#servername
$servers=@("server1","server2")
Get-WmiOBject `
-ComputerName $servers `
-Class Win32_Volume|
Select-Object @{N="Name";E={$_.Name}},
@{N="DriveLetter";E={$_.DriveLetter}},
@{N="DeviceType";
E={switch ($_.DriveType)
{
 0 {"Unknown"}
 1 {"No Root Directory"}
 2 {"Removalble Disk"}
 3 {"Local Disk"}
 4 {"Network Drive"}
 5 {"Compact Disk"}
 6 {"RAM"}
}};
},
@{N="Size(GB)";E={"{0:N1}" -f($_.Capacity/1GB)}},
@{N="FreeSpace(GB)";E={"{0:N1}" -f ($_.FreeSpace/1GB)}},
@{N="FreeSpacePercent";E={
if($_.Capacity -gt 0)
{
 "{0:P0}" -f($_.Freespace/$_.Capacity)
}
else
{
 0
}
}}|

Format-Table -AutoSize 

----File and folder size info
CLEAR
GCI -Force 'K:\MSSQL' -ErrorAction SilentlyContinue|
? {$_ -is [io.directoryinfo]}| % {
$len =0
gci -Recurse -force $_.FullName -ErrorAction SilentlyContinue |
% { $len += $_.Length}
$_.FullName,'{0:N2} GB' -f ($len / 1GB)

}

//******* Percentage of each drive **************************//

cls 
write-host `n`r "ServerName : " $env:COMPUTERNAME -ForegroundColor Yellow `n`r 
get-psdrive -psprovider filesystem |ft @{n='Drive'; e={$_.Root}}, Description, 
@{n='Total_Size'; e={'{0:N2} GB' -f [Math]::round(($_.used/1GB)+($_.free/1GB), 2)}}, 
@{n='Used_Space'; e={'{0:N2} GB' -f [Math]::round(($_.used/1GB), 2)}}, 
@{n='Free_Space'; e={'{0:N2} GB' -f [Math]::round(($_.free/1GB),2)}}, 
@{n='Free(%)';  e={'{0:N2} %' -f [Math]::round(($_.free/($_.used+$_.free)*100),2)}} 
//*********************** Each Folder size in drive **********************************//

cls 
$Percentage =@(  
gci -path "F:\" -ErrorAction SilentlyContinue |%{ 
$len =0 
gci -Recurse -force $_.FullName -ErrorAction SilentlyContinue |%{$len+=$_.Length} 
New-Object psCustomObject -Property @{ 
FileName =$_.FullName 
Size =$(` 
if($len -le 1GB){ 
'{0:N2} MB' -f ($len/1MB) 
} 
else { 
'{0:N2} GB' -f ($len/1GB) 
} 
) 
} |select FileName, Size
}
)
$Percentage |Sort-Object {[int]($_.Size -replace '(\d+).*', '$1')} 
//********************** Each file size in folder *****************************//

cls
gci -Recurse -path 'C:\' -ErrorAction SilentlyContinue |sort Length |`
ft -autosize FullName, @{n='Size'; e={
if($_.length -le 1GB){ 
'{0:N2} MB' -f ($_.length/1MB) 
} 
else { 
'{0:N2} GB' -f ($_.length/1GB) 
}  
}
}
$DiskSizeReport = @()
$computers = 'Server1','Server2','Server3','Server4'
$computers | ForEach {
$Disks = Get-WmiObject win32_logicaldisk -ComputerName $_ -Filter "Drivetype=3" -ErrorAction SilentlyContinue |
Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1GB)}},
@{Label = "Used Space (GB)";Expression = {(Round($_.Size /1GB,2)) - (Round($_.FreeSpace /1GB,2))}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1GB ) }},@{Label = "Free Space (%)"; Expression = {"{0:P0}" -f ($_.freespace/$_.size) }}
$DiskSizeReport += $Disks
} 
$DiskSizeReport | Export-CSV "C:\DiskSizeReport.csv" -NoTypeInformation -Encoding UTF8
The below piece of code written to send a with attached result set. Handled it through Send-MailMessage.
cls
$DiskSizeReport = @()
$computers = 'RAMESH'
$computers | ForEach {
$Disks = Get-WmiObject win32_logicaldisk -ComputerName $_ -Filter "Drivetype=3" -ErrorAction SilentlyContinue |
Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1GB)}},
@{Label = "Used Space (GB)";Expression = {(Round($_.Size /1GB,2)) - (Round($_.FreeSpace /1GB,2))}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1GB ) }},@{Label = "Free Space (%)"; Expression = {"{0:P0}" -f ($_.freespace/$_.size) }}
$DiskSizeReport += $Disks
} 
$DiskSizeReport | Export-CSV "C:\Azure\DiskSizeReport.csv" -NoTypeInformation -Encoding UTF8

#Above Results will be mail to respective mail id
$PSEmailServer = 'smtp-mail.outlook.com'
$UserName = "rameshmamillapalli@hotmail.com"
$Password = ConvertTo-SecureString 'passwordhere' -AsPlainText -Force
$cred = New-Object `
 -TypeName System.Management.Automation.PSCredential `
 -ArgumentList $UserName,$Password
Send-MailMessage `
-From 'rameshmamillapalli@hotmail.com' `
-To 'ramesh30.m@gmail.com' -Subject 'Disk information from RAMESH server' `
-SmtpServer $PSEmailServer `
-Port 587 `
-UseSsl `
-Credential $cred `
-Attachments "C:\Azure\DiskSizeReport.csv" 
 

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server