我终于找到了实现我所需要的方法。
首先,我创建了一个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
MyModule.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"
}
MyModule.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远程处理,并使用加载模块的statup脚本注册自定义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();
}