gpt4 book ai didi

powershell - 无法运行 PowerShell 卸载脚本

转载 作者:行者123 更新时间:2023-12-01 13:29:37 26 4
gpt4 key购买 nike

我正在尝试启动一个进程并等待退出代码。该进程使用参数列表启动 msiexec。当我运行我的脚本时,它会出现参数帮助向导,但是如果我直接在生成的 CMD 中运行命令:

write-host $command

它按预期工作。这是我的完整脚本:

# Get uninstall strings from registry, looking for the msiexec option
$applist = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Where-Object {$_.DisplayName -match "Microsoft Visio Standard 2013" -and $_.UninstallString -match "msiexec"} |
Select-Object -Property DisplayName, UninstallString

# Check for any aplications requiring uninstall and output their names
if ($applist) {
foreach ($app in $applist) {
Write-host "$($app.DisplayName) has been detected for uninstall"
}

Write-host "Attempting to uninstall application(s)"

# Uninstall each application that has been identified
foreach ($app in $applist) {
try {
$uninst = $app.UninstallString
$pos = $uninst.IndexOf(" ")
$leftPart = $uninst.Substring(0, $pos)
$rightPart = $uninst.Substring($pos+1)
$command = """$rightPart /qn /L*V ""C:\UninstallVisio.txt"""""
write-host $command
$uninstall = (Start-Process "msiexec.exe" -ArgumentList $command -Wait -Passthru).ExitCode

if($uninstall.ExitCode -ne 0) {
write-host "attempting XML config uninstall"
#**still to be worked on**
}
} catch {
write-host "Unable to uninstall $_.Name Please view logs"
Continue
}
}
}
Exit
# Exit script as no apps to uninstall
else {
write-host "No application(s) detected for uninstall"
Exit
}

最佳答案

而不是这个

$command = "$uninst /qn /L*V ""C:\UninstallVisio.txt"""

尝试

$command = @(
$uninst
"/qn"
"/L*V"
'"C:\UninstallVisio.txt"'
)

来源:见最后一个例子 https://kevinmarquette.github.io/2016-10-21-powershell-installing-msi-files/

关于powershell - 无法运行 PowerShell 卸载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46663935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com