Я наконец-то нашел способ выполнить то, что мне было нужно.
Сначала я создал скрипт 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 - это обычная сборка .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();
}