Doc
Doc

Reputation: 11

Getting remote VMs configuration for Inventory

I need to provide an inventory of VM server configs with OS/Disk Status/Excel Version and a specific, priority file version. PowerShell / VBScript would be nice. I'm having trouble with remote sys env variables and pulling the correct values for display.

$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

Upvotes: 1

Views: 88

Answers (1)

brendan62269
brendan62269

Reputation: 1096

You are killing puppies! Please stop using Write-Host! This was quite a buzz a few years back, but the temptation stays current and is still real. To your question, your [string] concatenations will work better if you use $(substring):

you have:

    "Computer Name: " + $computerSystem.Name

try:

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

The real issue is that you should try to output an object (of some sort). Check out this article by Don Jones. Returning an object means that you will have a consistent data set that can easily be searched, sorted, reported on, reformatted and converted to html. Your report has a sub-report for disks, you have to be creative with that. Anyway...

If you were to decide against the puppy slaughter, you could use a [PSCustomObject]. This will allow you to use the Export-Csv cmdlet and create much nicer reports on the fly. I am not going to rewrite your code, but here's a sample of how it works.

    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>

Upvotes: 1

Related Questions