获取Inventory的远程VM配置

时间:2017-05-10 20:13:32

标签: powershell

我需要提供具有OS /磁盘状态/ Excel版本和特定优先级文件版本的VM服务器配置清单。 PowerShell / VBScript会很好。我在使用远程sys env变量时遇到了问题,并为显示提取了正确的值。

$ArrComputers = "TPWAPCSLABVM1.ips-sendero.com",

#Specify the list of PC names in the line above.
Clear-Host
function Get-ComputerInfo {
    foreach ($Computer in $ArrComputers) {
        if (Test-Connection -ComputerName $Computer -Quiet -Count 1) {
            $Date = Get-Date
            $computerSystem = Get-WmiObjectWin32_ComputerSystem -Computer $Computer
            $computerBIOS = Get-WmiObjectWin32_BIOS -Computer $Computer
            $computerOS = Get-WmiObjectWin32_OperatingSystem -Computer $Computer
            $computerCPU = Get-WmiObjectWin32_Processor -Computer $Computer
            $DiskReport = Get-WmiObjectwin32_logicaldisk -Computer $Computer -Filter "Drivetype=3" -ErrorAction SilentlyContinue
            $Office_Excel_Version = Get-WmiObjectwin32_product -Computername $Computer -Filter "Name LIKE '%Excel%'"
            $EnvObj = @(Get-WmiObject-Class Win32_Environment -ComputerName $Computer | Where-Object {$_.Name -eq "SVDIR"})        
            $Risk_Location = "$EnvObj\SV\ALM\APP\SVAL.exe"
            $Risk_Version = (Get-Command $Risk_Location).FileVersionInfo.FileVersion
            $Ora_Location = "$Base\TEMP_IPS\Ora12c_install.rsp"
            $Oracle_Install_type = Select-String -Pattern "oracle.install.db.InstallEdition" -Path $Ora_Location
            Write-Host "Gathering System Information for: " $computerSystem.Name -BackgroundColor DarkCyan

            "Computer Name: " + $computerSystem.Name
            "Manufacturer: " + $computerSystem.Manufacturer
            "Model: " + $computerSystem.Model
            "Serial Number: " + $computerBIOS.SerialNumber
            "CPU: " + $computerCPU.Name
            "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
            "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
            "Office Version: " + $Office_Excel_Version
            "Risk Version: " + $Risk_Version
            "Oracle_Install Type: " +$Oracle_Install_type
            #"User logged In: " + $computerSystem.UserName
            "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
            $DiskReport |
                Select-Object @{Label = "Drive Letter";Expression = {$_.DeviceID}},
                    @{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
                    @{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb )}},
                    @{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.Freespace / $_.Size)}}
            #@{Label = "Server Name";Expression = {$_.SystemName}},
            ""
            "Machine stats collected on : ---------> $date <---------"
            "_____________________________________________________________________________________________________"
            ""
        } else {
            $OfflineSystem = New-Object -TypeName System.Object | Select-Object -Property ComputerName
            $OfflineSystem.ComputerName = $Computer
            [array] $arrOfflineSystem += $OfflineSystem     
        }
        #$arrOfflineSystem | Where-Object {$_} > D:\Scripts\PowerShell\Results1.csv
    }
}

#Entry point to script  
Get-ComputerInfo > D:\Scripts\PowerShell\Results.csv

1 个答案:

答案 0 :(得分:1)

你是killing puppies!请停止使用Write-Host!几年前这是一个非常流行的嗡嗡声,但诱惑仍然是最新的,仍然是真实的。对于您的问题,如果您使用$(substring):

,您的[string]连接将更好地工作 你有:

    "Computer Name: " + $computerSystem.Name

尝试:

    "Computer Name: " + $(computerSystem.Name)

真正的问题是你应该尝试输出object(某种)。查看this article by Don Jones。返回对象意味着您将拥有一致的数据集,可以轻松搜索,排序,报告,重新格式化并转换为html。您的报告有一个磁盘子报告,您必须具有创造性。总之...

如果你决定反对小狗屠宰,你可以使用[PSCustomObject]。这将允许您使用Export-Csv cmdlet并动态创建更好的报告。我不打算重写你的代码,但这里有一个如何工作的样本。

    PS C:\pwrshl> function get-compInfo {
    >>     param ( $Computer )
    >>     $computerSystem = Get-WmiObject Win32_ComputerSystem -Computer $Computer
    >>     $computerBIOS = Get-WmiObject Win32_BIOS -Computer $Computer
    >>     [pscustomobject]@{
    >>         'Computer Name' = $computerSystem.Name
    >>         'Serial Number' = $computerBIOS.SerialNumber
    >>     }
    >> }
    PS C:\pwrshl>
    PS C:\pwrshl> get-compInfo localhost

    Computer Name   Serial Number
    -------------   -------------
    DESKTOP-W0FDZ1  F44FTY64


    PS C:\pwrshl> get-compInfo localhost | export-csv .\csv3.csv -NoTypeInformation
    PS C:\pwrshl> cat .\csv3.csv
    "Computer Name","Serial Number"
    "DESKTOP-W0FDZ1","F44FTY64"
    PS C:\pwrshl>
相关问题