Local Copy Functionality

We use the Powershell App Deployment Toolkit to develop packages that can be deployed in our environment with or without SCCM.

One of the requirements we have is to be able to create a local cached copy of the package and execute from that location. This ensures the SOURCEDIR and location used for repairs is available locally.

This is easy to achieve with an additional script and or function in the Toolkit itself however we were wondering if this could be incorporated as a feature of the Toolkit.

Below is the (pretty basic) section of code we use in the Pre-Installation phase to copy the entire package folder locally and then execute Deploy-Application.ps1. This allows us to run the primary script from a network location and by specifying the -RunLocal parameter it copies the package folder to C:\Windows\Software, runs Deploy-Application.ps1 and terminates the primary script. The result is the application is installed from the locally cached copy. What this fails to accommodate is passing all the parameters or arguments used with the primary script.

If ($RunLocal) {
        [string]$Destination = (Join-Path -Path $envWinDir -ChildPath 'Software')
        $localFolder = (Get-Item -Path $scriptDirectory).Name
        Try {
            Show-InstallationProgress -StatusMessage "Copying source locally. Please wait..."
            Copy-File -Path "$scriptDirectory" -Destination "$Destination" -Recurse
            . "$Destination\$localFolder\Deploy-Application.ps1" "$DeploymentType" "$DeployMode"
        }
        Catch {
            $InstallPromptErrMsg = "There was an error in copying and executing the install locally. `n$(Resolve-Error)"
	        Write-Log -Message $InstallPromptErrMsg -Severity 3 -Source $appDeployToolkitName
	        Show-DialogBox -Text $InstallPromptErrMsg -Icon 'Stop'
        }
        ## Exit the script here as it will have already run from the local copy
        Exit-Script -ExitCode $mainExitCode
    }

Is this a feature you could consider incorporating into the Toolkit and/or can you advise on the best way to pass the parameters between the scripts?

Many thanks in advance

Make sure you use the [CmdletBinding()] tag and have a Param() block, even if it empty, to create an advanced function. This will allow you to then access an automatic variable called $PSBoundParameters. This stores all of the parameters and values that were passed to that function/script. You can pass these on to another function or script by using splatting: Execute-Script @PSBoundParameters.

Thanks for the advice - however I am still struggling with splatting the parameters to the function.

I have found that unless I define all the parameters in the destination function I cannot pass @PSBoundParameters without getting the error “A parameter cannot be found …”

Ideally I would like to pass all the parameters that were specified for the primary script to the function that would then pass them when executing the local copy of the script.

See if this method of enumerating parameters and passing them along works for you:

Look at line 6080 for another example.