PowerShell Scripting For MOCK

Advance Scripting for PowerShell MOCK

Remote Query Hardware Information

// 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.

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.

Creating an Error Handler on the Script

This function will create an error friendly message

Reference:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3

Last updated