2017-12-18 224 views
0

我想從具有參數的PowerShell腳本調用「PS App Deployment Toolkit」包(Link)。PowerShell啓動過程與潑濺

提到的「PS應用程序部署工具包」 - 包是一個powershell腳本,我想用參數調用。 (從.ps1調用.ps1)

我想使用splatting作爲參數。
我想等待腳本結束。
我想從腳本中獲取退出代碼。

這裏是我的代碼,這是行不通的:

$PSADToolKitInstallWrapper = "C:\Temp\MyPackage\PS-AppDeploy.ps1" 

$PSADToolKitParameters = @{ 
    "DeploymentType" = "Uninstall"; 
    "DeployMode" = "Interactive"; 
    "AllowRebootPassThru" = $True; 
    "TerminalServerMode" = $False; 
    "DisableLogging" = $False; 
}   

$InstallStatus = Start-Process -FilePath "PowerShell.exe" -ArgumentList $PSADToolKitInstallWrapper @PSADToolKitParameters -Wait -PassThru 

Write-Host "Exit-Code: $($InstallStatus.ExitCode)" 

此線將正常工作,但我想在上面的例子中,設置參數,如:

$InstallStatus = Start-Process -FilePath "PowerShell.exe" -ArgumentList "$PSADToolKitInstallWrapper","-DeploymentType Install -DeployMode Silent -AllowRebootPassThru -TerminalServerMode" -Wait -PassThru 

請你幫助我得到這個工作?

謝謝!

+0

這不是在你的例子中濺起。如果你擺脫了'@ PSADToolKitParameters',它將工作 – TheIncorrigible1

+0

...和@InIncorrigible1是正確的。您的示例不包含濺射。查看我的答案,瞭解如何使用濺射。 –

回答

1

我認爲你不需要這麼努力。爲什麼在PowerShell腳本中運行powershell.exe?您已經在運行PowerShell。只要運行所需的命令行:

$PSADToolKitParameters = @{ 
    "DeploymentType" = "Uninstall" 
    "DeployMode" = "Interactive" 
    "AllowRebootPassThru" = $True 
    "TerminalServerMode" = $False 
    "DisableLogging" = $False 
} 
C:\Temp\MyPackage\PS-AppDeploy.ps1 @PSADToolKitParameters 

如果你想運行的路徑和/或文件名的腳本包含空格,然後調用運營商(&)調用它,並且引用的文件名;例如:

& "C:\Temp\My Package\PS-AppDeploy.ps1" @PSADToolKitParameters 

檢查腳本的結果取決於腳本返回的內容。如果返回的輸出對象,那麼你可以簡單地給它分配:

$output = C:\Temp\MyPackage\PS-AppDeploy.ps1 ... 

如果腳本運行,設置退出代碼的可執行文件,檢查$LASTEXITCODE變量的值(這是類似於%ERRORLEVEL%動態變量在cmd.exe)。

+0

只是一個說明,他想要捕捉腳本的退出代碼 – TheIncorrigible1

+0

是的,腳本得到正確調用,但我沒有得到退出代碼... – Walhalla

+0

@Walhalla如果添加'$ Var =&.. .'你應該得到'$ Var'變量中的任何腳本。 – TheIncorrigible1