Question

I'm trying to use Powershell to get SCSI hardware from several virtual servers and get the operating system of each specific server. I've managed to get the specific SCSI hardware that I want to find with my code, however I'm unable to figure out how to properly get the operating system of each of the servers. Also, I'm trying to send all the data that I find into a csv log file, however I'm unsure of how you can make a powershell script create multiple columns.

Here is my code (almost works but something's wrong):

$log = "C:\Users\me\Documents\Scripts\ScsiLog.csv"
Get-VM | Foreach-Object {
    $vm = $_
        Get-ScsiController -VM $vm | Where-Object { $_.Type -eq "VirtualBusLogic" } | Foreach-Object {
            get-VMGuest -VM $vm } | Foreach-Object{
        Write-output $vm.Guest.VmName  >> $log
            }
    }

I don't receive any errors when I run this code however whenever I run it I'm only getting the name of the servers and not the OS. Also I'm not sure what I need to do to make the OS appear in a different column from the name of the server in the csv log that I'm creating.

What do I need to change in my code to get the OS version of each virtual machine and output it in a different column in my csv log file?

Was it helpful?

Solution

get-vmguest returns a VMGuest object. Documented here: http://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/VMGuest.html. The documentation is sparse, but I would guess the OSFullName field would give you the OS version. So you could change the write-output line to

add-content $log "$($vm.guest.vmname) , $($vmguest.guest.OSFullName)"

and you'd be on the right track. The comma in the output is what makes the output "comma separated values". A CSV file can optionally have a header. (See Import-CSV / Export-CSV help for details). So you might want to add the following as the second line of your script:

add-content $log "VM Name, OS Name"  # add CSV header line
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top