English 中文(简体)
遥控指挥
原标题:Error when running powershell command remotely

我想用遥控机器指挥权力。 这是我使用的方法(当地人:131 是因为我使用隧道去遥远的机器5985号港口):

  public string RunRemotePowerShellCommand(string command)
    {
           System.Security.SecureString password = new System.Security.SecureString();
            foreach (char c in _password.ToCharArray())
            {
                password.AppendChar(c);
            }

            string schema = "http://schemas.microsoft.com/powershell/Microsoft.Powershell";

            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false,
                "localhost", 131, "/wsman", schema, new PSCredential(_domain + @"" + _userName, password));

            using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                remoteRunspace.Open();
                using (PowerShell powershell = PowerShell.Create())
                {
                    powershell.Runspace = remoteRunspace;
                    powershell.AddCommand(command);
                    powershell.Invoke();

                    Collection<PSObject> results = powershell.Invoke();

                    // convert the script result into a single string
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (PSObject obj in results)
                    {
                        stringBuilder.AppendLine(obj.ToString());
                    }
                    return stringBuilder.ToString();
                }
            }
    }

我试图在指挥下行动:

D:FolderNamescriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4"

与此类似:

RunRemotePowerShellCommand(@"D:FolderNamescriptName.ps1 -action editbinding -component ""comp1"",""comp2"",""comp3"",""comp4""");

但我收到:

Error: System.Management.Automation.RemoteException: The term  D:FolderNamescriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4"  is not recognized as a name of cmdlet, function, script file, or operable program. Check the spelling of the name, or if the path is included, verify that the path is correct and try again.

这种方法用简单的指挥进行罚款,而我想要掌握的指挥在我用遥远的机器操作时ok。

提前感谢。

Regards, Dusan

问题回答

您需要使用<代码>功率标值()法,以补充你的指挥参数。 “AddCommand()”电话应仅指指挥:圆顶名称、功能名称、通向文字的道路等。 摘自:

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process");
ps.AddArgument("wmi*");
ps.AddCommand("Sort-Object");
ps.AddParameter("descending");
ps.AddArgument("id");

我有类似的要求。

我的解决办法是,在C#法典中设立一个权力职能,并像在权力分散的边远会议中使用。

using System;
using System.Management.Automation;

namespace PowerShellTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string func = @"function Test { Write-Host  hello  };";
            PowerShell ps = PowerShell.Create();
            ps.AddScript(func);
            ps.Invoke();
            ps.AddCommand("Test");
            ps.Invoke();
            Console.WriteLine("Successfully executed function");
            Console.ReadLine();
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签