使用PowerShell在远程客户端上卸载软件

时间:2013-11-15 07:13:38

标签: powershell uninstall

我正在寻找一个脚本来帮助我在网络中的几个客户端上卸载某个软件。

现在我正在查看列表,远程访问客户端,使用我的管理员帐户登录并卸载软件,然后再退出并重复此过程。所有这些都是手动的,所以我希望你帮忙写一个PowerShell脚本来为我做这些事情。

可能出现的一些问题: 我无法远程登录,因为我无法与客户端建立连接。 另一个用户可能已经在客户端登录。 在我不知情的情况下,要卸载的软件实际上已经卸载了。

大约有900个客户,所以脚本真的会有所帮助。

此外,如果在脚本完成后,可以获得卸载软件的客户端列表以及不合适的客户端列表。

1 个答案:

答案 0 :(得分:1)

这样写的问题可能会引出“你试过什么”的类型回复......

我建议使用Windows Installer Powershell模块Uninstall-MSIProduct

我已在此帖子中描述了如何远程使用此模块:remote PCs using get-msiproductinfo,此示例使用Get-MSIProductInfo,但可以轻松更新以使用Uninstall-MSIProduct

我已快速更改此内容以使用Uninstall-MSIProduct,但尚未对其进行测试。

[cmdletbinding()]
param
(
    [parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipelineByPropertyName=$true)]
    [string]
    $computerName,
    [string]
    $productCode
)

begin
{
    write-verbose "Starting: $($MyInvocation.MyCommand)"

    $scriptFolder   = Split-Path -Parent $MyInvocation.MyCommand.Path
    $moduleName     = "MSI"
    $modulePath     = Join-Path -Path $scriptFolder -ChildPath $moduleName  

    $remoteScript   = {
        param($targetPath,$productCode)

        Import-Module $targetPath
        uninstall-msiproduct -ProductCode $productCode
    }

    $delayedDelete  = {
        param($path)
        Remove-Item -Path $path -Force -Recurse
    }
}
process
{
    $remotePath = "\\$computerName\c$\temp\$moduleName"

    write-verbose "Copying module to $remotePath"
    Copy-Item -Path $modulePath -Destination $remotePath -Recurse -Container -Force

    write-verbose "Getting installed products"
    Invoke-Command -ComputerName $computerName -ScriptBlock $remoteScript -ArgumentList "c:\temp\$moduleName", $productCode

    write-verbose "Starting job to delete $remotePath"
    Start-Job -ScriptBlock $delayedDelete -ArgumentList $remotePath | Out-Null
}

end
{
    write-verbose "Complete: $($MyInvocation.MyCommand)"
}
相关问题