Having a very bad Friday afternoon, no idea how I can eliminate a possible race condition?  I have a working script intermittently :(

I have a script that seems to work intermittently, usually it fails on the line of characters 1 128 (in bold below) I think that the VC will not return with information and therefore the script fails on the capture...

on the other

{

Write-Host "space is insufficient on the target data store. Please increase the space to continue.

Write-Output "space is insufficient on the target data store. Please increase the space to continue"> > $logfile

Write-Host "Exiting with code 1.

Write-Output "exit with code 1" > > $logfile

Send-MailMessage-to $From-$To-CC $Cc - subject $Subject '

-Body $BodyFailure1 - SmtpServer $SMTPServer - port $SMTPPort

Output 1

The architect suggested it might be a race condition, can someone with a lot more experience than me (it's pretty anyone) suggests an alteration that may well solve this problem?  I guess I could put breaks in but I'm not 100% where would be the best place to do so or if maybe it's the mickey mouse method?

Error

# ==============================================================================================

# NAME: CloneVMs

#

# AUTHOR: Luc

# DATE: 10/09/2015

#

# COMMENT: Creates a clone/backup of virtual machines described in the text and names using the

# past param.  Virtual MACHINE will be appointed in accordance with the following VM1-12345678 example

#

#

#

# * Software dependencies: *.

# - Microsoft PowerShell *.

# - VMWare PowerCLI *.

# ==============================================================================================

# Parameters

# ==============================================================================================

# Below is provided by Control-M parameter refers to the prefix point cycle and restoration,

# the date can be added automatically, however, this feature has been disabled.

Param ([string] $CYRPYYMMDD)

# ==============================================================================================

# Functions

# ==============================================================================================

#Function for you to check if a command is responsible

Function Check-Command ($cmdname)

{

return [bool] (Get-Command - Name $cmdname - ErrorAction SilentlyContinue)

}

# ==============================================================================================

# Varibles

# ==============================================================================================

# Add the snap of vmware for powershell

# Add - PSSnapin VMware.VimAutomation.Core

# Add the snap of vmware for powershell (keeps see you error messages)

If (control "Connect-VIServer")

{

Write-Host "already automation tool for".

}

on the other

{

#If not already loaded load the snap

Write-Host "loading automation tools.

Add-PSSnapin VMware.VimAutomation.Core

}

backup of # = true adds date; false creates a clone with the same name.

$backup = 'True '.

debugging of #-true: clones not vm. FALSE will clone the virtual machine.

$debug = "FALSE".

# Target Datastore

$targetdatastore = "LUN9_BASIC_MATADM_ONLY".

Check the #Refers the percentage of free space for space DS

$freePerc = 10

# Target location - folder existing in vcenter structure, where the clones will be held

$targetlocation = "DMAT DNT for Cycles compatibleM BackUp.

# Set a date

$datestart = (get-date - uformat %Y %m %d)

# Name a log file to record the results.

$logfile = $datestart + "_VMClones_bulk.txt".

#

Write-output "new journal ($datestart) - ($logfile)" > > $logfile

# Gets the list of virtual machines from the file associated with this script

$VmFileList = get-Content VmsToCloneList.list | Select-Object - jump 3

#

$ds = get-Datastore-name $targetdatastore

#Email varibles

$From = «»

$To = «»

$Cc = «»

#$Attachment = "C:\temp\Some random file.txt".

$Subject = «!» DMAT TEST createRP TEST! »

$BodySuccess = "A DMAT VM has correctly saved to the specified folder of destination organizational data store and inventory in vSphere"

$BodyFailure1 = "the combined use of VMDK put in service of the virtual machine is greater than the capacity of the destination database or the datstore does not exist, coming out with code 1"

$BodyFailure2 = "there is not enough space on the target data store, increase the space to continue, exit with code 1"

$BodyFailure3 = "a virtual machine with the specified name is not found maybe it does not exist? Exit with code 1 "

$SMTPServer = «»

$SMTPPort = "25".

# Connect

$Creds = get-VICredentialStoreItem-Host 127.0.0.1 - folder C:\Users\matadm\PS_ITC_BAS.xml

SE connect-VIServer 127.0.0.1-$ username-password Creds.User $Creds.Password

# Gets the total ProvisionedSpaceGB of VM for all s VM in the list.

# Ensures whitespace and hashes are effectively ignored

# create a clean list that calculates / creates the variable required for the validation check 1

$vmCleanedList = New-Object system. Collections.ArrayList

# Loop through our VM file list and own is in place, check for incorrectly specified VM

ForEach ($vmname to $VmFileList)

{

If ([string]: IsNullOrWhiteSpace ($vmname) - or $vmname.) StartsWith("#"))

{

writing-host("Invalid machine name-$vmname")

}

on the other

{

#Should trim vmname to ensure that we have no spaces before or the end

$vm = get - VM $vmname

$vmCleanedList.Add ($vmname)

$TotalRequiredGB += $vm. ProvisionedSpaceGB

}

Write-Host "Total needed = $TotalRequiredGB.

}

# ==============================================================================================

# Validation checks before launching the clone script

# ==============================================================================================

# Updates the prior to storage data store information.

$ds. ExtensionData.RefreshDatastoreStorageInfo)

# Validation check 1, check if the combined usage of VMDK put into service of the virtual machine is greater than the capacity of the destination database?

If (($ds.)) FreeSpaceGB - gt $TotalRequiredGB))

{

Write-Host 'continue with your script.

}

on the other

{

Write-Host "space is insufficient on the target data store. Please increase the space to continue.

Write-Output "space is insufficient on the target data store. Please increase the space to continue"> > $logfile

Write-Host "Exiting with code 1.

Write-Output "exit with code 1" > > $logfile

Send-MailMessage-to $From-$To-CC $Cc - subject $Subject '

-Body $BodyFailure1 - SmtpServer $SMTPServer - port $SMTPPort

Output 1

}

$ds = get-Datastore-name $targetdatastore

# 2 validation control, you have X % datatstorespace free?

If ((($ds.)) FreeSpaceGB / $ds. CapacityGB) * 100)-ge $freePerc)

{

Write-Host 'continue with your script.

}

on the other

{

Write-Host "there is less than X % of free space on the data store. Please increase the space to continue.

Write-Output "there less than X % of free space on the data store. Please increase the space to continue"> > $logfile

Write-Host "Exiting with code 2.

Write-Output 'output with code 2' > > $logfile

Send-MailMessage-to $From-$To-CC $Cc - subject $Subject '

-Body $BodyFailure2 - SmtpServer $SMTPServer - port $SMTPPort

Output 2

}

#Hack + of the duplication of efforts

#$VmListFile = get-Content VmsToCloneList.list | Select-Object - jump 3

ForEach ($vmname to $vmCleanedList)

{

# The virtual computer object

$vm = get-VM-name $vmname

# Target host - use the same host as the current VM (it's faster than the cloning on hosts).

$targethost = $vm.vmhost.name

# Target VM name - name if the BACKUP is FALSE

$vmtarget = $vmname

#

$datastore = get-datastore $targetdatastore - vmhost $targethost

# ==============================================================================================

# Start the real clone script

# ==============================================================================================

If ($vm - no $null)

{

Write-Host "a virtual machine named $VM exists. From clone "

Write-output "a virtual machine named $VM exists. From clone"> > $logfile

}

on the other

{

Write-Host "a virtual machine with the specified name is not found maybe it does not exist?

Write-Output "a virtual machine with the specified name is not found there is perhaps no?" > > $logfile

Write-Host "Exiting with code 3.

Write-Output "output with code 3" > > $logfile

Send-MailMessage-to $From-$To-CC $Cc - subject $Subject '

-Body $BodyFailure3 - SmtpServer $SMTPServer - port $SMTPPort

exit 3

}

If ($backup - eq "TRUE")

{

# Clone the virtual machine for backup_vmname_todaysdate

$vmtarget = $vmtarget + '-' + $CYRPYYMMDD #+ '-' + $datestart

}

beautiful colors # if you look at the script runs

write-host - foregroundcolor green 'cloning $vm to $vmtarget.

Write-output - foregroundcolor green 'Cloning $vm to $vmtarget' > > $logfile

new-vm-name $vmtarget - vm $vm vmhost - $targethost - $datastore - $targetlocation location data store - slim DiskStorageFormat

Send-MailMessage-to $From-$To-CC $Cc - subject $Subject '

-Body $BodySuccess - SmtpServer $SMTPServer - port $SMTPPort

}

Write-Host "complete."

Exit 0

# COMPLETED

Have you ever tried to test if $ds-eq $null before you call the RefreshDatastoreStorageInfo method?

In the error message, it looks like $ds contains $null, no idea why when the data store 'LUN9_BASIC_MATADM_ONLY' exists in the environment in which you are conencted.

In fact, you could test immediately after the Get-data store.

Ah, but wait, you seem to be doing the first Get-Datastore (there are two in the script, but I guess you know) before the connect-VIServer, it won't work.

Unless I'm missing something here

Tags: VMware

Similar Questions

Maybe you are looking for

  • I installed Windows XP but I can't access the internet. I checked the IPconfig and it is empty. The indicator light on the adapter is on green.

    I installed Windows XP but I can't access the internet. I checked the IPconfig and it is empty. The indicator light on the adapter is on green. Of course, the drivers are missing but whom? original title: IPConfig and lack of internet access

  • SPA400 Voice Message

    It is possble to leave a voicemail customized what callers can hear before leaving a message?

  • can not use wifi

    HelloW my name is michel , last night, I decided to reinstall my windows 7 like the version I was using started to crash and freez. After downloading and installing windows 7 ultimate 64 bit operating system I couldn't use wifi more. my pc just to sc

  • Can't connect to HP ePrintCenter

    I have problems to connect with HP ePrintCenter. I enter the e-mail address I used in the new configuration of the user on the login screen. Login screen does not accept my password. I then click on 'forgot password' and return the email address I us

  • um144m1p34

    Is there a solution for this error when you try to update to photoshop?Thank you