通过PowerShell在远程计算机上的服务器上执行卸载安装

时间:2019-10-06 21:47:20

标签: powershell

这是我的第一个问题,在PowerShell上我也很陌生,所以我希望一切都很好。

我的问题如下:我想在多台计算机上卸载程序,检查注册表项是否已删除,然后安装该程序的新版本。

该设置位于与计算机位于同一域中的服务器上。

我希望我的脚本在计算机之间循环并为每台计算机从服务器执行设置。由于我是PowerShell的新手,所以我不知道该怎么做。我当时在考虑使用Copy-Item,但我不想真正移动设置,而只是将其从服务器执行到计算机上吗?知道怎么做吗?

最诚挚的问候

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法。

请注意,对于臭名昭著的double-hop problem,需要明确提供凭据是一种解决方法。

# The list of computers on which to run the setup program.
$remoteComputers = 'computer1', 'computer2' # ...

# The full UNC path of the setup program.
$setupExePath = '\\server\somepath\setup.exe'

# Obtain credentials that can be used on the
# remote computers to access the share on which 
# the setup program is located.
$creds = Get-Credential

# Run the setup program on all remote computers.
Invoke-Command -ComputerName $remoteComputers {

    # WORKAROUND FOR THE DOUBLE-HOP PROBLEM:
    # Map the target network share as a dummy PS drive using the passed-through
    # credentials.
    # You may - but needn't - use this drive; the mere fact of having established
    # a drive with valid credentials makes the network location accessible in the
    # session, even with direct use of UNC paths.
    $null = New-PSDrive -Credential $using:cred dummy -Root (Split-Path -Parent $using:$setupExePath) -PSProvider FileSystem

    # Invoke the setup program from the UNC share.
    & $using:$setupExePath

    # ... do other things

} 

答案 1 :(得分:0)

只要服务器位于同一域中,就可以通过完成以下操作来实现此目的。

$app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "Software Name" 
}

$app.Uninstall()

要将其定向到远程服务器/计算机

Invoke-Command -FilePath <FilePath> -ComputerName <hostname>

这将从计算机上卸载软件,并允许您根据需要将其定向到主机名。

从这里只需将其放入嵌套循环中,并在.csv中添加计算机名称即可循环浏览。

Foreach($Computer in $Computers){

Invoke-Command -FilePath <FilePath> -ComputerName $Computer

}
相关问题