I am new to c# from vb and having a hard time figuring out how to run a method. My full code is below. My question is what should be in the main method. I got the functions from forums that is supposed to let me remote execute exchange admin commands. I am trying to run basic powershell command with remote exchange to see if I can get this to work.
Any help would be greatly appreciated.
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)
is defining a new method in your code. Because it specified Collection<PSObject>
your return type of this method will be in the form of a Collection<PSObject>
. To call the method you reference it by name, then pass in any usable parameters.
ie
var coll = GetUsersUsingBasicAuth("liveidconnectionurl","schemauri",new PSCredentials(...), 5 );
for your first method defined.
You would use that above code snippet (with actual values) inside of:
static void Main(string[] args)
{
var coll = GetUsersUsingBasicAuth("liveidconnectionurl","schemauri",new PSCredentials(...), 5 );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.