There is probably a cleaner way of doing this but I'm just starting with the Powershell scripting but I had a bizarre scenario where I knew the UUID but not the VM name so I wrote this script to list all the VM's in my environment with their UUID and output it to a text file which I could simply search:
-
#CREATE FUNCTIONS
function Find_UUID
{
foreach($vm in $vmlist)
{
echo ""
echo "UUID For $vm "
echo "UUID For $vm " >> $file3
get-vm $vm | %{(Get-View $_.Id).config.uuid}
get-vm $vm | %{(Get-View $_.Id).config.uuid} >> $file3
echo ""
echo "" >> $file3
}
}
#SET VARIABLES
$file1 = "E:\VMScripts\Variables\VMList.txt"
$file2 = "E:\VMScripts\Variables\VMList_Clean.txt"
$file3 = "E:\VMScripts\Variables\VMList_UUID.txt"
echo ""
echo "Finding UUID For VM Script..."
echo ""
echo "Producting List Of VM's..."
echo ""
get-vm | select name > $file1
echo "List Of VM's Produced"
(gc $file1) | foreach-object {$_ -replace " ", ""} | sc $file1
(gc $file2) | foreach-object {$_ -replace "Name", ""} | sc $file1
(gc $file2) | foreach-object {$_ -replace "----", ""} | sc $file1
cat $file1 | where {$_ -ne ""} > $file2
$vmlist = Get-Content $file2
echo ""
echo "Find UUID"
echo "Find UUID" > $file3
echo ""
Find_UUID
echo ""
echo "VM > UUID List Produced"
-