3 Replies Latest reply on Sep 20, 2019 11:57 AM by vespavbb

    Foreach Rename VM with $name plus XX

    vespavbb Novice

      Hi,

       

      I need some help.

       

      I wannt to do a foreach loop with import of some VM´s, shut them down and rename each VM to the original VM Name plus "_xx_delete"

       

      so for example vm " VMADC01 will renamed to VMADC01_xx_delete after shutdown,

      I have the loop but now clue how to rename it

       

       

       

       

      ######################

      $importDatei = "C:\scripts\VMliste.txt"

       

       

      $vmlist = Import-Csv -Path $importDatei -Delimiter ";"

       

       

      foreach ($vm in $vmlist)

       

       

      {

       

       

       

      $vm1 = Get-VM -Name $vm.name -ErrorAction:SilentlyContinue

       

      if ($vm1.powerstate -eq "poweredon"-and $vm1.Guest.State -eq "Running")

      {

      $vm1  | Shutdown-VMGuest -Confirm:$false | out-null

       

       

      Write-Host $vm1.name  "go offline soft"

       

       

      sleep -Seconds 3

      }

       

       

       

      elseif($vm1.powerstate -eq "poweredon"-and $vm1.Guest.State -eq "NotRunning")

       

       

      {

      $vm1 | where {$_.Guest.State -eq "NotRunning"} | Stop-VM -Confirm:$false | out-null

       

       

      Write-Host $vm1.name  "go offline hard"

       

       

      sleep -Seconds 3

      }

       

       

      else

      {write-host $vm1.name "is already offline" | Format-Table -AutoSize

       

      }

      }

        • 1. Re: Foreach Rename VM with $name plus XX
          Gidrakos Hot Shot

          $newVmName = ($vm1.name + "_xx_delete")

           

          Set-VM [vmCurrentName] -Name $newVmName -confirm:$false

           

           

          Throw that in the loop and it will rename it for ya

          1 person found this helpful
          • 2. Re: Foreach Rename VM with $name plus XX
            LucD Guru
            Community WarriorsvExpertUser Moderators

            Try something like this

             

            Invoke-VMScript -VM <your-vm> -ScriptText 'echo %Path%' -ScriptType Bat

            ######################

            $importDatei = "C:\scripts\VMliste.txt"

            $vmlist = Import-Csv -Path $importDatei -Delimiter ";"

            foreach ($vm in $vmlist) {

                $vm1 = Get-VM -Name $vm.name -ErrorAction:SilentlyContinue

                if ($vm1.powerstate -eq "poweredon" -and $vm1.Guest.State -eq "Running") {

                    $vm1 | Shutdown-VMGuest -Confirm:$false | out-null

                    Write-Host $vm1.name  "go offline soft"

                    sleep -Seconds 3

                }

                elseif ($vm1.powerstate -eq "poweredon" -and $vm1.Guest.State -eq "NotRunning") {

                    $vm1 | where { $_.Guest.State -eq "NotRunning" } | Stop-VM -Confirm:$false | out-null

                    Write-Host $vm1.name  "go offline hard"

                    sleep -Seconds 3

                }

                else {

                    write-host $vm1.name "is already offline" | Format-Table -AutoSize

                }

                Set-VM -VM $vm1 -Name "$($vm1.Name)_xx_delete" -Confirm:$false

            }

            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
            1 person found this helpful
            • 3. Re: Foreach Rename VM with $name plus XX
              vespavbb Novice

              Thank you so much guys,  thats exactly what I ´m looking for