I'd rather not use Out-Null, because it might hide something useful. Here's the full script, maybe it shows the the reason for all the output? I've attached the output as file "screenoutput.txt".
May I add a second question, now I'm revealing my poor scripting (all advice for improvement gratefully accepted!) ? The script tries to do Disconnect-VIServer at the end, but somehow I can't get the -Server parameter right; that is, Disconnect-VIServer doesn't accept the $Server object returned by $Server = Connect-VIserver -Server 10.1.1.27 <etc>
I hope it's OK to put the script inline - if attachments are preferred please let me know (obviously if it was bigger, then attachment would be better).
Thanks
Alan
# $Id: //depot/branch/alansi/build/bin/MakeVpx.ps1#5 $
# $DateTime: 2013/01/29 17:07:38 $
# $Change: 317348 $
param (
[string]$version = "7.0.0.1", # the real versioning is set by kickstart script, this is just for filename
[string]$tgtdir = $env:USERPROFILE + "\Desktop\"
)
[string]$Scriptrev ='$Revision: #5 $'
[string]$Vmname = "CBR-ESX-VPX"
[string]$Outputdir = $tgtdir # TODO 'env' probably NOT suitable destination using ssh
# connection params for ESXi host
[string]$ESXHOST = "10.1.1.27"
[string]$username = "root"
[string]$password = ""
###########################################################################
# show what's running
$fullPathIncFileName = $MyInvocation.MyCommand.Definition
#$currentScriptName = $MyInvocation.MyCommand.Name
#$currentExecutingPath = $fullPathIncFileName.Replace($currentScriptName, "")
$msg = "{0}({1}) creating {2} version {3} in {4} on host {5}" -f $fullPathIncFileName, $Scriptrev, $Vmname, $VERSION, $Outputdir, $ESXHOST
write-host $msg
Get-Date
# load the VMware module
[string]$VimAutoCore = "VMware.VimAutomation.Core"
if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host "loading $VimAutoCore powershell module"
Add-PsSnapin $VimAutoCore
}
###########################################################################
#
# connect to ESX host
$protocol = "https"
$port=443
$Server=''
$myserver=''
Try {
$myserver = get-vmhost -Server $ESXHOST -State connected -erroraction "Stop"
#if (get-vmhost -Server $ESXHOST -State connected -erroraction "Stop") {
if ($myserver) {
Write-host "already connected to $myserver"
$Server = $myserver
$connected=$True
}
}
Catch {
Try {
Write-Host "Connecting to ESX host server $ESXHOST"
$Server = Connect-VIserver -Server $ESXHOST -user $username -password $password -errorAction "Stop"
$connected=$True
Write-Host "Connected OK to server $Server"
}
Catch {
$msg="Failed to connect to server {0} on {1} as user {2}" -f $ESXHOST,$protocol,$username
#Write-Warning $msg
#Write-Warning $error[0].Exception.Message
Write-Error $msg
Write-Error $error[0].Exception.Message
Exit 1
}
}
if ($connected) {
#Write-Host "Connected to VM host $ESXHOST"
Write-Host "Connected to VM host $Server"
}
###########################################################################
#
# create the CBR VM
# remove any pre-existing CBR VM
if(Get-VM $Vmname -ErrorAction SilentlyContinue){
Write-Host "Found pre-existing VM '$Vmname' on $Server, will delete it"
if((Get-VM $Vmname).powerstate -eq "PoweredOn"){
Write-Host "$Vmname is powered on, powering it off"
Shutdown-VMGuest -VM (Get-VM $Vmname) -Confirm:$false
}
Remove-VM $Vmname -DeletePermanently -Confirm:$false
if( ( Get-VM $Vmname -ErrorAction SilentlyContinue) -eq $null) {
Write-Host "Removed $Vmname"
}
} else {
Write-Host "No pre-existing VM '$Vmname' found"
}
# make the new VM
# no simple way to define 1 CPU with 2 cores-per-socket, so give 2 CPUs for now
Try {
$cbrvm = New-VM -Name $Vmname -Version v7 `
-NumCpu 2 -MemoryMB 1024 -DiskMb 61440 `
-DiskStorageFormat Thick `
-GuestId rhel5_64Guest `
-Notes "Provides services" `
-Confirm:$false `
-ErrorAction Stop
}
Catch {
$msg="Failed to create New-VM named {0}" -f $Vmname
# Write-Warning $msg
# Write-Warning $error[0].Exception.Message
Write-Error $msg
Write-Error $error[0].Exception.Message
Exit 1
}
Write-Host "New vm $Vmname created, ready for install"
# use modified CentOS ISO for boot from CD ======================================
#
# There's no way to enter a kickstart command
# so the CentOS boot disk is modified to include the command to load our kickstart script
# The ISO defines
# name and location of kickstart script
# temporary IP address and network info for the new VM
# The kickstart script is updated with version number etc before use
"WARNING: Using modified CentOS boot cd ISO with *HARDCODED* IP addresses and Kickstart script name"
$cbrcd = New-CDDrive -VM $cbrvm -ISOPath "[datastore1] ISO/CentosForESX.iso" -StartConnected
#" " # pause here for me to open the console in client
#$a = read-host " press any key to START $Vmname"
# boot the VM, CBR installs automagically, configured by kickstart script ======================================
Write-Host "Installing Branch Repeater in the new VM"
Start-VM -VM $cbrvm -Confirm:$false
<#
# show what we've got
$nic = Get-NetworkAdapter -VM $cbrvm
"vm is $cbrvm"
"cd is $cbrcd"
"nic is $nic"
#>
# TODO wait for semaphore then shutdown ... for now sleeping instead of semaphore
Write-Host "Please wait"
$SleepSecs = 300 # five mins probably NOT long enough
#$SleepSecs = 600 # longer wait
Start-Sleep $SleepSecs
# following code assumes the CBR VM was installed OK and is ready to export
# shutdown the VM ======================================
#" "
#$a = read-host " press any key to STOP $Vmname"
Stop-VM -VM $cbrvm -Confirm:$false
#Configure the CBR VPX - set NICs, CPUs, CD ======================================
Remove-CDDrive -CD $cbrcd -Confirm:$false # VM must be in state PoweredOff
$nic = Get-NetworkAdapter -VM $cbrvm
Remove-NetworkAdapter -NetworkAdapter $nic -Confirm:$false # VM must be in state PoweredOff
Get-VM $cbrvm | New-NetworkAdapter -NetworkName "APA-1" -StartConnected -Type Vmxnet3
Get-VM $cbrvm | New-NetworkAdapter -NetworkName "APA-2" -StartConnected -Type Vmxnet3
# for two cores per socket cannot use -NumCPU but *might* be able to use
# $spec=New-Object –Type VMware.Vim.VirtualMAchineConfigSpec –Property @{“NumCoresPerSocket” = 1} (Get-VM –Name Test_VM).ExtensionData.ReconfigVM_Task($spec)
#export the CBR VPX ======================================
#
$ovafilename = $Outputdir + "\" + $Vmname + "-" + $VERSION + ".ova"
# makes export dir if rqd
# overwrites (deletes & replaces) an existing file
#
$ovftool = "C:\Program Files\VMware\VMware OVF Tool\ovftool.exe"
if (!(test-path $ovftool))
{
write-host -ForegroundColor red "ERROR: Cannot find OVFtool $ovftool"
# download ovftool here: http://www.vmware.com/support/developer/ovf/
exit 1
}
# TODO modify these to use same naming throughout
if(!( test-path "$Outputdir" ))
{
Try {
New-Item -itemtype "Directory" "$Outputdir" -force -Confirm:$False
}
Catch {
$msg="Failed to creat output directory {0}" -f $Outputdir
Write-Warning $msg
Write-Warning $error[0].Exception.Message
Exit 1
}
}
if( test-path "$ovafilename" )
{
write-host "deleting previous $ovafilename"
Try {
Remove-Item "$ovafilename" -force -Confirm:$False -ErrorAction Stop
}
Catch {
$msg = "Failed to delete pre-existing file {0}\{1}" -f $Outputdir,$ovafilename
Write-Warning $msg
Write-Warning $error[0].Exception.Message
Exit 1
}
}
function Export-VM
{
# from http://geekafterfive.com/category/vmware/page/3/
param
(
[parameter(Mandatory=$true,ValueFromPipeline=$true)] $vm,
[parameter(Mandatory=$true)][String] $destination
)
$ovftool = "C:\Program Files\VMware\VMware OVF Tool\ovftool.exe"
if( test-path "$($destination)$($vm.name).ova" )
{
write-host "deleting previous $($destination)$($vm.name).ova"
Remove-Item "$($destination)$($vm.name).ova" -force -Confirm:$False
} else {
write-host "did not see $($destination)\$($vm.name).ova"
}
$moref = $vm.ExtensionData.MoRef.Value
$session=get-view -Id $global:DefaultVIServer.ExtensionData.Client.ServiceContent.SessionManager
$ticket = $session.AcquireCloneTicket()
& $ovftool --skipManifestCheck "--I:sourceSessionTicket=$($ticket)" `
"vi://$($defaultviserver.name)?moref=vim.VirtualMachine:$($moref)" `
"$($destination)$($vm.name).ova"
}
Export-VM $cbrvm $Outputdir
####################################################
" VM export complete"
" "
# destroy the VM and delete it from disk ======================================
#$a = read-host " press any key to delete $Vmname and disconnect from $ESXHOST"
Remove-VM $Vmname -DeletePermanently -Confirm:$false
"VM $Vmname deleted from ESX host $Server"
if ( (Disconnect-VIServer -Server $Server -Force -Confirm:$false -ErrorAction SilentlyContinue) -eq $null ) {
Write-Host "did not disconnect"
} else {
Write-Host "Disconnected from $ESXHOST"
}
Disconnect-viserver * -force -confirm:$false
Write-Host "Finished."
# all done