VMware Cloud Community
jingleharish
Contributor
Contributor

VM Notes

Hello,

We have the below entities in the notes section;

Provision Date:
App Name:

App Owner:

SR No:

I am looking for a script which will split the notes data into seperate fields in the csv file. For example;

VMName | Provision Date | App Name | App Owner | SR No

Your help will be much appreciated.

Thanks!!!

0 Kudos
5 Replies
LucD
Leadership
Leadership

Is each of these entries on a seperate line in the Notes field ?


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

0 Kudos
jingleharish
Contributor
Contributor

Both the below types are there;

>> Single Line

>> Seperate Line

0 Kudos
RvdNieuwendijk
Leadership
Leadership

The next solution uses a regular expression to select the data from the notes field. It works if all the data is on a single line and also if every field is on a seperate line.

$regex = "Provision Date:(?<ProvisionDate>[^$\r\n]*)([$\r\n]*)App Name:(?<AppName>[^$\r\n]*)([$\r\n]*)App Owner:(?<AppOwner>[^$\r\n]*)([$\r\n]*)SR No:(?<SRNo>[^$]*)"
Get-VM | ForEach-Object {
  if ($_.Notes -Match $regex) {
    $Report = ""| Select-Object -Property "VM Name","Provision Date","App Name","App Owner","SR No"
    $Report."VM Name"        = $_.Name
    $Report."Provision Date" = $Matches["ProvisionDate"].Trim(" ")
    $Report."App Name"       = $Matches["AppName"].Trim(" ")
    $Report."App Owner"      = $Matches["AppOwner"].Trim(" ")
    $Report."SR No"          = $Matches["SRNo"].Trim(" ")
    $Report
  }
  else {
    $Report = ""| Select-Object -Property "VM Name","Provision Date","App Name","App Owner","SR No"
    $Report."VM Name"        = $_.Name
    $Report
  }
} | Export-CSV -Path VmInfo.csv -NoTypeInformation -UseCulture


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

This one will work without the LF in the last field

$mask = "Provision Date:\s+(?<date>.+)\s+App Name:\s+(?<appname>.+)\s+App Owner:\s+(?<appowner>.+)\s+SR No:\s+(?<srno>.+)\s+" 
$report = Get-VM | %{     $result = Select-String $mask -InputObject $vm.Notes     New-Object PSObject -Property @{         VMname = $_.Name         "Provision date" = $result.Matches[0].Groups["date"]         "App Name" = $result.Matches[0].Groups["appname"]         "App Owner" = $result.Matches[0].Groups["appowner"]         "SR NO" = $result.Matches[0].Groups["srno"]     } } $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

0 Kudos
david615
Enthusiast
Enthusiast

Hmm I was looking for a script that will fill in information on those fields and I ran in to this post.

http://powerclinic.blogspot.com/
0 Kudos