【问题标题】:Launch msiexec as an admin through powershell通过 powershell 以管理员身份启动 msiexec
【发布时间】:2018-06-20 17:48:58
【问题描述】:

我在尝试编写有关执行 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

提前致谢!

【问题讨论】:

  • 谢谢,但如果你看看我的小代码块,我就会这样做。我认为我传递论点的方式;有一个我不确定的语法错误,因为它只是关闭了我。我看到一瞬间的警告,但不知道它在说什么。
  • 我认为你只需要从脚本块中取出-verb runas 位:Start-Process -FilePath "msiexec $FullFilePath" -Credential $adminCreds -ArgumentList "-noprofile -command &{Start-Process /i $FullFilePath /passive /norestart }" -WorkingDirectory $path -verb runas
  • 不幸的是,我得到了同样的结果。我得到了更多的信息。我最终记录了我的屏幕,同时它闪烁了警告消息以查看它在说什么,这是消息的内容:“警告:由于错误而无法执行此命令:系统找不到指定的文件。”我不确定为什么会这样说,因为当我在“启动过程”正上方打印变量时,一切都是正确的。路径打印到文件的路径,过滤器打印文件名和扩展名,FullFilePath 打印完整的文件路径。
  • -FilePath 接受一个可执行文件名/文件路径,而不是参数,所以像"msiexec $FullFilePath" 这样的东西不能工作。即使您解决了这个问题,您也会通过 -ArgumentList 传递用于 PowerShell 的参数,msiexec 不知道如何解释。
  • @mklement0 我修改了我的问题,希望现在更有意义,我也发布了答案。希望这对某人有所帮助。

标签: powershell credentials windows-installer runas start-process


【解决方案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

【讨论】:

    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多