Expand variables in arguments in PowerShell
I’m trying to write a PowerShell script that copies a temporary directory on the local PC, copies files and folders from server to the temporary directory and finally executes a setup installer with arguments.
The problem I’m facing is the Start-Process cmdlet is having problems passing through variables containing string paths.
function Install-AutoCAD_2024 {
# Define variables
$temp_dir="C:deploy_temp"
$application_name="AutoCAD 2024"
$source_path = "\serversoftwareAutodesk2024AutoCAD_2024"
$destination_path="$temp_dir$application_name"
$installer_path="$destination_pathimageInstaller.exe"
# Check directory exist
If (!(Test-Path $destination_path) -and (Test-Path $temp_dir)) {
Write-Host "Creating tempoary directory for '$application_name...'"
New-Item -Path "$temp_dir$application_name" -ItemType Directory -Force
} else {
Write-Host "Copying files. Please wait..."
Copy-Item -Path "$source_path" -Destination "$destination_path" -Recurse
Write-Host "Installing '$application_name'..."
Start-Process -FilePath "$installer_path" -NoNewWindow -Wait -ArgumentList "-i deploy --offline_mode -q -o ""$destination_pathimageCollection.xml"" --installer_version "1.40.0.2""
}
}
However, if I expand the variables under the Start-Proceas cmdlet to full strings then I’m able to install the application perfectly fine.
function Install-AutoCAD_2024 {
# Define variables
$temp_dir="C:deploy_temp"
$application_name="AutoCAD 2024"
$source_path = "\serversoftwareAutodesk2024AutoCAD_2024"
$destination_path="$temp_dir$application_name"
$installer_path="$destination_pathimageInstaller.exe"
# Check directory exist
If (!(Test-Path $destination_path) -and (Test-Path $temp_dir)) {
Write-Host "Creating tempoary directory for '$application_name...'"
New-Item -Path "$temp_dir$application_name" -ItemType Directory -Force
} else {
Write-Host "Copying files. Please wait..."
Copy-Item -Path "$source_path" -Destination "$destination_path" -Recurse
Write-Host "Installing '$application_name'..."
Start-Process -FilePath 'C:deploy_tempAutoCAD 2024imageInstaller.exe' -NoNewWindow -Wait -ArgumentList "-i deploy --offline_mode -q -o 'C:deploy_tempAutoCAD 2024imageCollection.xml' --installer_version '1.40.0.2'"
}
}
I’ve tried surrounding each parameter with single and double quotes and I’ve also tried placing backtifks between the double quotes to escape the quotes and nothing works.