我努力尋找我的代碼有什么問題。我試圖在幾個 Windows 10 客戶端上卸載一個應用程式,其中 .exe 的路徑在任何機器上都不同。我Get-ChildItem用來獲取該特定檔案夾。現在我嘗試在最后使用 .exe 執行該路徑,并添加一個/qn觸發器以靜默卸載。但是,我總是收到有關該Invoke-Item功能的錯誤。
這是我的腳本:
首先我試圖在應用程式檔案夾中找到變數檔案夾名:
$path_to_exe = Get-ChildItem -Path "C:\Program Files\ApplicationName" -Include "installer.exe" -Recurse |
Where-Object { $_.FullName -match '\{(.*)\}' }
這給了我作為Get-ChildItem回應的路徑。
現在我將給出的路徑轉換Get-ChildItem為“正常”路徑
$path_to_exe = Convert-Path $Path_to_exe.PSPath
現在我嘗試呼叫我用/qn觸發器找到的路徑以靜默卸載。
"`"$path_to_exe`"" " /qn" | Invoke-Item
我 100% 確定我的方法是初學者一級。如果有人有更好的主意,請教育我。
謝謝 :)
編輯:
制造商宣告我應該使用以下內容:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1D9F5D88-12AA-427F-8A33-DED71D60E4D9} - MsiExec.exe /X{1D9F5D88-12AA-427F-8A33-DED71D60E4D9}
有人知道如何從Get-ChildItem注冊表查詢中提取該 guid嗎?
uj5u.com熱心網友回復:
get-package "*applicationname*" | uninstall-package
uj5u.com熱心網友回復:
我終于想通了!據我所知,我的方法是錯誤的。我正在嘗試將EXE檔案決議為單獨的 powershell 實體并添加引數變得復雜。我使用該msiexec函式重寫了我的整個代碼。
所以首先我需要使用該Get-ChildItem函式找出應用程式的 GUID 。
$Path_to_GUID = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Where-Object {$_.DisplayName -match "Application Name" } |
Select-Object -Property DisplayName, UninstallString
現在我有了該Get-ChildItem物件的所需路徑,以及該.UninstallString屬性。由于我只想要 GUID 本身,我使用該Split-Path函式按檔案夾名稱/GUID 分隔路徑,開頭有點像這樣:
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{xxxxxx-xxxx-xxxx-xxxx}\installer.exe
所以使用該Split-Path功能我首先只切割-Parent零件。
$Path_to_GUID = Split-Path -Path $Path_to_GUID.UninstallString -Parent
這給了我這樣的路徑,最后沒有 installer.exe:
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{xxxxxx-xxxx-xxxx-xxxx}
所以現在我只想要使用-Leaf引數的帶花括號的路徑部分:
$Path_to_GUID = Split-Path $Path_to_GUID -Leaf
這給了我想要的字串:
{xxxxxx-xxxx-xxxx-xxxx}
現在我只需要使用msiexec并添加silent引數來卸載:
msiexec /x $Path_to_GUID /qn
這是完整的代碼(Powershell):
$Path_to_GUID = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Where-Object {$_.DisplayName -match "Application Name" } |
Select-Object -Property DisplayName, UninstallString
$Path_to_GUID = Split-Path -Path $Path_to_GUID.UninstallString -Parent
$Path_to_GUID = Split-Path $Path_to_GUID -Leaf
msiexec /x $Path_to_GUID /qn
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372238.html
標籤:电源外壳
