I wrote up a PowerShell snippet that connects to VMM 2012 R2, and searches through all VMs. What I'd like this script to do is match any VM's that have _Replica in their name, and rename them.
Right now, the script works, however it goes through the renaming process for EVERY VM. I'd only like it to perform this function on the VM's that have "_Replica" in them. For instance:
testing123_Replica needs to rename to testing123. VMtest123 should be ignored.
Experimenting with Write-Host - I only want it to affect VMs with _Replica in the name - I was under the impression that Match would display a match, vs replacing.
Thank you for help!
### This should rename the VM if it contains the string _Replica ###
$VMs = Get-VM -VMMServer "vmmserver01.domain.com" | Sort-Object Name
ForEach ($VM in $VMs)
$vmNameNew = [RegEx]::Match($VM.Name,"^[^_]+")
Write-Host "Virtual Machine:" $vmNameNew
#Set-SCVirtualMachine -VM $VM -Name $vmNameNew
This currently enumerates and renames only VM's with _Replica, but I'd like it to skip over the VMs that do NOT have _Replica.