-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGet-Drives.ps1
39 lines (35 loc) · 1.38 KB
/
Get-Drives.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
param (
[Parameter(Mandatory = $false)]
[string] $ComputerName = 'localhost',
[Parameter(Mandatory = $false)]
[String] $JsonUser
)
$parms = @{
ComputerName = $ComputerName
ErrorAction = "Stop"
}
if ( ! [string]::IsNullOrEmpty($JsonUser) ) {
$hash = $JsonUser | ConvertFrom-Json
$hash.pass = $hash.pass | ConvertTo-SecureString
$parms.Credential = [PSCredential]::new($hash.user, $hash.pass)
}
try {
$out = Invoke-Command @parms {
[System.IO.DriveInfo]::GetDrives() |
Where-Object {$_.TotalSize} |
Select-Object @{Name='Name'; Expr={$_.Name}},
@{Name='Label'; Expr={$_.VolumeLabel}},
@{Name='Size(GB)'; Expr={[int32]($_.TotalSize / 1GB)}},
@{Name='Free(GB)'; Expr={[int32]($_.AvailableFreeSpace / 1GB)}},
@{Name='Free(%)'; Expr={[math]::Round($_.AvailableFreeSpace / $_.TotalSize,2)*100}},
@{Name='Format'; Expr={$_.DriveFormat}},
@{Name='Type'; Expr={[string]$_.DriveType}}
} | Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName
} catch [System.Management.Automation.RuntimeException] {
$myError = @{
Message = $_.Exception.Message
Type = $_.FullyQualifiedErrorID
}
$out = @{ Error = $myError }
}
ConvertTo-Json $out -Compress