远程打开自定义远程 powershell

远程打开自定义远程 powershell

我有 2 台计算机。在计算机 A 上,我有一个用 C# 为 powershell 3.0 编写的自定义模块,并通过 MSI 安装。我还有一个快捷方式,可以打开已加载模块的 powershell。

我可以双击我的快捷方式并在这台计算机上运行我的命令 Do-Something 而不会出现任何问题,就像 Exchange Server powershell 一样。

但现在我想用 C# 从计算机 B 上的远程会话来执行此操作。

所以我的问题是,如何在已加载模块并配置好 shell 的情况下打开到计算机 A 的远程 powershell 会话,以便我可以运行我的命令并获得与在计算机 A 上运行相同的结果?

答案1

我终于找到了实现我需要的方法。

首先我创建了一个 powershell 脚本来导入我的模块和模块脚本文件

ShellInitScript

function Get-ScriptDirectory
{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}
$MyModuleDirectory = Get-ScriptDirectory
Set-Location $MyModuleDirectory 
Import-Module .\MyModule.psd1

我的模块.psd1

@{
GUID                = "[GENERATE A GUID HERE]" # Generate using $myGuid = [guid]::NewGuid() 

Author              = "Me"

CompanyName         = "My Company"

Copyright           = "© My Company. All rights reserved."

ModuleVersion       = "1.0.0.0"

ModuleToProcess     = "MyModule.psm1"
}

我的模块.psm1

Import-Module .\MyModuleCSharpAssembly.dll

function Enable-MyShellRemoting([switch]$Force)
{
    $proceed = 0

    if($Force -eq $false)
    {
        Write-Warning "Enable-MyShellRemoting restarts the WinRM service and all dependent services.`r`nAll WinRM sessions connected to Windows PowerShell session configurations are disconnected."

        $title = "Are you sure you want to perform this action?"
        $message = "Performing this operation will allow selected users to remotely run MyModule commands on this computer."

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes"

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"

        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $proceed = $Host.UI.PromptForChoice($title, $message, $options, 0) 
    }

    if($proceed -eq 0)
    {
        Enable-PSRemoting -Force

        if((Get-WmiObject Win32_ComputerSystem).PartOfDomain -eq $false){
            Set-Item WSMan:\localhost\Client\TrustedHosts *
            Restart-Service WinRM
        }

        $path = Join-Path -Path $MyModuleDirectory  -ChildPath "ShellInitScript.ps1"
        Register-PSSessionConfiguration -Name "MyShellUri" -StartupScript $path -Force
    }
}
function Disable-MyShellRemoting([switch]$Force, [switch]$DisablePSRemoting)
{
    $proceed = 0

    if($Force -eq $false)
    {
        Write-Warning "Disable-MyShellRemoting restarts the WinRM service and all dependent services.`r`nAll WinRM sessions connected to Windows PowerShell session configurations are disconnected."

        $title = "Are you sure you want to perform this action?"
        $message = "Performing this operation will prevent all users to remotely run MyModule commands on this computer.`r`nUse the the -DisablePSRemoting switch to disable all PowerShell remoting features."

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes"

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"

        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $proceed = $Host.UI.PromptForChoice($title, $message, $options, 0) 
    }

    if($proceed -eq 0)
    {
        if($DisablePSRemoting)
        {
            Disable-PSRemoting
        }

        if((Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue) -eq $null){
            Write-Host "The session configuration MyShellUri is already unregistered."
        }
        else {        
            Unregister-PSSessionConfiguration -Name "MyShellUri" -Force -ErrorAction Ignore
        }
    }
}

function Test-MyShellRemote()
{
    return "Test completed"
}

dll MyModuleCSharpAssembly.dll 只是一个包含自定义 Cmdlet 的常规 .Net 程序集。

问题的解决方案是在函数 Enable-MyShellRemoting 中,此方法启用 powershell 远程处理并使用加载模块的启动脚本注册自定义 Powershell 会话配置。

然后在 C# 中我们要做的就是在连接对象中指定 ShellUri,如下所示

WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.ComputerName = "MyComputer";
connectionInfo.ShellUri = "http://schemas.microsoft.com/powershell/MyShellUri";
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
     remoteRunspace.Open();
     using (PowerShell shell = PowerShell.Create())
     {
          shell.Runspace = remoteRunspace;
          shell.AddCommand("Test-MyShellRemote");
          Collection<PSObject> results = shell.Invoke();
          //the results collection contains the string return by the command
     }
     remoteRunspace.Close();
 }

相关内容