Managing systems can feel like juggling too many balls at once. One minute, you’re troubleshooting a storage issue; the next, you’re hunting down processor specs or checking memory capacity. Doing so manually is not only tedious but prone to mistakes. If you’re stuck in this cycle, it’s time to stop the madness. Automation is your answer!
In this guide, you’ll learn how to build PowerShell functions so you can streamline your workflows and focus on what really matters.
Enjoy a set of powerful scripts in your toolkit, saving you time and helping you operate like a pro!
Building Functions for Extracting Information
As we build on the module, we’ll populate the Info
property with hardware information using the Get-CimInstance
cmdlet. This approach allows us to standardize and reuse code effectively.
The following functions gather memory, storage, and processor information from a remote session:
function Get-MemoryInfo { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Management.Automation.Runspaces.PSSession]$Session ) $outObject = @{ 'ComputerName' = $Session.ComputerName 'HardwareCategory' = 'Memory' 'Info' = $null } $scriptBlock = { Get-CimInstance -ClassName Win32_PhysicalMemory } $result = Invoke-Command -Session $Session -ScriptBlock $scriptBlock $outObject['Info'] = $result [pscustomobject]$outObject } function Get-StorageInfo { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Management.Automation.Runspaces.PSSession]$Session ) $outObject = @{ 'ComputerName' = $Session.ComputerName 'HardwareCategory' = 'Storage' 'Info' = $null } $scriptBlock = { Get-CimInstance -ClassName Win32_LogicalDisk } $result = Invoke-Command -Session $Session -ScriptBlock $scriptBlock $outObject['Info'] = $result [pscustomobject]$outObject } function Get-ProcessorInfo { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Management.Automation.Runspaces.PSSession]$Session ) $outObject = @{ 'ComputerName' = $Session.ComputerName 'HardwareCategory' = 'Processor' 'Info' = $null } $scriptBlock = { Get-CimInstance -ClassName Win32_LogicalDisk } $result = Invoke-Command -Session $Session -ScriptBlock $scriptBlock $outObject['Info'] = $result [pscustomobject]$outObject }
Transforming Output
The raw data retrieved from system queries often includes more details than necessary, making it cumbersome to interpret. This situation can lead to inefficiencies when focusing on specific, actionable attributes.
To standardize the data, you can transform the output from Get-CimInstance
into a custom object.
Here’s an updated version:
function Get-MemoryInfo { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Management.Automation.Runspaces.PSSession]$Session ) $outObject = @{ 'ComputerName' = $Session.ComputerName 'HardwareCategory' = 'Memory' 'Info' = $null } $scriptBlock = { Get-CimInstance -ClassName Win32_PhysicalMemory } $result = Invoke-Command -Session $Session -ScriptBlock $scriptBlock $info = $result | ForEach-Object { [pscustomobject]@{ 'LocatorId' = $_.DeviceLocator 'Capacity' = $_.Capacity } } $outObject['Info'] = $info [pscustomobject]$outObject } function Get-StorageInfo { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Management.Automation.Runspaces.PSSession]$Session ) $outObject = @{ 'ComputerName' = $Session.ComputerName 'HardwareCategory' = 'Storage' 'Info' = $null } $scriptBlock = { Get-CimInstance -ClassName Win32_LogicalDisk } $result = Invoke-Command -Session $Session -ScriptBlock $scriptBlock $info = $result | ForEach-Object { [pscustomobject]@{ 'VolumeName' = $_.VolumeName 'VolumeLetter' = $_.DeviceId 'VolumeCapacity' = $_.Size 'VolumeFreeSpace' = $_.FreeSpace } } $outObject['Info'] = $info [pscustomobject]$outObject }
Handling Byte Values
Hardware information often includes numerical values like memory or storage capacity in bytes. While technically accurate, these values could be more practical for quick interpretation. Converting them to gigabytes provides a more intuitive understanding of hardware metrics.
For better readability, the helper function ConvertTo-Gb
makes this process efficient:
function ConvertTo-Gb { [CmdletBinding()] param( [Parameter(Mandatory)] [string]$Bytes ) $numberGb = $Bytes / 1GB [math]::Round($numberGb, 2) } function Get-MemoryInfo { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Management.Automation.Runspaces.PSSession]$Session ) $outObject = @{ 'ComputerName' = $Session.ComputerName 'HardwareCategory' = 'Memory' 'Info' = $null } $scriptBlock = { Get-CimInstance -ClassName Win32_PhysicalMemory } $result = Invoke-Command -Session $Session -ScriptBlock $scriptBlock $info = $result | ForEach-Object { [pscustomobject]@{ 'LocatorId' = $_.DeviceLocator 'Capacity' = (ConvertTo-Gb -Bytes $_.Capacity) } } $outObject['Info'] = $info [pscustomobject]$outObject }
This approach improves the functionality and readability of the data returned by these functions. Future updates or changes become more manageable by centralizing repetitive logic into helper functions like ConvertTo-Gb
.
Conclusion
You’ve learned how to create PowerShell functions using the steps outlined in this guide. Be it gathering hardware information from remote systems, transforming raw outputs into structured custom objects, or making data more readable.
Now that you’ve mastered these techniques, consider how you can expand on this foundation. For instance, you could add more hardware categories to your module, such as network adapters or GPU information. You might also integrate these functions into larger automation workflows, like system health monitoring or inventory management.
The possibilities are endless, and you’re well on your way to building a robust toolset for confidently managing your systems.