使用 PowerShell 使用参数执行 msiexec uninstall

使用 PowerShell 使用参数执行 msiexec uninstall

我正在尝试使用 WinRM 从客户端卸载 Palo Alto Traps 防病毒软件。

我确实知道,静默卸载任务的正确命令是

msiexec /x c:\Traps_x64_4.2.3.41131.msi /quiet /l*v c:\traps_uninstall_log.txt UNINSTALL_PASSWORD=TimeToUninstall!

https://docs.paloaltonetworks.com/cortex/cortex-xdr/5-0/cortex-xdr-agent-admin/traps-agent-for-windows/troubleshoot-traps-for-windows/cytool

但是在 PowerShell 中,每次我尝试,它都会挂起。

我猜测它传递的这些参数是错误的,所以 msiexec 试图打开一个窗口但它不能,
因为我在 Enter-PSSession 下。

Start-Process "msiexec.exe" -ArgumentList "/x Y:\Traps_x64_4.2.3.41131.msi /quiet /l*v c:\traps_uninstall_log.txt UNINSTALL_PASSWORD=TimeToUninstall!" -Wait -PassThru

我需要逃避某些角色才能工作吗?

答案1

使用 Powershell 运行 exe 是一件有据可查的事情。正确引用是必需的。如何执行可执行文件是明智之举。

TechNet - PowerShell:运行可执行文件

在某些情况下,Start-Process 不是正确的过程。更明智的做法是只调用 installer/exe/msi 等。

按照上面的链接,当说针对 cmd.exe 时:

5. The Call Operator &
Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.
In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.
The PowerShell V3.0 parser do it now smarter, in this case you don’t need the & anymore.
Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters

Example:
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen
Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths!
With spaces, you have to nest Quotation marks and the result it is not always clear! 
In this case it is better to separate everything like so:

$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4

# or same like that:
 
$AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
 
& 'SuperApp.exe' $AllArgs

或者这篇关于 MSIExec 和使用 Start-Process 的好文章,采用同样的方法。

Powershell:安装 MSI 文件

$DataStamp = get-date -Format yyyyMMddTHHmmss
$logFile = '{0}-{1}.log' -f $file.fullname,$DataStamp
$MSIArguments = @(
    "/i"
    ('"{0}"' -f $file.fullname)
    "/qn"
    "/norestart"
    "/L*v"
    $logFile
)
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow 

不过你的问题很常见。请参阅此 SU 和 SO 问答。

使用 PowerShell 运行 msiexec

msiexec.exe /qb /I "C:\myInstaller.msi" INSTALLLOCATION=`"C:\Program Files\installFolder`" ALT_DOC_DIR=`"C:\Program Files\otherFolder`"

如何使用 PowerShell 安装 .MSI

相关内容