使用Powershell将命令发送到远程服务器

时间:2019-10-10 00:35:43

标签: powershell

我是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

在我的本地计算机上对其进行测试

  

在距离外的Grid View上保持距离。       + CategoryInfo:InvalidOperation:(Microsoft.Power ... GridViewCommand:OutGridViewCommand)[Out-GridView]   ,NotSupportedException       + FullyQualifiedErrorId:RemotingNotSupported,Microsoft.PowerShell.Commands.OutGridViewCommand       + PSComputerName:192.168.1.200

google翻译:

  

Out-GridView在远程会话中不起作用。 + CategoryInfo:   InvalidOperation:(Microsoft.Power ... GridViewCommand:   OutGridViewCommand)[Out-GridView],NotSupportedException +   FullyQualifiedErrorId:RemotingNotSupported,   Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName:   192.168.1.200

但是,如果我发送了一个简单的命令,例如ipconfig,它就会起作用

我做错了什么?

1 个答案:

答案 0 :(得分:0)

由于您无法在远程的非交互式会话中运行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