0

当我运行下面的代码时,我收到一个型号错误,其中没有显示标题,还有一件事是我无法按顺序获得所有三个。

$arrComputers = get-Content -Path "C:\Desktop\Computers.txt"
    $e=$arrComputers | ForEach-Object {Get-WMIObject -Class Win32_BIOS -ComputerName $_ } |Select PSComputerName, Version,Manufacturer,Status,BIOSVersion,SerialNumber |ConvertTo-Html -fragment
     $e2=$arrComputers |ForEach-Object { get-wmiobject -class Win32_logicaldisk -Filter "DeviceID = 'C:'" -ComputerName $_ } | select freeSpace,size | ConvertTo-Html -fragment
      $e3=$arrComputers |ForEach-Object { get-wmiobject -class "Win32_ComputerSystem" -ComputerName $_ } | select Model| ConvertTo-Html -fragment

     ConvertTo-HTML -Body "$e $e2 $e3" -Title "List of Computers" |  Out-File C:\Users\Desktop\gf.html
4

1 回答 1

1

将所有 WMI 调用放入一个对象中要容易得多。处理格式要容易得多。我想我得到了你想要的一切:

function GetCompInfoWork 
{
param (
    [string]$computername,[string]$logfile
)
    $pc = Get-WmiObject Win32_ComputerSystem -ComputerName $computername

    $bios = Get-WmiObject win32_bios -ComputerName $computername

    $disk = Get-WmiObject win32_logicalDisk -Filter "DeviceID= 'C:'" `
    -computername $computername

    $obj = New-Object -TypeName PSObject

    $obj | Add-Member -MemberType NoteProperty `
        -Name PSCompName -Value ($bios.PSComputerName)

    $obj | Add-Member -MemberType NoteProperty `
        -Name Version -Value ($bios.version)    

    $obj | Add-Member -MemberType NoteProperty `
         -Name Manufacturer -Value ($bios.manufacturer)

    $obj | Add-Member -MemberType NoteProperty `
        -Name Status -Value ($bois.status)

    $obj | Add-Member -MemberType NoteProperty `
        -Name SerialNumber -Value ($bios.serialnumber)

    $obj | Add-Member -MemberType NoteProperty `
        -Name DiskSize -Value ($disk.size / 1GB -as [int])

    $obj | Add-Member -MemberType NoteProperty `
        -Name SysDriveFree -Value ($disk.freespace / 1GB -as [int])

    $obj | Add-Member -MemberType NoteProperty `
        -Name ComputerName -Value ($pc.model)

    Write-Output $obj

}

function Get-CompInfo 
{
param ([string[]]$computername,[string]$logfile )

BEGIN
{
    $usedParamater = $False
    if ($PSBoundParameters.ContainsKey('computername')) {
        $usedParamater = $True
        }
}
PROCESS {
    if ($usedParamater) 
    {
        foreach ($computer in $computername)
        {
            Getcompinfowork -computername $computer `
            -logfile $logfile
        }
    }  

    else 
    {
        Getcompinfowork -computername $_ `
        -logfile $logfile
    }

}
END {}
}


Get-Content C:\Users\Kev\Desktop\computers.txt| Get-CompInfo | ConvertTo-Html | Out-File C:\Users\kev\Desktop\Output.html
于 2013-07-17T20:51:41.820 回答