powershellazure-devopspowershell-dsc

Passing parameter using variable While generating DSC configuration


I am executing Powershell DSC script from Powershell. Below is the code snippet

Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
    param ($driveformat)
    cd c:/provisioning
    Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
    Install-Module -Name PSDscResources -Force
    Install-Module -Name xStorage -Force
    . .\DiskSetup.ps1
    disksconfig -outputpath C:\DataDiskSetting -driveFormat $driveFormat
    Start-DscConfiguration -Path C:\DataDiskSetting -Wait -Force -Verbose
} -ArgumentList ($driveformat)

While generating configuration I want to pass driveformat as variable "$driveFormat" instead of hardcoding like "NTFS". Somehow its not getting value of $driveformat any idea how we can solve this.


Solution

  • You can add a named Parameter $driveformat in your script. See below example:

    Param([String]$driveformat)
    
    Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
        param ($driveformat)
        ...
    } -ArgumentList ($driveformat)
    

    Then in the powershell task from pipeline, add -driveformat "NTFS" in the argument field. See below screenshot: (I defined a pipeline variable driveformat to hold the value "NTFS")

    enter image description here

    enter image description here

    Or, you can add an Argument (eg. $driveformat = $args[0]) in your scripts. See below:

    $driveformat = $args[0]
    
    Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
        param ($driveformat)
        ...
    } -ArgumentList ($driveformat)
    

    Then you can directly pass the variable ("NTFS") in the Arguments field of powershell task:

    enter image description here