3

カスタム コマンドレットを C# のリモート パワーシェルの実行空間に追加しようと何日も試みてきました

string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
        System.Security.SecureString sString = new System.Security.SecureString();
        foreach(char passwordChar in password.ToCharArray())
        {
            sString.AppendChar(passwordChar);
        }
        PSCredential credential = new PSCredential(username, sString);
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, computerName, 5985, "/wsman", shellUri, credential);
        try {
            using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                runspace.Open();
                pipe = runspace.CreatePipeline();
                pipe.Commands.AddScript(script);
                    Collection<PSObject> result = pipe.Invoke();
                    foreach (PSObject line in result)
                    {
                        scriptOutput.Add(line.ToString());
                    }
                pipe.Dispose();
                runspace.Close();
            }

リモートマシンでスクリプトを実行するために機能するこのようなものがあります。しかし、カスタム コマンドレットを追加する必要があります。

私はあなたがこのように InitialSessionState を使うことができるローカルパワーシェルで知っています:

InitialSessionState iss = InitialSessionState.CreateDefault();
        SessionStateCmdletEntry gfv = new SessionStateCmdletEntry("Get-Custom", typeof(GetCustomCommand), null);
        iss.Commands.Add(gfv);
        using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))

GetCustomCommand は PSCmdlet を拡張します。

ただし、リモート PowerShell では、InitialSessionState で実行空間を初期化できません。リモート実行空間に SessionStateCmdLetEntry を追加するにはどうすればよいですか?

誰かが私を助けることができますか?ありがとう

4

0 に答える 0