PowerShell + VMM + RegEx match

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
    $vmNameNew
    #Set-SCVirtualMachine -VM $VM -Name $vmNameNew
}

Open in new window


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.
JWeb AdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Why do you need a regex?

### 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) 
{
    If ($VM.Name.Contains("_Replica"))
    {
      $vmNameNew = $VM.Name.REplace("_Replica", "")
      Write-Host "Virtual Machine:" $vmNameNew
      $vmNameNew
      #Set-SCVirtualMachine -VM $VM -Name $vmNameNew
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JWeb AdminAuthor Commented:
Doesn't have to be a regex  - is this more efficient?
0
käµfm³d 👽Commented:
Did you see my proposed code snippet?
0
Terry WoodsIT GuruCommented:
It's generally more efficient not to use a regex when no pattern wildcards are needed in a value to be matched or replaced.
0
JWeb AdminAuthor Commented:
Excellent  thank you!  It works great.  I was over thinking it I suppose.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.