// Query
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName "RemoteComputerName or IP_PC"
Specify Parameters piping to Select-Object to identify more objects
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName "RemoteComputerName or IP_PC" | Select-Object DeviceID, DriveType, Size, FreeSpace
Script to Automate the Remote Query and accepts a Computer name and prompts for a PC name if one is not provided
function Get_info {
param(
[parameter(Mandatory, Position=0)]
[string[]]$getPC=(Read-Host "Enter PC name: ")
)
if (-not $getPC) {
Write-Host "Please provide a PC or IP address to search."
} else {
# Proceed with your search logic here
foreach ($computerName in $getPC) {
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName
}
}
}
# Call the function with one or more PC or IP addresses as arguments
Get_info -getPC LON-DC1
This script will accept a switch parameter to indicate whether alternate credentials are required.
<#.
.SYNOPSIS
This function is to assist the user to query a remote computer disk size
.DESCRIPTION
Use this function to obtain the Disk Size of a computer:
Example:
To query remote PC:
Get_info -getPC "PC Name"
To query with a remote credential:
Get_info -getPC "PC Name" -UseAlternateCredentials
.PARAMTER
getPC - this function is the first arguement that takes a PC_name or IP as a param
AlternateCredentials - This arguement will make the function request for a user credential
#>
function Get_info {
param(
[parameter(Mandatory, Position=0)]
[string[]]$getPC=(Read-Host "Enter PC name: "),
[switch]$UseAlternateCredentials
)
if ($AlternateCredentials -eq $true) {
$cred = Get-Credential
$session = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName -Credential $cred
Get-CimInstance -ClassName Win32_LogicalDisk -CimSession $session
} else {
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName
}
# Call the function with one or more PC or IP addresses as arguments
Logging Function
This script will accept parameters for the file name for the log file, accepts parameter for the folder containing the log file, accepts parameter for the data being written to the log file, Adds a time stamp to the data being written to the log file, & lastly appends each log entry to an existing file.
function wLog {
param(
[string]$folder,
[string]$file,
[string]$info
)
#finding the full path
$logP = Join-Path -Path $folder -ChildPath $file
#Calc time stamp for log entry
$date = Get-Date
$timeStmp = $date.ToShortDateString() + " " + $date.ToLongTimeString() + ":"
#Redirect info to log file
$timeStmp + " " + $info | Out-File -FilePath $logP -Append
}
# Call the function with named parameters
wLog -folder "E:\Mod09" -file "testL.txt" -info "Test Log"
Creating an Error Handler on the Script
This function will create an error friendly message
function wLog {
param(
[string]$folder,
[string]$file,
[string]$info
)
try {
# Finding the full path
$logP = Join-Path -Path $folder -ChildPath $file
# Calculate timestamp for log entry
$date = Get-Date
$timeStmp = $date.ToShortDateString() + " " + $date.ToLongTimeString() + ":"
# Redirect info to log file
$logEntry = $timeStmp + " " + $info
$logEntry | Out-File -FilePath $logP -Append
}
catch {
# Handle any errors that occur during execution
Write-Host "An error occurred: $($_.Exception.Message)"
}
}
# Call the function with named parameters
wLog -folder "E:\Mod09" -file "testL.txt" -info "Test Log"