Scheduled Job stops after logging to VMWare vCenter

Welcome Forums General PowerShell Q&A Scheduled Job stops after logging to VMWare vCenter

Viewing 0 reply threads
  • Author
    Posts
    • #179877
      Participant
      Topics: 4
      Replies: 4
      Points: 83
      Rank: Member

      Hello everyone,

      to give you an overview what is idea behind this script.
      Deployment of new virtual Windows Servers is done by templates in vmware.
      These templates needs to be update regulary, so I wrote script doing that.

      IDEA: Scheduled Job will run script that will do update process
      PROBLEM: If I schedule script, after logging to vCenter is stops. Scheduled job shows running status in Task Scheduler but no action continue.

      Does anyone had similar problem? Can anyone help me to point to where and how to troubleshoot?

      Here is piece of code, after first IF condition VM is converted but no line of code is processed

      # Loading functions into memory
      . $env:USERPROFILE\Documents\vmware-templates-maintenance\Write-MessageToLogFile.ps1 # Function "Write-MessageToLogFile" to write log entries to file
      
      # Defining variables
      $TemplateWin2k16Std = 'ops_run_win2k16test'
      $NewLogDateWin2k16Std = Get-Date -Format dd-MM-yyyy-HH-mm-ss
      $vCenterServer = 'VCENTERNAME'
      $CheckCounter = 0
      
      # New log file to record progress of template update
      if (!(Test-Path -Path "$env:USERPROFILE\Documents\vmware-templates-maintenance")) {
          New-Item -Path "$env:USERPROFILE\Documents" -Name 'vmware-templates-maintenance' -ItemType Directory
          New-Item -Path "$env:USERPROFILE\Documents\vmware-templates-maintenance" -Name "$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt" -ItemType File
      } else {
          New-Item -Path "$env:USERPROFILE\Documents\vmware-templates-maintenance" -Name "$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt" -ItemType File
      }
      
      
      # >>>> Preparing virtual machine <<<<
      # Connecting to vCenter
      $UserNameToAccessVcenter = 'SERVICEACCOUNTNAME'
      $EncryptedPasswordToAccessVcenter = Get-Content -Path "$env:USERPROFILE\Documents\vmware-templates-maintenance\eid.txt" | ConvertTo-SecureString
      $CredentialsToAccessVcenter = New-Object -TypeName System.Management.Automation.PSCredential($UserNameToAccessVcenter, $EncryptedPasswordToAccessVcenter)
      
      try {
          Write-MessageToLogFile -LogMessage "INFORMATION - Script is connecting to vCenter $vCenterServer" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          Connect-VIServer -Server $vCenterServer -Credential $CredentialsToAccessVcenter -ErrorAction Stop
          Write-MessageToLogFile -LogMessage "INFORMATION - Script successfully connected to vCenter $vCenterServer" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          Start-Sleep -Seconds 5
      }
      catch {
          Write-MessageToLogFile -LogMessage "ERROR - Script did not connect to vCenter $vCenterServer" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          Start-Sleep -Seconds 5
          Write-MessageToLogFile -LogMessage "ERROR - script has been terminated!" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          exit
      }
      
      # Converting template to virtual machine
      if (Get-Template -Name $TemplateWin2k16Std -ErrorAction SilentlyContinue) {
          Set-Template -Template $TemplateWin2k16Std -ToVM
          Write-MessageToLogFile -LogMessage "INFORMATION - Template $TemplateWin2k16Std converted to virtual machine" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          Start-Sleep -Seconds 5
      } elseif (Get-VM -Name $TemplateWin2k16Std -ErrorAction SilentlyContinue) {
          Get-VM -Name $TemplateWin2k16Std
          Write-MessageToLogFile -LogMessage "WARNING - Template $TemplateWin2k16Std already set as virtual machine" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          Start-Sleep -Seconds 5
      } else {
          Write-MessageToLogFile -LogMessage "ERROR - Template $TemplateWin2k16Std not found or other problem to convert template occured" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          Start-Sleep -Seconds 5
          Write-MessageToLogFile -LogMessage "ERROR - script has been terminated!" -LogFilePath "$env:USERPROFILE\Documents\vmware-templates-maintenance\$TemplateWin2k16Std-$NewLogDateWin2k16Std.txt"
          exit
      }
      
      

      Here is code for function Write-MessageToLogFile

      function Write-MessageToLogFile {
          [CmdletBinding()]
          param (
              # Message as string that will be written to log file
              [Parameter(Mandatory=$True)]
              [string] $LogMessage,
              # Path to log file
              [Parameter(Mandatory=$True)]
              [string] $LogFilePath
          )
          
          begin {
              $GetDateForLogEntry = Get-Date -Format dd-MM-yyyy-HH-mm-ss
          }
          
          process {
              "$GetDateForLogEntry - $LogMessage" | Out-File -FilePath $LogFilePath -Append
          }
          
          end {
              Clear-Variable -Name GetDateForLogEntry
          }
      }
      
Viewing 0 reply threads
  • The topic ‘Scheduled Job stops after logging to VMWare vCenter’ is closed to new replies.