tdubb123
Expert
Expert

params check if $null

I got a param script and need to get vmhost info

 

params($vc,$dc,$cluster,$vmhost)

if ($dc -or $cluster -eq $null) {

get-vmhost $vmhost | select Name, etc.....

}

else if ($cluster -and $vmhost -eq $null}

get-datacenter $dc | get-vmhost | select Name, etc....

 

else if ($vmhost -eq $null}

get-datacenter | get-cluster | get-vmhost | select Name etc....

 

Is this correct?

0 Kudos
1 Reply
LucD
Leadership
Leadership

If I understand what you are trying to do I would opt for parametersets.
Something like this

  [CmdletBinding(DefaultParameterSetName = 'Default')]
  param(
  [Parameter(ParameterSetName = 'Datacenter',
    HelpMessage = 'Enter a Datacenter name')]
  [string]$DC,
  [Parameter(ParameterSetName = 'Cluster',
    HelpMessage = 'Enter a Cluster name')]
  [string]$Cluster,
  [Parameter(ParameterSetName = 'VMHost',
    HelpMessage = 'Enter a VMHost name')]
    [string]$VMHost,
  [String]$vc
  )

  if ($PSCmdlet.ParameterSetName -eq 'Datacenter') {
    Get-Datacenter $dc | Get-VMHost | Select-Object Name
  }
  elseif ($PSCmdlet.ParameterSetName -eq 'Cluster') {
    Get-Cluster -Name $Cluster | Get-VMHost | Select-Object Name
  }
  elseif ($PSCmdlet.ParameterSetName -eq 'VMHost') {
    Get-VMHost $vmhost | Select-Object Name
  }
  else{
    Get-Datacenter | Get-Cluster | Get-VMHost | Select-Object Name
  }


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

0 Kudos