VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Formatting Output from PowerCLI in a file

I'm looking for a script to get a bunch of data on VM's, then write it to a file, and format it in the file with specified delimiters in between the fields - so that in the file it would look like this (if commas are the delimiter):

<> NumberofvCPU's, RAMinGB, HDSizeinGB, OperatingSystem, HostName, IPAddress, MacAddress <>

Reply
0 Kudos
1 Solution

Accepted Solutions
ThompsG
Virtuoso
Virtuoso
Jump to solution

Hi,

Apologies if you where just looking for the command to output in CSV format otherwise read on.

$vcserver = "vc-servername"
$outputfile = "C:\VM-info.csv"

Connect-VIServer -Server $vcserver

$report = @()

Get-VM | foreach { 
    $vmv = Get-View $_.ID
    
    $row = " " | Select VMname, NumberofvCPU, RAMinGB, HDSizeGB, OperatingSystem, Hostname, IPAddress, MacAddress
    
    $row.VMname = $_.Name
    $row.NumberofvCPU = $_.NumCpu

    # Gets the guest memory size and then converts to GB
    $row.RAMinGB = ($_.MemoryMB)/1Kb

    # Gets the total size of all the harddisks connected to the guest and then converts this to GB
    $row.HDSizeGB = (($_.HardDisks | Measure-object -property CapacityKB -sum).Sum)/1Mb

    $row.OperatingSystem = $vmv.Guest.GuestFullName
    $row.Hostname = $_.Host

    # We are assuming here that there is a maximum of 1 network adapter in the guest.
    $row.IPAddress = $vmv.Guest.Net[0].IpAddress
    $row.MacAddress = $vmv.Guest.Net[0].MacAddress
    
    $report += $row 
}

# Outputing the report in a CSV format, removing TypeInformation from the top
$report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation

Disconnect-VIServer -confirm:$false

Modify the variables vcserver and outputfile for your environment and this should do the trick or at least get you started.

Kind regards.

View solution in original post

Reply
0 Kudos
5 Replies
WessexFan
Hot Shot
Hot Shot
Jump to solution

add 'output nameofyourfile.csv' to the end of your script

vghetto has a large script repository (William Lam)

http://communities.vmware.com/docs/DOC-9852

VCP5-DCV, CCNA Data Center
LucD
Leadership
Leadership
Jump to solution

You can try the Export-Csv cmdlet.

Collect all your data in an array and the you can do

$array | Export-Csv "C:\myfile.csv" -NoTypeInformation

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership
Jump to solution

William has indeed a most impressive repository of scripts.

But I'm afraid most, if not all, are written in Perl Smiley Wink

Now if you could convince William to start converting...

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
WessexFan
Hot Shot
Hot Shot
Jump to solution

He's the reason I'm using vMA haha! Its good stuff..

VCP5-DCV, CCNA Data Center
Reply
0 Kudos
ThompsG
Virtuoso
Virtuoso
Jump to solution

Hi,

Apologies if you where just looking for the command to output in CSV format otherwise read on.

$vcserver = "vc-servername"
$outputfile = "C:\VM-info.csv"

Connect-VIServer -Server $vcserver

$report = @()

Get-VM | foreach { 
    $vmv = Get-View $_.ID
    
    $row = " " | Select VMname, NumberofvCPU, RAMinGB, HDSizeGB, OperatingSystem, Hostname, IPAddress, MacAddress
    
    $row.VMname = $_.Name
    $row.NumberofvCPU = $_.NumCpu

    # Gets the guest memory size and then converts to GB
    $row.RAMinGB = ($_.MemoryMB)/1Kb

    # Gets the total size of all the harddisks connected to the guest and then converts this to GB
    $row.HDSizeGB = (($_.HardDisks | Measure-object -property CapacityKB -sum).Sum)/1Mb

    $row.OperatingSystem = $vmv.Guest.GuestFullName
    $row.Hostname = $_.Host

    # We are assuming here that there is a maximum of 1 network adapter in the guest.
    $row.IPAddress = $vmv.Guest.Net[0].IpAddress
    $row.MacAddress = $vmv.Guest.Net[0].MacAddress
    
    $report += $row 
}

# Outputing the report in a CSV format, removing TypeInformation from the top
$report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation

Disconnect-VIServer -confirm:$false

Modify the variables vcserver and outputfile for your environment and this should do the trick or at least get you started.

Kind regards.

Reply
0 Kudos