1

我是 Powershell 的新手,我想为 $computerName 中的所有计算机运行我的自定义对象 $info 中的所有内容,以便获取它们的信息。在这段代码中,我将它们命名为 A、B、C 以供一般参考。我尝试实现一个 foreach 循环来写入 $info 的主机,以便它完成任务,但它不起作用。我也尝试只为 CustomObjects 运行 $info.OSInfo,但是当我在 VS Code 中运行文件时没有任何反应。感谢您的帮助。

Function Get-SystemInfo {

    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $computerName
    )
    $compSysProps = @{ Property = @('Name','Model','NumberOfProcessors','Manufacturer','Domain','NumberOfLogicalProcessors','TotalPhysicalMemory'); }
    $OsInfoProps = @{ Property = @('Property', 'InstallDate', 'Caption', 'Version');}
    $BiosProps = @{ Property = @('SerialNumber'); }
    $ProcessorProps = @{ Property = @('Name', 'NumberofCores','NumberofLogicalProcessors', 'SocketDesignation');}
    $DiskProps = @{ Property = @('Caption', 'Size', 'FreeSpace'); }


    $computerName = @('A','B','C')

    foreach ($computer in $computerName)
    {
        Write-Host $info
    }


    $info = [PSCustomObject]@{
        OSInfo  = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computerName | Select-Object  @OsInfoProps
        BIOSInfo = Get-CimInstance -ClassName Win32_bios -ComputerName $computerName | Select-Object @BiosProps
        ComputerSystemInfo = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computerName | Select-Object @compSysProps
        ProcessorInfo = Get-CimInstance -Class Win32_processor -ComputerName $computerName| Select-Object @ProcessorProps
        DiskInfo = Get-CimInstance -Class Win32_LogicalDisk -ComputerName $computerName | Select-Object @DiskProps
    }
}
4

1 回答 1

1

您在设置值之前调用打印$info,这意味着它将不打印任何内容。还有一个问题是它$computerName不是$computer在获取信息时。StackOverflow 中的重构代码如下:

Function Get-SystemInfo {

    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $computerName
    )
    $compSysProps = @{ Property = @('Name','Model','NumberOfProcessors','Manufacturer','Domain','NumberOfLogicalProcessors','TotalPhysicalMemory'); }
    $OsInfoProps = @{ Property = @('Property', 'InstallDate', 'Caption', 'Version');}
    $BiosProps = @{ Property = @('SerialNumber'); }
    $ProcessorProps = @{ Property = @('Name', 'NumberofCores','NumberofLogicalProcessors', 'SocketDesignation');}
    $DiskProps = @{ Property = @('Caption', 'Size', 'FreeSpace'); }


    $computerName = @('A','B','C')

    foreach ($computer in $computerName)
    {
        $info = [PSCustomObject]@{
            OSInfo  = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer | Select-Object  @OsInfoProps
            BIOSInfo = Get-CimInstance -ClassName Win32_bios -ComputerName $computer | Select-Object @BiosProps
            ComputerSystemInfo = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object @compSysProps
            ProcessorInfo = Get-CimInstance -Class Win32_processor -ComputerName $computer | Select-Object @ProcessorProps
            DiskInfo = Get-CimInstance -Class Win32_LogicalDisk -ComputerName $computer | Select-Object @DiskProps
        }
        Write-Host $info
    }  
}
于 2020-05-31T03:33:01.287 回答