您现在的位置是:首页 > 文章详情

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

日期:2019-05-13点击:663

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

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

 

方法如下:

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

X86 Script:

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

X64 Script:

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

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

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

image

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

image

3、 完整的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”

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

image

5、 将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

6、 核准脚本:在刚创建的脚本上点右键、批准

image

在生产环境中,为了安全,用户创建的脚本需要第二人来批准(即自己不能批准自己创建的脚本),如果想自己批准自己的创建的脚本,请在进到:Administration\Site Configuration\Sites\Hierarchy Settings,

image

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

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

image

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

image

9、 也可以晚点再去软件报告里进行查询!

10、 如果针对某个软件是使用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中调用上面的脚本!

原文链接:https://blog.51cto.com/hubuxcg/2394045
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章