创建现实世界模块:创建函数

管理系统可能让人感觉像是在同时 juggling 太多球。你可能在解决存储问题,接着又在寻找处理器规格或检查内存容量。手动执行这些操作不仅繁琐而且容易出错。如果你陷入这种循环,是时候停止这种疯狂了。自动化是你的答案!

在本指南中,你将学习如何构建 PowerShell 函数,以便简化工作流程,专注于真正重要的事情。

在你的工具包中享受一套强大的脚本,节省你的时间,帮助你像专业人士一样运作!

构建提取信息的函数

在我们构建模块时,我们将使用 Get-CimInstance cmdlet 填充 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/