[英]c# powershell collection<PsObject> query with linq lambda expression
[英]c# call method that returns collection of PSObject
我是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();
}
}
}
}
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 );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.