Public/ps1/Export-VMNotes.ps1
<#
.Synopsis Exports a list of all VMs with their notes to a CSV file using ';' as a delimiter. .Description This function retrieves all virtual machines from a connected VMware vSphere environment and exports details of these VMs, focusing on their notes, to a specified CSV file. The details exported include VM name, host, power state, and notes, formatted as a single string. .Parameter OutputPath The file path where the CSV file will be saved. Default is 'C:\VMsNotes.csv'. .Example Export-VMNotes -OutputPath "D:\Exports\VMsNotes.csv" This command exports the notes and basic details of all VMs to a CSV file located at 'D:\Exports\VMsNotes.csv', using ';' as the delimiter. .Notes Author: Benjamin Siegrist Version: 0.1.0 Requires that you are connected to a vCenter server with appropriate permissions to retrieve VM information. #> function Export-VMNotes { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$OutputPath ) try { # Ensure the VMware PowerCLI module is loaded if (-not (Get-Module -Name VMware.PowerCLI)) { Import-Module VMware.PowerCLI } # Retrieve all VMs $vms = Get-VM # Check if VMs are retrieved successfully if (-not $vms) { throw "No VMs could be retrieved. Check your connection to vCenter." } # Export the data to a CSV file with ';' as the delimiter $vms | ForEach-Object { # Ensure notes are a single string with no new lines or extra spaces $notes = ($_.Notes -join "; ").Trim() [PSCustomObject]@{ Name = $_.Name VMHost = $_.VMHost PowerState = $_.PowerState Notes = $notes } } | Export-Csv -Path $OutputPath -NoTypeInformation -Delimiter ';' Write-Output "Export completed successfully. File saved at $OutputPath" } catch { Write-Error "An error occurred: $_" } } |