<#Created By - Abhishek Bansal
Use - Script that runs on Servers mentioned in Input.txt file, fetches details of Servers having Unallocated free space right after Recovery Partition.
Prerquistes - Copy entire code, save it in a file with .ps1 extension. On same location, create another file named "Input.txt" and mention all the servers against which this script will run.
Output - Two Output files, Output.csv containing details and Errorlogs.csv containing errors if any occued during execution of script.
#>
$servers = Get-Content .\Input.txt
$line = 0
$linecount = $servers.Count
$pct = 0
foreach($server in $servers)
{
$line = $line + 1
$pct = $line/$linecount*100
$Error.Clear()
Write-Progress -Activity "Checking $server" -Status "$line out of $linecount" -PercentComplete $pct
Write-Host -ForegroundColor Green "Exporting results for $server."
try{
$OSDisk = Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock{Get-Disk | ?{$_.bootfromdisk -eq $true}}
$partitions = Invoke-Command -ComputerName $server -ScriptBlock{Get-Partition -DiskNumber $using:OSDisk.Number | Sort-Object Offset} -ErrorAction Stop
$recoverypartition = $partitions | Where-Object {$_.Type -eq 'Recovery'} | Sort-Object Offset -Descending | Select-Object -First 1
if ($recoverypartition) {
$recoveryEnd = $($recoverypartition.Offset + $recoverypartition.Size)
$lastoffset = $partitions | Select-Object -Last 1
if($($recoveryEnd /1GB) -lt $($lastoffset.Offset /1GB))
{
$server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"Recovery Partition is not at the end of volume, hence skipping checks"}},@{n="Size (GB)";e={"NA"}} | Export-Csv ./Output.csv -NoTypeInformation -Append
}
else
{
if($($OSDisk.Size /1GB) -gt $($recoveryEnd /1GB))
{
$server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"Yes, Unallocated space exists."}},@{n="Size (GB)";e={$($OSDisk.Size /1GB - $($recoveryEnd /1GB))}} | Export-Csv ./Output.csv -NoTypeInformation -Append
}
else
{
$server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"No, Unallocated space exists."}},@{n="Size (GB)";e={"NA"}} | Export-Csv ./Output.csv -NoTypeInformation -Append
}
}}
else
{
$server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"No Recovery Partition exists."}},@{n="Size (GB)";e={"NA"}} | Export-Csv ./Output.csv -NoTypeInformation -Append
}
}
catch{
$server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Error";e={$Error.exception.Message}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append
}
}
No comments:
Post a Comment