如何在Powershell中获取名称和顺序

时间:2013-07-17 19:51:18

标签: powershell powershell-remoting powershell-ise

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

$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

1 个答案:

答案 0 :(得分: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