VMware

This Question is Answered

1 "helpful" answer available (6 pts)
4 Replies Last post: Nov 12, 2009 4:09 PM by rg01  

help enhancing existing script posted: Nov 6, 2009 9:09 AM

Click to view rg01's profile Novice 4 posts since
Oct 24, 2007

So i initially tried getting this to loop through all the folders but couldn't figure it out so i made it so i could pass the folder as a parameter but ideally i want it to do all VMs without having to enter any parameters. Any ideas on how to best do this? I want to make csv files for each folder with all the custom attributes for each VM contained within.

##Variables
Param($Param1)
$report = @()
$folders = get-folder $Param1 # | sort name

############################################################################################

###########################

ForEach ($folder in $folders)
{
$vms = get-vm -location $folder
ForEach ($vm in $vms)
{
$ReportObj = "" | Select "Folder", "Virtual Machine", "POC", "Username", "Password",

$ReportObj."Folder" = $folder.name
$ReportObj."Virtual Machine" = $vm.name
$ReportObj."POC" = $vm.CustomFields
$ReportObj."Username" = $vm.CustomFields
$ReportObj."Password" = $vm.CustomFields
$Report += $ReportObj
$csvfile = "D:\"$folder".csv"
$report | Export-CSV $csvfile -NoTypeInformation

}
}

Re: help enhancing existing script

1. Nov 6, 2009 12:40 PM in response to: rg01
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
Have a look at this variant on your idea.
It lists the complete path and creates a separate CSV file per path found
filter Get-FolderPath {
	$_ | Get-View | % {
		$row = "" | select Name, Path
		$row.Name = $_.Name

		$current = Get-View $_.Parent
		$path = ""
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$row.Path = $path
		$row
	}
}

Get-VM | Get-FolderPath | Group-Object -Property Path | %{$_.Group | Export-Csv ("C:\" + $_.Name.Replace("\","-") + ".csv")}

Re: help enhancing existing script

3. Nov 12, 2009 2:46 PM in response to: rg01
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
The Get-FolderPath filter returns everything you put in the $row.
So if you add additional properties they will be in the resulting CSV files.

For example, the following version will add the Notes property and all the Custom Attributes
filter Get-FolderPath {
	$vmImpl = $_
	$_ | Get-View | % {
		$row = "" | select Name, Path, Notes
		$row.Name = $_.Name

		$current = Get-View $_.Parent
		$path = ""
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$row.Path = $path
		$row.Notes = $vmImpl.Description
		$vmImpl.CustomFields.GetEnumerator() | %{
			$row | Add-Member -Name $_.Key -Value $_.Value -MemberType NoteProperty 
		}
		$row
	}
}

Get-VM | Get-FolderPath | Group-Object -Property Path | %{$_.Group | Export-Csv ("C:\" + ($_.Name.TrimEnd("\")).Replace("\","-") + ".csv")}

This version also solves the problem with the filename of the CSV files that had a trailing "-".

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities