VMware Cloud Community
IRivera
Contributor
Contributor
Jump to solution

Help modifying a script

I found this script some where out there that lists datastores, name of vms in datastore and datastore's path. I want to also get the notes field in the report. Can any one help?

$report = @()

Get-VM | Get-View | % {

$row = "" | select Name, Datastore, Path

$row.Name = $_.Name

$row.Datastore = (Get-View $_.Datastore[0]).Summary.Name

$current = Get-View $_.Parent

$path = $_.Name

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

$report += $row

}

$report

thanks

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sorry, must have misread your question.

The Notes field makes it even a bit simpler.

Get-VM | % {
	$vmImpl = $_
	$_ | Get-View | % {
		$current = Get-View $_.Parent
		$path = $_.Name
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$row = "" | select Name, Datastore, Path, Note
		$row.Name = $_.Name
		$row.Datastore = (Get-View $_.Datastore[0]).Summary.Name
		$row.Path = $path
		$row.Note = $vmImpl.Description
		$report += $row
	}
}
$report 


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VM | % {
	$vmImpl = $_
	$_ | Get-View | % {
		$current = Get-View $_.Parent
		$path = $_.Name
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$VMnote = ""
		for($i = 0; $i -lt $vmImpl.CustomFields.Keys.Count; $i ++ ){
			$VMnote += ($vmImpl.CustomFields.Keys[$i] + "=" + $vmImpl.CustomFields.Values[$i] + "  ")
		}
		$row = "" | select Name, Datastore, Path, Note
		$row.Name = $_.Name
		$row.Datastore = (Get-View $_.Datastore[0]).Summary.Name
		$row.Path = $path
		$row.Note = $VMnote
		$report += $row
	}
}
$report 

All custom attributes are appended to one string.


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

0 Kudos
IRivera
Contributor
Contributor
Jump to solution

the script works fine, but I'm looking to add the string value in the notes field. Report's output is of the VMs custom attributes.

See attachment for more information.

LUCD, thanks for you quick response and help on this.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, must have misread your question.

The Notes field makes it even a bit simpler.

Get-VM | % {
	$vmImpl = $_
	$_ | Get-View | % {
		$current = Get-View $_.Parent
		$path = $_.Name
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$row = "" | select Name, Datastore, Path, Note
		$row.Name = $_.Name
		$row.Datastore = (Get-View $_.Datastore[0]).Summary.Name
		$row.Path = $path
		$row.Note = $vmImpl.Description
		$report += $row
	}
}
$report 


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

0 Kudos