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

栏目: 编程工具 · 发布时间: 4年前

最近碰到一个需求:针对所有电脑卸载某个小软件,但这个软件并不是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

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

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

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

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:

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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中调用上面的脚本!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

网络传播学

网络传播学

吴风 / 中国广播电视出版社 / 2004-6-1 / 22.00元

本书把网络传播置于构型与解构的双重语境中,全面而深入地梳理了网络传播的概念、发展背景与现状、传播模式、传播物征、传播学意义,并从文化学、舆论学、政治学、心理学、符号学、法学、伦理学等视角,对网络传播对于国家民族进步、社会文明与个体发展等方面所带来的影响,作了理性审视。最后,作者指出网络传播在目前的新发展中,尚存在着侵犯个人隐私权、网络著作侵权、公共信息安全、网络色情、虚假信息等诸多的问题,对于这些......一起来看看 《网络传播学》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具