从SCCM中建立并运行Powershell脚本卸载软件

最近碰到一个需求:针对全部电脑卸载某个小软件,但这个软件并非SCCM部署,有的是用户本身安装的,有的是系统部署时就已经封装好的,版本繁多,安装路径也不同!shell

首先想到的固然是用Powershell来作,前后测试了用Get-apppacke\get-appxpacke, Get-WmiObject -Class win32_product等几种方法都不行,最后用找注册表中的UninstallString的方式解决!express

 

方法以下:安全

一、 先用PowerShell定位到注册表位置,app

X86 Script:ide

Set-Location HKLM:\software\microsoft\Windows\Currentversion\uninstall测试

X64 Script:ui

Set-Location HKLM:\software\WOW6432Node\microsoft\Windows\Currentversion\uninstall操作系统

二、 查询到软件安装后在注册表Uninstall中的名称,如:Chrome,在Uninstall中的Childitem名为:Google Chrome,其中Uninstallstring有卸载的运行文件具体路径、此文件名及参数:3d

"C:\Program Files (x86)\Google\Chrome\Application\74.0.3729.131\Installer\setup.exe" --uninstall --system-level --verbose-loggingblog

image

这就是咱们要运行的卸载命令,有些软件只有执行文件,并不带参数,如Teamviewer 只有一个Uninstall.exe,咱们可使用Uninstall.exe /S进行静默卸载!

image

三、 完整的PS脚本以下:

Set-Location

HKLM:\software\WOW6432Node\microsoft\Windows\Currentversion\uninstall

#指定注册表的位置

$UninstallTeamviewer =  get-childitem -path *Teamviewer* | get-itemproperty | %{$_.Uninstallstring}

#查找卸载命令并赋值给变量:$UninstallTeamviewer

start $UninstallTeamviewer /S

#Powershell中运行卸载命令:$UninstallTeamviewer

针对注册表中已有卸载程序和参数的,上面不用再加参数数:“/S”

四、 建立脚本:在SoftWare Library\Overview\Scripts中右键、Create Script:

image

五、 将PowerShell脚本贴到脚本,下一步,完成!

TeamViewer

$OS = (Get-WmiObject Win32_OperatingSystem).OSArchitecture

    #检查操做系统版本

if ((gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "32-bit"){

    #32 Bit 运行如下步骤

Set-Location HKLM:\software\microsoft\Windows\Currentversion\uninstall

    #指定如下注册表位置

$UninstallTeamviewer = get-childitem -path *Teamviewer* | get-itemproperty | %{$_.Uninstallstring}

    #查找卸载命令并赋值给变量:$UninstallTeamviewer

start $UninstallTeamviewer /S

    #Powershell中运行卸载命令:$UninstallTeamviewer

$UninstallTeamviewer

    #输出变量结果,方便查询分析

}

else{

    #64 Bit 运行如下步骤

Set-Location HKLM:\software\WOW6432Node\microsoft\Windows\Currentversion\uninstall

    #指定如下注册表位置

$UninstallTeamviewer = get-childitem -path *Teamviewer* | get-itemproperty | %{$_.Uninstallstring}

    #查找卸载命令并赋值给变量:$UninstallTeamviewer

start $UninstallTeamviewer /S

    #Powershell中运行卸载命令:$UninstallTeamviewer

$UninstallTeamViewer

    #输出变量结果,方便查询分析

}

image

六、 核准脚本:在刚建立的脚本上点右键、批准

image

在生产环境中,为了安全,用户建立的脚本须要第二人来批准(即本身不能批准本身建立的脚本),若是想本身批准本身的建立的脚本,请在进到:Administration\Site Configuration\Sites\Hierarchy Settings,

image

将“Script authors require additional script approver前面的勾取消。

七、 运行脚本:在须要运行的电脑或集全中点右键,选择:Run Script\Next,即开始运行。

image

八、 在Monitoring中也能够查看已运行的脚本汇总结果 ,在“脚本状态”列表中,能够查看在客户端设备上运行的每一个脚本的结果。 脚本退出代码为“0”一般表示脚本已成功运行。

image

九、 也能够晚点再去软件报告里进行查询!

十、 若是针对某个软件是使用MIS封装包安装的,Uninstall值会是“MsiExec.exe /I {23170F69-40C1-2701-1602-000001000000}“,就要改另外一种PowerShell方式,例:

if ((gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "32-bit"){

#32 Bit 7zip X32 1602 {23170F69-40C1-2701-1602-000001000000}

$script = { invoke-expression "msiexec /qn /x '{23170F69-40C1-2701-1602-000001000000}' "}

Invoke-Command -ScriptBlock $script

$script

}else{

#64 Bit 7zip X64 1602 {23170F69-40C1-2702-1602-000001000000}

$script = { invoke-expression "msiexec /qn /x '{23170F69-40C1-2702-1602-000001000000}' "}

Invoke-Command -ScriptBlock $script

$script

}

固然,若是没有SCCM环境,也能够尝试用在AD中调用上面的脚本!