So it seems I'm never done with this script. I want there to be default answers in some areas, and if a user passes something that's invalid it asks again until they pass a proper bit of info.
$type = Read-Host "Enter Host or Cluster"
if ($type -eq "Cluster")
{
Get-Cluster | Select Name | sort Name | Format-List #Gets a list of clusters in the vCenter#
$Cluster = Read-Host "Enter Cluster Name"
$myCluster = Get-Cluster -Name "$Cluster"
Get-VMHost -Location "$myCluster" -state "Connected" | sort Name | Select Name,ConnectionState,PowerState | Format-Table #Lists the host from the cluster specified earlier#
}
else
{
Get-VMHost | where {$_.state -eq "Connected"} | sort Name | Select Name,ConnectionState,PowerState | Format-Table
}
$vmHost = Read-Host "Enter host name"
Get-Datastore -VMHost $vmHost | where {$_.type -eq "NFS"} | Select @{N="Cluster";E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} | Out-Default #Lists datastores and capacity#
$datastore = Read-Host "Enter Datastore Name"
$myDatastore = Get-DataStore -Name "$datastore"
$ovfhost = Read-Host "Enter path to OVA/OVF"
$vmName = Read-Host "Enter a name for the VM"
$diskStorage = Read-Host "Enter Thin, Thick or EagerZeroedThick for disk format"
Import-vApp -Source "$ovfhost" -VMHost $vmHost -Location $myCluster -Name "$vmName" -DiskStorageFormat $diskStorage -Datastore $myDatastore #Imports the OVA/OVF into vCenter#
Sleep 10
$VMmove = Read-Host "Enter folder to move VM to"
Move-VM -VM $vmName -Destination $VMmove #Moves VM to the appropriate folder#
Get-Cluster -Name "$Cluster" | Get-VMHost -Name "$vmHost" | Foreach-Object {$strClusterName = $_.Parent.Name; Get-VirtualPortGroup $_ | Select-Object @{N="Cluster";E={$strClusterName}},Name,VLanId} #Gets available VLANs from the host#
$VLAN = Read-Host "Please enter the VLAN name to use"
$NIC = Get-NetworkAdapter -VM $vmName
So for the first part for $type it defaults to Host which I'm fine with. After that I get a list of only hosts that are in the Connected state. I'd like it if someone could just only type a name from the list provided, and if they don't provide one it prompts again. Same thing would be fore the rest really, for the datastore, disk storage, folder path (hoping they put in a valid path) and VLAN type. I'm new to all of this and have been googling around, just haven't found what I need yet.
Yes.
In that case you'll have to foresee that case in your code when obtaining the $vlans content.
See attached.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I tried that script and it fails when importing the OVA. I get the following:
Import-VApp : Cannot convert 'System.Object[]' to the type
'VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.StorageResource'
required by parameter 'Datastore'. Specified method is not supported.
At C:\Users\emcclure\Desktop\test.ps1:33 char:13
+ Import-vApp @sVapp
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Import-VApp], ParameterBin
dingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor
e.Cmdlets.Commands.ImportVApp
That seems to indicate that you more than 1 datastore selected in the $sVApp.Datastore property.
At least that is what the 'System.Object[]' part in the error message seems to indicate
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That I selected more than 1 datastore? I ran thru it again, still doesn't work. I can only select 1 of each thing that pops up, so I don't know what's going on here.
That would mean that Get-DataStore -Name $datastore.Name returns more than 1 object.
Try changing that line to
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I realize that this is bringing up an old thread of mine, but I found that I never quite got the script to work the way I want. I have attached a copy of the script since there's a lot to it. Basically I'm getting the following error:
Set-NetworkAdapter : 11/26/2018 3:55:24 PM Set-NetworkAdapter The network "@{Cluster=; Name=VLAN3654; VLanId=3654}"
doesn't exist on the host.
At C:\Users\emcclure\Desktop\GenScripts\GenImportOVF.ps1:96 char:1
+ Set-NetworkAdapter -NetworkAdapter $NIC -NetworkName $vlan -Confirm:$ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (@{Cluster=; Name=VLAN3654; VLanId=3654}:String) [Set-NetworkAdapte
r], ViError
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_TryGetHostNetworkByName_NonexistentNetwork,VMware.VimAutomati
on.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter
Which would be on line 79 in the script I attached. There was a point before I added lines 69 - 74 that it would work ok when I selected Host, but now I get the error for selecting both host and cluster. It seems to work, but I don't want to see the error popping up. Any idea on what change needs to be made to fix this once and for all?
The $vlan variable contains the output of a Select-Object cmdlet.
Try like this
Set-NetworkAdapter -NetworkAdapter $NIC -NetworkName $vlan.Name -Confirm:$false #Applies selected VLAN to VM#
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
That did it. Works like a charm now for both host and cluster. Thanks so much.