【问题标题】:Send commands to remote server using powershell使用 powershell 向远程服务器发送命令
【发布时间】:2019-10-10 00:35:43
【问题描述】:

我是 powershell 脚本的新手

我想从本地计算机上卸载位于远程计算机上的软件 所以我正在使用

$computer =Read-Host -Prompt 'Input your server name or IP adress'
$user =Read-Host -Prompt 'Input your server username'
Invoke-Command -ComputerName $computer -ScriptBlock {  

Write-host -ForegroundColor Magenta "Please select the software you wish to uninstall..."


    $javaVer = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | select DisplayName, UninstallString, InstallLocation, InstallDate | out-gridview -PassThru



    write-host -ForegroundColor Yellow "The following software will be uninstalled:"




ForEach ($ver in $javaVer) {

    If ($ver.UninstallString) {

        $uninst = $ver.UninstallString
        & cmd /c $uninst /quiet /norestart
    }

}
} -credential $user

在我的本地机器上测试我得到了这个错误

Out-GridView ne fonctionne pas à partir d'une sessionà 距离。 + CategoryInfo : InvalidOperation : (Microsoft.Power...GridViewCommand:OutGridViewCommand) [Out-GridView] , NotSupportedException + FullyQualifiedErrorId:RemotingNotSupported,Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName : 192.168.1.200

谷歌翻译:

Out-GridView 在远程会话中不起作用。 + 类别信息: 无效操作:(Microsoft.Power ... GridViewCommand: OutGridViewCommand) [Out-GridView], NotSupportedException + FullyQualifiedErrorId:RemotingNotSupported, Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName: 192.168.1.200

但是如果我发送一个像ipconfig 这样的简单命令,它就可以工作

我做错了什么?

【问题讨论】:

  • Out-GridView cmdlet 要求处于交互式会话中。 Invoke-Command 使用的会话既在另一个系统上又是非交互式的。如果您需要用户交互,请将您的远程调用分成两部分并在本地 Out-GridView 显示中使用第一部分返回的信息。
  • @Lee_Dailey 对不起,我还在学习 powershell 脚本,你能修改代码并将其作为答案发布吗?
  • 您是否只想卸载特定的应用程序 - 也许是 java?您的 $JavaVer 变量名暗示了这一点...
  • @Lee_Dailey 不,我想从打开的列表中选择要卸载的软件或输入软件名称,但从列表中选择对我来说更容易
  • OK ...我认为我刚刚发布的 ANSWER 中的代码可以工作,但我无法完全测试,因为我只有一个系统 - 并且没有要卸载的应用程序。 [咧嘴]

标签: powershell


【解决方案1】:

由于您不能在远程、非交互式会话中运行 Out-GridView [您从 Invoke-Command 获得的内容],因此这会将过程分为几个步骤...

  • 询问目标系统名称
  • 设置两个 I-C 脚本块
  • 通过 I-C 和第一个脚本块获取远程安装的应用列表
  • 显示通过O-GV供用户选择应用程序
  • 显示要卸载的应用程序
  • 使用第二个脚本块通过第二次调用 I-C 来进行实际的远程卸载

请注意,卸载应用程序的实际命令已转换为扩展字符串。准备好真正执行此操作时,请删除 "& cmd /c $uninst /quiet /norestart" 周围的双引号。

此外,根本没有错误处理。 [咧嘴一笑]

# the following likely should include a test to see if the system actually exists
$ComputerName = Read-Host -Prompt 'Input your server name or IP adress '
#$user = Read-Host -Prompt 'Input your server username'
Write-Host ''

$ICScriptblock_One = {
    $HKLM_TargetList = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
        "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
        )
    # send the results to the calling system
    Get-ItemProperty -Path $HKLM_TargetList |
        Where-Object {
            # filter out items with blanks in the follwing properties
            $_.DisplayName -and
            $_.UninstallString
            } |
        Sort-Object -Property DisplayName |
        Select-Object -Property DisplayName, UninstallString, InstallLocation, InstallDate
    } # end >>> $ICScriptblock_One = {

$ICScriptblock_Two = {
    ForEach ($ATR_Item in $Using:AppsToRemove)
        {
        $uninst = $ATR_Item.UninstallString
        # remove the double quotes when ready to do this for real
        "& cmd /c $uninst /quiet /norestart"
        }
    } # end >>> $ICScriptblock_Two = {

$InstalledAppList = Invoke-Command -ComputerName $ComputerName -ScriptBlock $ICScriptblock_One

$OGVMessage = 'Please select the software you wish to uninstall...'
$AppsToRemove = $InstalledAppList |
    Out-GridView -Title $OGVMessage -OutputMode Multiple

write-host -ForegroundColor Yellow "The following software will be uninstalled :"
$AppsToRemove.DisplayName |
    Out-Host
Write-Host ''

Invoke-Command -ComputerName $ComputerName -ScriptBlock $ICScriptblock_Two

在屏幕上输出...

Input your server name or IP adress : localhost

The following software will be uninstalled :
Apple Software Update
Google Update Helper
Python 3.7.4 Utility Scripts (64-bit)
Speccy

& cmd /c MsiExec.exe /I{A30EA700-5515-48F0-88B0-9E99DC356B88} /quiet /norestart
& cmd /c MsiExec.exe /I{60EC980A-BDA2-4CB6-A427-B07A5498B4CA} /quiet /norestart
& cmd /c MsiExec.exe /I{16F74529-EDE0-4BBD-B2AF-89AF9C696EA8} /quiet /norestart
& cmd /c "C:\Program Files\Speccy\uninst.exe" /quiet /norestart

【讨论】:

  • 我正在 VLC 媒体播放器上进行测试,但没有卸载,我也没有得到相同的输出,这部分没有显示& cmd /c MsiExec.exe /I{A30EA700-5515-48F0-88B0-9E99DC356B88} /quiet /norestart & cmd /c MsiExec.exe /I{60EC980A-BDA2-4CB6-A427-B07A5498B4CA} /quiet /norestart & cmd /c MsiExec.exe /I{16F74529-EDE0-4BBD-B2AF-89AF9C696EA8} /quiet /norestart & cmd /c "C:\Program Files\Speccy\uninst.exe" /quiet /norestart
  • 如果您按原样运行代码...根本没有任何更改...并将您的本地系统设置为$ComputerName,是否会显示所选应用程序?最后两个文本块应显示选定的应用程序和运行的卸载命令。
  • 是的,完全没有改变,它给出了与你相同的输出
  • 我在我的问题中发布的代码卸载 msi 和非 msi 安装程序,例如 VLC 已成功卸载,你能看看你的代码和我发布的代码可能需要一些更改才能卸载所有的软件,
  • cmd /c "C:\Program Files (x86)\VideoLAN\VLC\uninstall.exe" /quiet /norestart 当我在 cmd 中编写它时它可以工作,而不是静默,但它会显示执行卸载过程的窗口
猜你喜欢
  • 2019-01-15
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
相关资源
最近更新 更多