HOWTO: csharp - powershell - call get-clusteredmailboxserverstatus with managed code.
// HOWTO: csharp - powershell - call get-clusteredmailboxserverstatus with managed code.
// Sample which calls get-clusteredmailboxserverstatus and returns a list of nodes.
//TODO:
//#1 - Create a C# winform application and add a button.
//#2 - Add namespace reference statements (using statements):
// //Need for handing things like SecureString types...
// using System.Security;
// // For calling commandlets:
// using System.Management.Automation;
// using System.Management.Automation.Host;
// using System.Management.Automation.Runspaces;
// //General collection handling:
// using System.Collections.Generic;
// using System.Collections.ObjectModel;
// #3 - Set a eference to the "System.Management.Automation.dll"
// This is installed by the Windows Powershell SDK.
// OK, some articles say the files are here in some articles:
// 32bit: C:\WINDOWS\SysWOW64\windowspowershell\v1.0
// 64bit: C:\WINDOWS\system32\windowspowershell\v1.0
// However, you will find it here after installing:
// C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0
//
// Here are some places to get whats neeed for powershell development.
// How to Download Windows PowerShell 1.0
// <https://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx>
// Note: Exchange will install Powershell itself, so you will not need to do this on an Exchange server.
//
// How to Install Windows PowerShell and Download the Windows PowerShell SDK
// <https://msdn2.microsoft.com/en-us/library/Bb204630.aspx>
//
// Microsoft® Windows® Software Development Kit for Windows Vista™ and .NET Framework 3.0 Runtime Components
// <https://www.microsoft.com/downloads/details.aspx?FamilyId=C2B1E300-F358-4523-B479-F53D234CDCCF&displaylang=en>
// Note: This is for an internet install.
//
// Here are some good articles to look over:
// Windows PowerShell SDK
// <https://msdn2.microsoft.com/en-us/library/ms714469.aspx>
// Using Exchange Management Shell Commands With Managed Code
// <https://msdn2.microsoft.com/en-us/library/bb332449.aspx>
//C# winform test stub for powershell calls
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics; // need for doing trace, etc
// Need for handing things like SecureString types...
using System.Security;
// For calling commandlets:
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
// General collection handling:
//using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace csharpapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//GetCMBS("");
GetCMBS("exchange111222");
}
public static void GetCMBS
(
string alias
)
{
ICollection<PSObject> results;
// Create a runspace. We can't use the RunspaceInvoke class this time
// because we need to get at the underlying runspace to explicitly
// add the commands.
RunspaceConfiguration rc = RunspaceConfiguration.Create();
PSSnapInException snapEx = null;
PSSnapInInfo info = rc.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.Admin",
out snapEx);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rc);
myRunSpace.Open();
// Create a pipeline...
Pipeline pipeLine = myRunSpace.CreatePipeline();
using (pipeLine)
{
// Create a command object so we can set some parameters
// for this command.
Command oCommand = new Command("get-clusteredmailboxserverstatus");
if (alias.Length != 0)
{
oCommand.Parameters.Add("identity", alias);
}
// Add the command we've constructed
pipeLine.Commands.Add(oCommand);
// Execute the pipeline and save the objects returned.
results = pipeLine.Invoke();
// Print out any errors in the pipeline execution
// NOTE: These error are NOT thrown as exceptions!
// Be sure to check this to ensure that no errors
// happened while executing the command.
if (pipeLine.Error != null && pipeLine.Error.Count > 0)
{
Trace.WriteLine("ERROR: There were pipeline errors...\n");
foreach (object item in pipeLine.Error.ReadToEnd())
{
Trace.WriteLine("Error: " + item.ToString() + "\n");
}
}
//PSObject//
// Print out the results of the pipeline execution
string[] aString;
int iCount = 0;
if (results != null && results.Count > 0)
{
Trace.WriteLine("RESULTS: \n");
foreach (PSObject ps in results)
{
if (ps.Members["OperationalMachines"].Value != null)
{
Trace.WriteLine( "--------------------------------------------------------------\n");
Trace.WriteLine("TypeNameOfValue: " + ps.Members["OperationalMachines"].TypeNameOfValue + "\n");
Trace.WriteLine("Name: " + ps.Members["OperationalMachines"].Name + "\n");
Trace.WriteLine("MemberType: " + ps.Members["OperationalMachines"].MemberType.ToString() + "\n");
if (ps.Members["OperationalMachines"].TypeNameOfValue == "System.String[]")
{
aString = (string[])ps.Members["OperationalMachines"].Value;
for (iCount = 0; aString.Length != iCount; iCount++)
{
Trace.WriteLine("(" + iCount.ToString() + ") " + aString[iCount]);
}
}
else
{
Trace.WriteLine("OperationalMachines: " + ps.Members["OperationalMachines"].Value + "\n");
}
}
Trace.WriteLine("--------------------------------------------------------------\n");
}
}
}
pipeLine = null;
myRunSpace.Close();
myRunSpace = null;
}
}
}
For further information on PowerShell, please visit the link below:
Links on Common PowerShell Automation Questions
Comments
- Anonymous
September 26, 2008
PingBack from http://hoursfunnywallpaper.cn/?p=8030