5 Replies Latest reply on Sep 11, 2015 4:35 AM by DZ1

    Find Lower Case VMs

    russjar Enthusiast

      Hi all,

       

      Trying to find all the lowercase VMs so I can then rename to UPPERCASE.

       

      When I run my command below it only returns a single lowercase VM when I know I have a lot more than one.

       

      Can someone push me in the right direction please?

       

      Thanks....

       

      get-vm | Where-Object { $_.name -match "[A-Za-z]+_[A-Za-z]+" }

        • 1. Re: Find Lower Case VMs
          pkolom Enthusiast

          You must use cmatch command:

           

          $vms = (Get-VM -Name TEST_vm).Name
          if ($vms -cmatch '^[A-Z\0-9\s-]*$') {write-host "name has allowed chars"} else {Write-Warning "name has low chars"}
          


          In this case allowed chars are A-Z, 0-9 and -


          First line puts in $vms variable only VM name. You should probably change $vms variable to an array.

           

          If all your VMs must be uppercase, you can also use ToUpper() method. Try this:

           

          $vms.ToUpper()

           

          • 2. Re: Find Lower Case VMs
            LucD Guru
            User ModeratorsCommunity WarriorsvExpert

            Try like this

             

            Get-VM | where {$_.Name -cmatch "^[a-z]+_[a-z]+$"}

             

            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
            • 3. Re: Find Lower Case VMs
              russjar Enthusiast

              The only one I found to work was this...

               

              get-vm | Where-Object { $_.name -cmatch "[a-z]+" }

              • 4. Re: Find Lower Case VMs
                LucD Guru
                User ModeratorsvExpertCommunity Warriors

                What is the layout of your VM names then ?

                Is it "abc-xyz" like your first code seems to suggest, or "abc" like this last code seems to suggest ?

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: Find Lower Case VMs
                  DZ1 Hot Shot

                  This should work.  I've used it in reverse to rename all upper to lower case.  I left the -Whatif there, but I usually change it to -confirm:$false

                   

                  get-vm | where { $_.name.ToLower() -ceq $_.name } | foreach { Set-VM -VM $_ -Name ($_.name.ToUpper() ) -whatif }