1

I have a simple PS script to query for Volume sizes and usage:

<#
.SYNOPSIS
This is a simple Powershell script to display Free space on a remote machine (in GB and percentage).

.DESCRIPTION
The script uses WMI. It excludes Zero size volumes (usually CD/DVD drives). 
Parameters: Computername (remote computer FQDN).
The result is sorted by % free space

.EXAMPLE
Get-VolumeFreeSpace -ComputerName dc.contoso.local -Creds contoso\superadmin
Result:

Device Size (GBs) Free (GBs) Free %
------ ---------- ---------- ------
C:             70         50     71
D:             50         49     98

.NOTES

#>

Function Get-VolumeFreeSpace {

Param(
  [string]$Computername

  )

  Get-WmiObject -Class Win32_LogicalDisk -Namespace "root\cimv2" -ComputerName $Computername | where {$_.Size -gt 0} | 

Select @{Name="Device";E={$_.DeviceID.Trim()}}, 
@{Name="Size (GBs)";E={[math]::Round($_.Size / 1GB)}},
@{Name="Free (GBs)";E={[math]::Round($_.FreeSpace / 1GB)}},
@{Name="Free %";E={[math]::Round(($_.FreeSpace*100)/($_.Size))}} | Sort-Object "Free %" | ft -autosize

}

I had originally a $Creds Parameter to pass GWMI credentials in case of the active user not being and administrator of the servers to be queried BUT assuming this user has an admin account he/she can use to query servers.

The problem here is that if I add the $Creds parameter, it will trigger nomatter if the credentials are needed or not.

What I want:

To check if the user executing the function is, or is not, and administrative user. They all begin with certain specific letters: admXXXX, admYYYY, etc. So the idea is to make a string validation of the executing user and if its name begins with "adm" proceed with the WMI query, and if it doesn't, ask for the Credentials. Basically, making the $Creds parameter conditional.

Thanks!

1 Answer 1

2

The way I would typically handle this is with an optional parameter to your function, and splatting.

Function Get-VolumeFreeSpace {

  Param(
      [string]$Computername,
      [PSCredential]$Credential
  )

  $params = @{
      Class = 'Win32_LogicalDisk'
      Namespace = 'root\cimv2'
      ComputerName = $ComputerName
  }
  if ($Credential) {
      $params.Credential = $Credential
  }

  Get-WmiObject @params | where {$_.Size -gt 0} | 

  Select @{Name="Device";E={$_.DeviceID.Trim()}}, 
  @{Name="Size (GBs)";E={[math]::Round($_.Size / 1GB)}},
  @{Name="Free (GBs)";E={[math]::Round($_.FreeSpace / 1GB)}},
  @{Name="Free %";E={[math]::Round(($_.FreeSpace*100)/($_.Size))}} | Sort-Object "Free %" | ft -autosize

}

Note that you also don't have to splat every parameter, you can mix it up:

$params = @{
    Class = 'Win32_LogicalDisk'
    Namespace = 'root\cimv2'
}
if ($Credential) {
    $params.Credential = $Credential
}

Get-WmiObject @params -ComputerName $Computer
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.