3 Replies Latest reply on Feb 8, 2019 1:58 PM by LucD

    General query

    marco2601 Novice

      Friends I have the two following scritps the first one is constantly pinging the Ip of a VM at the moment that there is no ping a cloned VM is turned on



      The second script simply clones a VM origena to that VM I ping with the first script



      Well the inconvniente is the following:



      Run the two scripts at the same time the ping script works correctly, then while cloning is done with the other script at the time of the cloning the sentence "else" of the first script is executed is like missing the vm ping but in this case it does not lose connection that could be happening...





      #IP VM origen

      $VMIP= ""

      ##Sentencia para verificacion de conexion a VM origen




      ##Ping hacia la IP de la VM Origen

      $results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$VMIP'"



      ##Si tenemos conexion mostrara mensaje que la VM esta activa

      if ($results.StatusCode -eq 0) {

      Write-Host "$VMIP online"


      ##Caso contrario mostrara que esta offline, desctivara vNIC de la Origen y encendera la ultima VM clonada

      else {

      Write-Host "$VMIP offline"

      ##Coneccion a vCenter

      connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd

      #Nombre de la VM Origen

      $VMorigen= "SRV-SERVER"

      #Nombre de la ultima VM clonada

      $VMclone= "$($VMorigen)-$((Get-Date).ToString('MMddyyyy'))"

      Get-VM -Name SRV-SERVER | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$false -Confirm:$false

      GET-VM -Name $VMclone | Start-VM -Confirm:$False

      ##Desconnexion de vCenter

      Disconnect-VIServer -Confirm:$false

      ##Para sentencia en caso de no tener conexion



      }while($i -le 9999)





      #Validacion en caso de noter certificado en el vcenter

      Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

      ##Coneccion a vCenter

      connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd



      #Nombre de la VM Origen

      $VMorigen = "SRV-SERVER"

      # Ip o hostname de host en donde se clonara la VM

      $esxi = ""

      #Nombre para VM clonada con fecha de clonacion

      $newVMclone = "$($VMorigen)-$((Get-Date).ToString('MMddyyyy'))"

      #Variable para que busque el clone de hace  un dia

      $oldCloneName = "$($VMorigen)-$((Get-Date).AddDays(0).ToString('MMddyyyy'))"

      #Datastore en donde se guardara la VM

      $datastore=Get-datastore -Name 'Datastore-1'

      #Parametro para que la VM se guarde en un Datastore compratido dentro de un cluster

      $ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData.Summary.MultipleHostAccess} | Sort-Object -Property FreeSpaceGB -Descending | select -First 1

      #Parametro para que la VM se guarde en cualquier host de lcluster

      $esx = Get-Cluster -VM $VMorigen | Get-VMHost | Get-Random



      ##Elimina VM de hasta 1 dia en base al nombre del VM clonada

      Get-VM -Name $oldCloneName -ErrorAction SilentlyContinue | Remove-VM -DeletePermanently:$true -Confirm:$false



      ##Clonacion de VM, disco virtual tipo thick y carpeta de almacenaiento de la VM



      if(New-VM -VM $VMorigen -Name $newVMclone -VMHost $esxi -DiskStorageFormat Thin -Datastore $datastore -Location "Laboratorios"){



      "Error al clonar la VM"


      ##Desconnexion de vCenter

      Disconnect-VIServer -Confirm:$false


        • 1. Re: General query
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          So what is the question?
          Do you want to combine the 2 scripts in 1 script?

          • 2. Re: General query
            marco2601 Novice

            mmm no, the 2 scripts are running as a program task in windows, the scrip ping is running all the time, but the scrip for clnar is executed once a day, when the cloning script is executed at the end of the cloning the ping script in the statement that sends the conection test is like it loses connection and executes the "else" statement of the ping script but in no time connection is lost at the time of cloning

            • 3. Re: General query
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              I'm not sure how you can remove the old clone while it is still powered on.
              Shouldn't you power it off before removing it?