Share via

Native cmdlets optional/dynamic arguments/parameters

Lanky Doodle 241 Reputation points
2023-07-21T11:23:46.1433333+00:00

Hi,

Instead of doing this:

$AddGW   = $True
$Adapter = Get-NetAdapter | ? { $_.InterfaceDescription -match "Intel" }

if( $AddGW -eq $True ) {
	$Adapter | New-NetIPAddress -IPAddress 1.1.1.10 -PrefixLength 24 -DefaultGateway 1.1.1.1
} else {
	$Adapter | New-NetIPAddress -IPAddress 1.1.1.10 -PrefixLength 24
} 

Can you do this - basically optional/dynamic parameters. Unfortunately Set-NetIPAddress doesn't have a -DefaultGateway parameter

$AddGW   = $True
$Adapter = Get-NetAdapter | ? { $_.InterfaceDescription -match "Intel" }
$Adapter | New-NetIPAddress -IPAddress 1.1.1.10 -PrefixLength 24 ( if $AddGW -eq $True ) { -DefaultGateway 1.1.1.1 }

Windows for business | Windows Server | User experience | PowerShell

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2023-07-21T21:10:17.1+00:00

Perhaps not exactly, but this way works for both versions of PowerShell:

$AddGW   = $True
$Adapter = Get-NetAdapter | Where-Object { $_.InterfaceDescription -match "Intel" }

$props = @{
    IPAddress = '1.1.1.10'
    PrefixLength = '24'
}
if ($AddGw){
    $props['DefaultGateway'] = '1.1.1.1'
}
$Adapter | New-NetIPAddress @props

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 45,241 Reputation points
    2023-07-24T14:57:24.82+00:00

    Hello Lanky,

    Thank you for your question and for reaching out with your question today.

    In PowerShell, you can use splatting to achieve optional/dynamic parameters for cmdlets. Splatting allows you to pass a collection of parameter names and their values as a single variable to a cmdlet. This way, you can conditionally include or exclude parameters based on your requirements.

    Here's how you can modify your code using splatting to make the -DefaultGateway parameter optional:

    
    $AddGW = $True
    
    $Adapter = Get-NetAdapter | Where-Object { $_.InterfaceDescription -match "Intel" }
    
    # Define the common parameters that are always used
    
    $parameters = @{
    
        IPAddress = "********"
    
        PrefixLength = 24
    
    }
    
    # Add the DefaultGateway parameter if $AddGW is true
    
    if ($AddGW) {
    
        $parameters.Add("DefaultGateway", "1.1.1.1")
    
    }
    
    $Adapter | New-NetIPAddress @parameters
    
    

    In this code, we first define a hashtable $parameters that includes the common parameters -IPAddress and -PrefixLength, which are always used. Then, we conditionally add the -DefaultGateway parameter to the hashtable if the variable $AddGW is $True. Finally, we use splatting (@parameters) to pass the parameters to the New-NetIPAddress cmdlet.

    With this approach, you can easily add or remove optional parameters without having to repeat the entire command. The code remains concise and easier to manage.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.