VMware Cloud Community
rg01
Contributor
Contributor
Jump to solution

help enhancing existing script

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

}

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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 "-".


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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")}


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

rg01
Contributor
Contributor
Jump to solution

This is very helpful. I like how you broke this out but how do I get more information into the csv file like custom attributes, notes, etc.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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 "-".


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

Reply
0 Kudos
rg01
Contributor
Contributor
Jump to solution

That is perfect. Thanks!

Reply
0 Kudos