系統管理有時感覺像一次性處理太多事情。一分鐘,您正在解決存儲問題;下一分鐘,您正在查找處理器規格或檢查內存容量。手動進行這些操作不僅繁瑣,而且容易出錯。如果您陷入這種循環中,是時候停止這種瘋狂了。自動化是您的答案!
在本指南中,您將學習如何構建 PowerShell 函數,以便優化工作流程,專注於真正重要的事情。
在您的工具箱中擁有一組強大的腳本,可以為您節省時間,幫助您像專家一樣運作!
構建提取信息的函數
隨著模塊的構建,我們將使用 Get-CimInstance
命令填充 Info
屬性,以硬體信息。這種方法可以有效地標準化並重複使用代碼。
以下函數從遠程會話中收集內存、存儲和處理器信息:
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 }
轉換輸出
從系統查詢中檢索的原始數據通常包含比必要的更多細節,這使得解釋起來很繁瑣。這種情況可能導致在專注於特定、可操作的屬性時效率低下。
為了標準化數據,您可以將從 Get-CimInstance
獲取的輸出轉換為自定義對象。
這是更新後的版本:
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 }
處理字節值
硬件信息通常包括以字节为单位的存储器或存储容量等数字数值。尽管从技术上讲是准确的,但这些数值可能更适合快速解释。将它们转换为千兆字节提供了对硬件指标更直观的理解。
为了提高可读性,辅助函数ConvertTo-Gb
使这个过程更高效:
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 }
这种方法改善了这些函数返回的数据的功能性和可读性。通过将重复逻辑集中到像ConvertTo-Gb
这样的辅助函数中,未来的更新或更改变得更易管理。
结论
您已经学会了如何使用本指南中概述的步骤创建PowerShell函数。无论是从远程系统收集硬件信息,将原始输出转换为结构化的自定义对象,还是使数据更易读。
既然您已经掌握了这些技术,考虑一下您如何可以在此基础上扩展。例如,您可以向您的模块添加更多的硬件类别,如网络适配器或GPU信息。您还可以将这些函数集成到更大的自动化工作流程中,如系统健康监控或库存管理。
可能性是无限的,您正在建立一个强大的工具集,自信地管理您的系统。
Source:
https://adamtheautomator.com/powershell-functions-module/