Search code examples
powercli

why powercli script is not accepting the variable parameters during the execution?


When I am trying to run this powercli script getting below error

The argument is null or empty. Provide an argument that is not null or empty, and then try the | command again

but its is working with static value. any clue why this is happening. here is the command with parameters

/usr/bin/pwsh /ansible-vmware/vmwarehost-config-playbooks/lag-add-host.ps1 -vcenter_fqdn "10.10.0.1" -vcenter_username "xxxxxx.abc.com" -vcenter_password "XXXX" -newhostname "hostdone1" -domainname "abc.com" -dvswitchname_1 "dv1" -nic1_pnicmac1 "ac:1f:6b:cb:83:3a" -cluster_name "xyzclr" -lag_name1 "lag1-0"

Script content:

#!/usr/bin/pwsh -command

param(
[String] $vcenter_fqdn,
[String] $vcenter_username,
[String] $vcenter_password,
[String] $newhostname,
[String] $domainname,
[String] $dvswitchname_1,
[String] $cluster_name,
[String] $lag_name1,
[String] $nic1_pnicmac1
)

# VC connectivity
Write-Host "Connecting to VC host " $vcenter_fqdn
Connect-VIServer -Server $vcenter_fqdn -User $vcenter_username -Password $vcenter_password -Force

$esxName1 = Get-cluster -Name $cluster_name | Get-VMhost -Name $newhostname.$domainname

$esxName=$esxName1.name
$vdsName1 = Get-VDSwitch $dvswitchname_1
$vdsName = $vdsName1.name

Add-VDSwitchVMHost -VMHost $esxName -VDSwitch $vdsName1
$esxihostnic1 = Get-VMHost hostdone1.examlab.com  | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.Mac -eq "$nic1_pnicmac1"}
$pnicName = $esxihostnic1.name

$uplinkName = $lag_name1

Solution

  • #!/usr/bin/pwsh -command
    
    param(
    [String] $vcenter_fqdn,
    [String] $vcenter_username,
    [String] $vcenter_password,
    [String] $newhostname,
    [String] $domainname,
    [String] $dvswitchname_1,
    [String] $cluster_name,
    [String] $lag_name1,
    [String] $nic1_pnicmac1
    )
    
    # VC connectivity
    Write-Host "Connecting to VC host " $vcenter_fqdn
    Connect-VIServer -Server $vcenter_fqdn -User $vcenter_username -Password $vcenter_password -Force
    
    $esxName1 = Get-cluster -Name $cluster_name | Get-VMhost -Name $newhostname.$domainname
    
    $esxName=$esxName1.name
    $vdsName1 = Get-VDSwitch "$dvswitchname_1"
    $vdsName = $vdsName1.name
    
    Add-VDSwitchVMHost -VMHost $esxName -VDSwitch $vdsName1
    $esxihostnic1 = Get-VMHost hostdone1.examlab.com  | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.Mac -eq "$nic1_pnicmac1"}
    $pnicName = $esxihostnic1.name
    
    $uplinkName = $lag_name1