Hi everyone,
I want to execute some powershell commands in an exchange management shell and found the following code snipped to do that:
I altered the second one the get me the output of the shell but i'm doing something wrong here. If the powershell output is small I sometimes get a fraction of the output (which means at least the command gets executed) in my application, however larger outputs create "nonsense" like this
You can see
System.Management.Automation.RemoteException: "Out-String" wasn't found as a cmdlet, function or scriptfile.
Moreover, I don't understand why I should convert the output to a string if I put it in an PSObject Collection afterwards.
Do I have to add Out-String? If so anybody got an Idea why I get that not found error?
Or is my approach completely wrong?
Sorry for any dumb questions and dirty code C# is fairly new for me, working with C# in connection with powershell completely new.
I want to execute some powershell commands in an exchange management shell and found the following code snipped to do that:
C#:
private static Runspace OpenExchangeManagementRunspace(string url,AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.Kerberos,string userName = null,string password = null)
{
Runspace runspace = null;
WSManConnectionInfo shellConnection = null;
PSCredential shellCred = null;
int keylenght = 32;
byte[] intBytes = BitConverter.GetBytes(keylenght);
Array.Reverse(intBytes);
byte[] keybyte = intBytes;
// Username and password is optional when using Kerberos authentication
if (!string.IsNullOrWhiteSpace(userName) &&
!string.IsNullOrWhiteSpace(password))
{
var securePassword = ConvertToSecureString(password,keybyte);
shellCred = new PSCredential(userName, securePassword);
}
shellConnection = new WSManConnectionInfo(new Uri(url), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", shellCred);
shellConnection.AuthenticationMechanism = authenticationMechanism;
// This will take some time
runspace = RunspaceFactory.CreateRunspace(shellConnection);
runspace.Open();
return runspace;
}
C#:
private void button1_Click_1(object sender, EventArgs e)
{
//https://stackoverflow.com/questions/57326768/how-can-i-start-a-script-in-exchange-management-shell-from-c
using (Runspace remoteRunspace = OpenExchangeManagementRunspace("http://domain/powershell?serializationLevel=Full;clientApplication=MyAppName"))
using (PowerShell shell = PowerShell.Create())
{
shell.Runspace = remoteRunspace;
Pipeline pipeline = remoteRunspace.CreatePipeline();
pipeline.Commands.AddScript("Get-MailboxFolderStatistics user");
//pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject pSObject in results)
stringBuilder.AppendLine(pSObject.ToString());
textBox1.Text = stringBuilder.ToString();
}
I altered the second one the get me the output of the shell but i'm doing something wrong here. If the powershell output is small I sometimes get a fraction of the output (which means at least the command gets executed) in my application, however larger outputs create "nonsense" like this
C#:
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
Microsoft.Exchange.Management.Tasks.MailboxFolderConfiguration
......
You can see
//pipeline.Commands.Add("Out-String");
in the second code block. I've seen many people using "Out-String" in similar code but enabling it gets me :System.Management.Automation.RemoteException: "Out-String" wasn't found as a cmdlet, function or scriptfile.
Moreover, I don't understand why I should convert the output to a string if I put it in an PSObject Collection afterwards.
Do I have to add Out-String? If so anybody got an Idea why I get that not found error?
Or is my approach completely wrong?
Sorry for any dumb questions and dirty code C# is fairly new for me, working with C# in connection with powershell completely new.