Looking for assistance. I have two major tasks I am trying to achieve.
1. To create a a new DVS with Port Groups (x6) and Uplinks defined (Open = vmnic 1,3 Restricted = 2,5)
2. Add new ESX Host to DVS.
I am very new to Power Shell (as you will see).
Question 1:
My first question is how to ensure PowerShell scripts are run using a Non-Personal Service Account in Microsoft AD without using RunAs on a .bat File each time. Does PowerGUI. e.g., have a built-in feature to run PowerShell scripts under a nominated account. I have searched under debug options. No joy there. What is th standard method here? My domain account does not have rights to connec to vCenter.High security environment.
Question 2:
Secondly for task 1 above I have developed the following using LucD notes: http://www.lucd.info/2009/10/09/dvswitch-scripting-part-1-creation. However, it is not working, see debug Task1.debug.log attached. Perhaps since Get-PowerCLIVersion returns the following: "VMware vSphere PowerCLI 4.1 build 264274" I should be using LucD's other post for vSphere SDK
for .Net: http://blogs.vmware.com/vipowershell/2010/07/vds-in-powercli-41.html
Please advise on best approach to take here.
##################################################################################################################
Script Information #########################################################################################
##################################################################################################################
#Name: DVS_Create-Define.ps1
#Author: Banking and Insurance Firm IT Dept
#Created: 11 November 2010
#Revisions: 01 Inital version
#Purpose: Create new DVS Routine
#NOTE: Sychronous with only one host being actioned at a time.
#INPUTS:
-VCServer : vCenter Serverhostname or IP address
-VCUser : vCenter Server useraccount
-VCPassword : vCenter Server password
-dvSwName : DVS to create
-portGroups : List of defined DVS's Port Groups
##
########################################################################################################################
param($VCServer,$VCUser,$VCPassword,$dcName,$dvSwName,$portGroups)
Connecting to the vCenter server
Connect-VIServer -server $VCServer -user $VCUser -password $VCPassword -ErrorVariable Err # -ErrorAction SilentlyContinue
if ($Err) {
Write-Log $TaskID "Critical" "Deploy halted. $Err" "$wflEID"
Close-Exit
} else {
Write-Log $TaskID "Info" "Connected to vCenter $VCServer" "$wflEID"
}
function New-dvSwitch{
param($dcName, $dvSwName, $baseUplink, $nrUplink)
$dc = Get-View -ViewType Datacenter -Filter @{"Name"=$dcName}
$net = Get-View -Id $dc.NetworkFolder
$spec = New-Object VMware.Vim.DVSCreateSpec
$spec.configSpec = New-Object VMware.Vim.DVSConfigSpec
$spec.configspec.name = $dvSwName
$spec.configspec.uplinkPortPolicy = New-Object VMware.Vim.DVSNameArrayUplinkPortPolicy
$spec.configspec.uplinkPortPolicy.UplinkPortName = (1..$nrUplink | % {$baseUplink + $_})
$taskMoRef = $net.CreateDVS_Task($spec)
$task = Get-View $taskMoRef
while("running","queued" -contains $task.Info.State){
$task.UpdateViewData("Info")
}
$task.Info.Result
}
function Get-dvSwHostCandidate{
param($container, $recursive, $dvs)
$dvSwMgr.QueryCompatibleHostForExistingDvs($dc.MoRef, $true, $dvs)
}
$datacenterName = "vc-wp-dcv"
$dvSwitchName = "dvSw1"
$dvSwitchUplinkBasename = "dvUp1"
$dvSwitchUplinkNumber = 2
$dvSwMgr = Get-View (Get-View ServiceInstance).content.dvSwitchManager
$dvSwMoRef = New-dvSwitch $datacenterName $dvSwitchName $dvSwitchUplinkBasename $dvSwitchUplinkNumber
$dvSw = Get-View -Id $dvSwMoRef
$dc = Get-Datacenter $datacenterName | Get-View
$candidates = Get-dvSwHostCandidate $dc.MoRef $true $dvSwMoRef
$candidates | % {
$esx = Get-View $_
if($esx.runtime.connectionState -eq "connected"){
$pnicInUse = @()
foreach($vswitch in $esx.Config.Network.Vswitch ){
foreach($pnic in $vswitch.pnic){
$pnicInUse += $pnic
}
}
foreach($vswitch in $esx.Config.Network.ProxySwitch ){
foreach($pnic in $vswitch.pnic){
$pnicInUse += $pnic
}
}
$pnicFree = @()
foreach($pnic in $esx.Config.Network.Pnic){
if(!($pnicInUse -contains $pnic.Key)){
$pnicFree += $pnic.Device
}
}
if($pnicFree.Count -ge $dvSwitchUplinkNumber){
Add-dvSwHost $dvSw $esx.MoRef $pnicFree $dvSwitchUplinkNumber
}
}
}
-
Question 3:
For Task 2 above, Add new ESX Host to DVS, I have developed the following.
##################################################################################################################
Script Information #########################################################################################
##################################################################################################################
#Name: DVS_Add_ESX.ps1
#Author: Banking and Insurance Firm IT Dept
#Created: 11 November 2010
#Revisions: 01 Inital version
#Purpose: Add ESX Hos to DVS routine
#NOTE: Sychronous with only one host being actioned at a time.
## INPUTS
-VCServer : vCenter Serverhostname or IP address
-VCUser : vCenter Server useraccount
-VCPassword : vCenter Server password
-Host : Host requiring addition to DVS
-DVS : Target DVS to add Host.
##
########################################################################################################################
param($VCServer,$VCUser,$VCPassword,$dcName,$Host,$DVS)
Connecting to the vCenter server
Connect-VIServer -server $VCServer -user $VCUser -password $VCPassword -ErrorVariable Err # -ErrorAction SilentlyContinue
if ($Err) {
Write-Log $TaskID "Critical" "Deploy halted. $Err" "$wflEID"
Close-Exit
} else {
Write-Log $TaskID "Info" "Connected to vCenter $VCServer" "$wflEID"
}
FUNCTION 1 - Add Host to DVS
27 function Add-dvSwHost{
28 param($dvSwitch, $hostMoRef, $pnic, $nrUplink)
29
30 $spec = New-Object VMware.Vim.DVSConfigSpec
31 $tgthost = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberConfigSpec
32 $tgthost.operation = "add"
33 $tgthost.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
34 0..($nrUplink - 1) | % {
35 $tgthost.Backing.PnicSpec += New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
36 $tgthost.Backing.PnicSpec[$_].pnicDevice = $pnic[$_]
37 }
38 $tgthost.host = $hostMoRef
39 $spec.Host = $tgthost
40 $dvSwitch.UpdateViewData()
41 $spec.ConfigVersion = $dvSwitch.Config.ConfigVersion
42
43 $taskMoRef = $dvSwitch.ReconfigureDvs_Task($spec)
44 $task = Get-View $taskMoRef
45 while("running","queued" -contains $task.Info.State){
46 $task.UpdateViewData("Info")
47 }
48 }
49
50 $datacenterName = "vc-wp-dcv"
51 $dvSwitchName = "dvSwitch001"
52 $dvSwitchUplinkBasename = "dvSwitch-DVUplink1-406"
53 $dvSwitchUplinkNumber = 2
54
55 $dvSwMgr = Get-View (Get-View ServiceInstance).content.dvSwitchManager
56
57 $dvSwMoRef = New-dvSwitch $datacenterName $dvSwitchName $dvSwitchUplinkBasename $dvSwitchUplinkNumber
58 $dvSw = Get-View -Id $dvSwMoRef
59
60 $dc = Get-Datacenter $datacenterName | Get-View
61 $candidates = Get-dvSwHostCandidate $dc.MoRef $true $dvSwMoRef
62 $candidates | % {
63 $esx = Get-View $_
64 if($esx.runtime.connectionState -eq "connected"){
65 $pnicInUse = @()
66 foreach($vswitch in $esx.Config.Network.Vswitch ){
67 foreach($pnic in $vswitch.pnic){
68 $pnicInUse += $pnic
69 }
70 }
71 foreach($vswitch in $esx.Config.Network.ProxySwitch ){
72 foreach($pnic in $vswitch.pnic){
73 $pnicInUse += $pnic
74 }
75 }
76
77 $pnicFree = @()
78 foreach($pnic in $esx.Config.Network.Pnic){
79 if(!($pnicInUse -contains $pnic.Key)){
80 $pnicFree += $pnic.Device
81 }
82 }
83 if($pnicFree.Count -ge $dvSwitchUplinkNumber){
84 Add-dvSwHost $dvSw $esx.MoRef $pnicFree $dvSwitchUplinkNumber
85 }
86 }
87 }
Again, debug Task2.debug.log attached.
It looks as if your Connect-VIServer fails in task 1.
Are you sure $vcserver is pointing to the vCenter ?
In Task 2 you seem to have copied the line numbers (27 on the line in error) along to your script.
This should not be there.
Btw the post "vDS in PowerCLI 4.1" is not ny me but was done by someone from the PowerCLI DEv Team.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
For your PowerGui question, I don't think PowerGui allows you to define a dedicated account for running the scripts.
The script will run under the account with which you are logged on.
But you can always ask in the PowerGui forum, perhaps someone over there knows of a way to do this.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Good tips LucD. Further now. VCServer was not specified accurately.
Please see scripts attached.
(1) DVS_Add_ESX.ps1 now reports:
Profile for 'TESTUSR' : TESTMGTSRV
Name Port User
---- ----
VCSERVER001 443 TESTMGTSRV
Get-View : Cannot validate argument on parameter 'Id'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command again.
At P:\test_scripts\DVS_Create-Define_2.ps1:64 char:21
+ $dvSw = Get-View -Id <<<< $dvSwMoRef
+ CategoryInfo : InvalidData: ( , ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
Exception calling "QueryCompatibleHostForExistingDvs" with "3" argument(s): "dv
s"
At P:\test_scripts\DVS_Create-Define_2.ps1:53 char:43
+ $dvSwMgr.QueryCompatibleHostForExistingDvs <<<< ($dc.MoRef, $true, $dvs)
+ CategoryInfo : NotSpecified: ( [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At P:\test_scripts\DVS_Create-Define_2.ps1:69 char:16
+ $esx = Get-View <<<< $_
+ CategoryInfo : InvalidData: ( , ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
(2) DVS_ADD_ESX.ps1
Profile for 'TESTUSR' : TESTMGTUSER
Name Port User
---- ----
VCSERVER001 443 ESTMGTUSER
Get-View : Cannot validate argument on parameter 'Id'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command ag
ain.
At P:\test_scripts\DVS_Create-Define_2.ps1:64 char:21
+ $dvSw = Get-View -Id <<<< $dvSwMoRef
+ CategoryInfo : InvalidData: ( , ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
Exception calling "QueryCompatibleHostForExistingDvs" with "3" argument(s): "dv
s"
At P:\test_scripts\DVS_Create-Define_2.ps1:53 char:43
+ $dvSwMgr.QueryCompatibleHostForExistingDvs <<<< ($dc.MoRef, $true, $dvs)
+ CategoryInfo : NotSpecified: ( [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At P:\test_scripts\DVS_Create-Define_2.ps1:69 char:16
+ $esx = Get-View <<<< $_
+ CategoryInfo : InvalidData: ( , ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView