VMware Cloud Community
lukeglazebrook
Enthusiast
Enthusiast

Newbie, would be grateful if someone could give me a clue as to why my cloning script fails withe following message...

So I have a script which is essentially supposed to clone VM's within a given folder.  I have modiefied it slightly in an attempt to get it to accept an argument specifying which folder to clone the VM's within.  Unfortuately however manually running it fails with the following...

The PowerCLI Error (see attached)

The Script...

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

# NAME: Showdown VM's in order with delay

#

# AUTHOR: Luke's script

# DATE  : 01/09/2015

#

# COMMENT:

#

# * Software Dependencies: *

# - Microsoft PowerShell *

# - VMWare PowerCLI *

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

# Parameters

param (

[string]$SourceVmFolderToClone)

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

# Establish Connection

Connect-VIServer -Server 127.0.0.1 -User administrator@Topsecret.net -Password TopSecret

# ----- Check if the passed parameters are empty or not

#

if(!$SourceVmFolderToClone)

{

Write-Output "You havent supplied the name of a valid folder containing VMs that exist within the virtual infrastructure"

}

else

{

#

# ----- Split the passed parameters and for each value perform task

#

$SourceVmFolderToClone.Split(',') | ForEach {

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

# Varibles

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

# Pause Time

$waittime = 60 #Seconds

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

$backup = "true"

# debug - true : will not clone vm; FALSE will clone the vm.

$debug = "FALSE"

# Set some variables.

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

# Name a logfile to capture results.

$logfile = $datestart + "_VMClones_bulk.txt"

echo  "New Log ($datestart) - ($logfile)" >> $logfile

# sourceLocation is a folder in your vcenter structure.

$SourceVmFolderToClone = "$SourceVmFolderToClone"

# Target Datastore

$datastore = "Synology LUN1"

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

$targetlocation = "TestTarget"

# Datacenter name

$datacenter = "Datacenter"

# get a list of servers from the sourceLocation

$vmservers = Get-VM -Location $SourceVmFolderToClone

# Loop through servers

echo  "Begin ($SourceVmFolderToClone)" >> $logfile

foreach ($vm in $vmservers)

{

# Target Host - use the same host as the current VM ( this is faster than cloning across hosts ).

$targethost = $vm.vmhost.name

# Source VM Name

$vmname = $vm.Name

# Target VM Name - name if BACKUP is FALSE

$vmtarget = $vm.Name

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

if ($backup -eq "TRUE")  {

  # Clone the VM to backup_vmname_todaysdate

  $vmtarget = "backup_" + $vmtarget + "_" + $datestart

}

# nice colors if you are watching the script run

write-host -foregroundcolor green "Cloning $vm to $vmtarget"

if ($debug -ne "TRUE") {

   # actually clone the VM.

   new-vm -name $vmtarget -vm $vm -vmhost $targethost -datastore $datastore -Location $targetlocation -DiskStorageFormat thin

}

# See if the clone succeded and log the status.

if (get-vm $vmtarget) {

  echo  "Cloned $vmname to $vmtarget on $targethost disk $datastore" >> $logfile

}

else {

  echo  "Failed Cloning $vmname to $vmtarget on $targethost"  >> $logfile

}

  

# end foreach loop

}

# COMPLETED

0 Kudos
2 Replies
LucD
Leadership
Leadership

Where did you store the .ps1 file ?

Now the PS engine assumes it is in the folder where the prompt points to.

You can specify the full path to the .ps1 file as an alternative.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
pkolom
Enthusiast
Enthusiast

To run ps1 scripts from console window, you must add .\ ​at the beggining of script name. That's security measure.

0 Kudos