首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图悄悄卸载Python,但我得到了一个错误:“您不能调用空值表达式上的方法。”

试图悄悄卸载Python,但我得到了一个错误:“您不能调用空值表达式上的方法。”
EN

Stack Overflow用户
提问于 2022-10-17 19:33:08
回答 1查看 43关注 0票数 0

我正在尝试将卸载脚本组织起来,以删除python。因为这是发生在后端,我需要它卸载静默。我做错了什么?谢谢你提前提供帮助。

代码语言:javascript
复制
    $Programs = $SearchList | ?{$_.DisplayName -match ($ProgramName -join "|")}
Write-Output "Programs Found: $($Programs.DisplayName -join ", ")`n`n"

Foreach ($Program in $Programs)
{
If (Test-Path $Program.PSPath)
{
Write-Output "Registry Path: $($Program.PSPath | Convert-Path)"
Write-Output "Installed Location: $($Program.InstallLocation)"
Write-Output "Program: $($Program.DisplayName)"
Write-Output "Uninstall Command: $($Program.UninstallString)"

$UninstallString = $_.GetValue('UninstallString')
$isExeOnly = Test-Path -LiteralPath $UninstallString
if ($isExeOnly)
{
$UninstallString = "'$UninstallString'"
}
$UninstallString += '/quiet'

$Uninstall = (Start-Process cmd.exe -ArgumentList '/c', $Program.UninstallString -Wait -PassThru) 
<#Runs the uninstall command located in the uninstall string of the program's uninstall registry key, this is the command that is ran when you uninstall from Control Panel.
If the uninstall string doesn't contain the correct command and parameters for silent uninstallation, then when PDQ Deploy runs it, it may hang, most likely due to a popup.#>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-17 20:09:03

有几个问题:

  • 您错误地指的是$_而不是$_.GetValue('UninstallString')中的$Program,这很可能是导致您所看到的错误的原因。

代码语言:javascript
复制
- However, `$Program.GetValue('UninstallString')` _also_ doesn't work, because (as you state it in a later comment) `$Program` is of type `[pscustomobject]`, which doesn't have a method by that name.
代码语言:javascript
复制
- _If_ you obtained `$Program` via `Get-ItemProperty` (without restricting the result to specific values with `-Name`), you can access the `UninstallString` value data as follows:

$Program.UninstallString

  • $UninstallString = "'$UninstallString'"应该是

$UninstallString = "`"$UninstallString`"",因为cmd.exe只理解"..."的引用。

  • $UninstallString += '/quiet'应该是$UninstallString += ' /quiet',也就是说,在/quiet.

之前需要一个空格

  • 您在Start-Process调用中没有使用$UninstallString
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74102315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档