Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#调用返回PSObject集合的方法_C#_Powershell - Fatal编程技术网

c#调用返回PSObject集合的方法

c#调用返回PSObject集合的方法,c#,powershell,C#,Powershell,我是vb的c#新手,很难弄清楚如何运行一个方法。 我的完整代码如下。我的问题是主要方法应该是什么。我从论坛获得了一些功能,这些功能应该可以让我远程执行exchange管理命令。我正在尝试使用远程exchange运行基本powershell命令,以查看是否可以使其正常工作 任何帮助都将不胜感激 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Thre

我是vb的c#新手,很难弄清楚如何运行一个方法。 我的完整代码如下。我的问题是主要方法应该是什么。我从论坛获得了一些功能,这些功能应该可以让我远程执行exchange管理命令。我正在尝试使用远程exchange运行基本powershell命令,以查看是否可以使其正常工作

任何帮助都将不胜感激

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;

namespace ExchPwrShellCmdletsTest
{
    class Program
    {
        static void Main(string[] args)
        {

        }

        public Collection<PSObject> GetUsersUsingBasicAuth(string liveIDConnectionUri, string schemaUri, PSCredential credentials, int count)
        {
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
                new Uri(liveIDConnectionUri),
                schemaUri, credentials);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

            using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                return GetUserInformation(count, runspace);
            }
        }

        public Collection<PSObject> GetUserInformation(int count, Runspace runspace)
        {
            using (PowerShell powershell = PowerShell.Create())
            {
                powershell.AddCommand("Get-Users");
                powershell.AddParameter("ResultSize", count);

                runspace.Open();

                powershell.Runspace = runspace;

                return powershell.Invoke();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Collections.ObjectModel;
使用系统、管理、自动化;
使用System.Management.Automation.Remoting;
使用System.Management.Automation.Runspaces;
命名空间EXCHPWSHELLCMDLETSTEST
{
班级计划
{
静态void Main(字符串[]参数)
{
}
公共集合GetUsersUsingBasicAuth(字符串liveIDConnectionUri、字符串schemaUri、PSCredential凭据、int计数)
{
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(
新Uri(liveIDConnectionUri),
方案、证书);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Basic;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
返回GetUserInformation(计数、运行空间);
}
}
公共集合GetUserInformation(int计数,运行空间运行空间)
{
使用(PowerShell PowerShell=PowerShell.Create())
{
powershell.AddCommand(“获取用户”);
powershell.AddParameter(“结果大小”,计数);
Open();
powershell.Runspace=运行空间;
返回powershell.Invoke();
}
}
}
}
公共集合GetUsersUsingBasicAuth(字符串liveIDConnectionUri、字符串schemaUri、PSCredential凭据、int count)
正在代码中定义新方法。因为它指定了
集合
,所以此方法的返回类型将是
集合
的形式。要调用该方法,请按名称引用它,然后传入任何可用的参数

i、 e

var coll=GetUsersUsingBasicAuth(“liveidconnectionurl”、“schemauri”、新PSCredentials(…),5)用于定义的第一个方法

您可以在以下内容中使用上述代码段(带有实际值):

static void Main(string[] args)
{
   var coll = GetUsersUsingBasicAuth("liveidconnectionurl","schemauri",new PSCredentials(...), 5 );
}

什么不起作用?你缺少一个主要方法。。。。没有什么可执行的。