通过Powershell以管理员身份启动msiexec

时间:2018-06-20 17:48:58

标签: powershell credentials msiexec runas start-process

在执行MSI应用程序方面,我遇到了一段我想尝试工作的代码。 我需要传递存储在变量中的凭据,然后将这些凭据通过“ runas”传递给MSI软件包,以便使用我传递给应用程序的逐步升级的凭据进行安装。

这是我遇到问题的代码部分。

if($filter -like "*.msi")
{
    Start-Process -FilePath "msiexec $FullFilePath" -Credential $adminCreds -ArgumentList "-noprofile -command &{Start-Process /i $FullFilePath /passive /norestart -verb runas}" -Wait -WorkingDirectory $path
    exit
}

我的变量如下:

$filter = Get-ChildItem $path -Filter $($installer[$i]) -name
$FullFilePath = $path + "\" + $filter
$path = Split-Path $script:MyInvocation.MyCommand.Path

谢谢!

1 个答案:

答案 0 :(得分:1)

要解决我的问题,我创建了一个代码块,该代码块导出一个临时批处理脚本,以在从中提取脚本的文件夹中查找任何msi应用程序。运行脚本并安装应用程序后,脚本将自行删除。我直接通过批处理脚本而不是MSI应用程序传递凭据,但这可以满足我的需要。这是对我的脚本所做的更改。

$msiBatch = @"
@echo off
pushd "%~dp0"

::Get the MSI file name to install
for /f "tokens=1* delims=\" %%A in ( 'forfiles /s /m *.msi /c "cmd /c echo @relpath"' ) do for %%F in (^"%%B) do (set myapp=%%~F)

::Launch our installer
start /w "" msiexec /i "%~dp0%myapp%" /passive /norestart

::Self Delete
DEL "%~f0"
"@

运行方式如下:

if($filter -like "*.msi")
{
    $installPath = $path + "\msiInstaller.bat"
    $msiBatch | Out-File -Encoding Ascii -append $installPath
    $FullFilePath = $installPath

}

Start-Process -FilePath powershell.exe -Credential $adminCreds -NoNewWindow -ArgumentList "-noprofile -command &{Start-Process $FullFilePath -verb runas}" -Wait -WorkingDirectory $path        
break