powershellmdt

Passing Parameters to a PowerShell Script in MDT 2013


I have a PowerShell script that takes an array as an input parameter:

Param( 
    Parameter(Mandatory=$true)]
    [System.String[]] $Adapters
)
Write-Output "Disabling network adapter(s)."
foreach ($adapter in $Adapters) {
    Disable-NetAdapterBinding -Name $adapter -ComponentID ms_tcpip6  
    Write-Host $adapter
}

I get the following error when I call the function from an MDT 2013 task sequence:

+ ..."E:\Deploy\Scripts\Disable-IPV6.ps1" -Adapters @(Teamed_NIC1, Teamed_...
    Missing argument in parameter list.

This is what my call to the function in MDT 2013 looks like:

PowerShell script: <code>%SCRIPTROOT%\Disable-IPV6.ps1</code>; Parameters: <code>-Adapters @("Teamed_NIC1", "Teamed_NIC2")</code>

I suspect that MDT is handling the quotation marks in an unexpected way.


Solution

  • @("Teamed_NIC1","Teamed_NIC2") is a PowerShell array. That construct is only recognized within PowerShell, but not by the environment from which you invoke the PowerShell script. The same applies if you remove the @().

    You cannot really pass array values to a parameter when invoking a PowerShell script from outside PowerShell. A common workaround is to pass the argument as a delimited string and split it:

    Param(
        Parameter(Mandatory=$true)]
        [String]$Adapters
    )
    $AdapterList = $Adapters -split ','
    foreach ($adapter in $AdapterList) {
        ...
    }
    

    with an invocation like this:

    %SCRIPTROOT%\Disable-IPV6.ps1 "Teamed_NIC1,Teamed_NIC2"
    

    Or you can drop the parameter definition and use the automatic variable $args:

    if (-not $args) { throw 'Missing argument.' }
    
    foreach ($adapter in $args) {
        ...
    }
    

    with an invocation like this:

    %SCRIPTROOT%\Disable-IPV6.ps1 "Teamed_NIC1" "Teamed_NIC2"