我是vb的c#新手,很难搞清楚如何运行方法。 我的完整代码如下。我的问题是主要方法应该是什么。我从论坛中获得了应该让我远程执行交换管理命令的功能。我正在尝试使用远程交换运行基本的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();
}
}
}
}
答案 0 :(得分:0)
public Collection<PSObject> GetUsersUsingBasicAuth(string liveIDConnectionUri, string schemaUri, PSCredential credentials, int count)
正在代码中定义一个新方法。因为它指定了Collection<PSObject>
,所以此方法的返回类型将采用Collection<PSObject>
的形式。要调用方法,请按名称引用它,然后传入任何可用的参数。
即
var coll = GetUsersUsingBasicAuth("liveidconnectionurl","schemauri",new PSCredentials(...), 5 );
为您定义的第一个方法。
您可以在上面使用上面的代码段(带有实际值):
static void Main(string[] args)
{
var coll = GetUsersUsingBasicAuth("liveidconnectionurl","schemauri",new PSCredentials(...), 5 );
}