Execute Office 365 PS script concurrently from C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I got an issue when try to execute a Poweshell script from C# concurrently.
The goal is to run the Set-MsolUserPassword (from Office 365) concurrently.
Here my code:

<div style="color:black; background-color:white
<pre><span style="color:blue class Program
{
<span style="color:blue static <span style="color:blue void Main(<span style="color:blue string[] args)
{
Thread t1 = <span style="color:blue new Thread(UpdateOffice365Password);
Thread t2 = <span style="color:blue new Thread(UpdateOffice365Password);
t1.Start();
Thread.Sleep(100);
t2.Start();
Console.ReadKey();
}

<span style="color:blue static <span style="color:blue void UpdateOffice365Password()
{
<span style="color:blue string scriptText = <span style="color:#a31515 "import-module MSOnline;rn" +
<span style="color:#a31515 "$Username=" + <span style="color:#a31515 admin@mytest.com + <span style="color:#a31515 ";rn" +
<span style="color:#a31515 "$Password=ConvertTo-SecureString -AsPlainText "p4$$w0rd" -Force;rn" +
<span style="color:#a31515 "$Livecred = New-Object System.Management.Automation.PSCredential $Username, $Password;rn" +
<span style="color:#a31515 "Connect-MsolService -Credential $Livecred;rn" +
<span style="color:#a31515 "Set-MsolUserPassword -UserPrincipalName user@mytest.com -NewPassword "N3wP4$$w0rd" -ForceChangePassword $false -WarningVariable warningVar -OutVariable outVar | Out-Null;rn" +
<span style="color:#a31515 "If ( !$warningVar -and !$Error ) { " +
<span style="color:#a31515 " If ( $outVar -eq "N3wP4$$w0rd") { echo OK; } Else { echo Failed; } " +
<span style="color:#a31515 "} Else {" +
<span style="color:#a31515 " If ( $Error ) { echo ERROR; $Error | fl Message; } Else { echo WARNING; $warningVar | fl Message; }" +
<span style="color:#a31515 "}";

PowerShell psExec = PowerShell.Create();
psExec.AddScript(scriptText);
psExec.AddCommand(<span style="color:#a31515 "Out-String");

Collection<PSObject> results;
results = psExec.Invoke();
StringBuilder stringBuilder = <span style="color:blue new StringBuilder();
<span style="color:blue foreach (PSObject obj <span style="color:blue in results)
{
stringBuilder.AppendLine(obj.ToString());
}
Console.WriteLine(stringBuilder.ToString());
}
}
[/code]


<br/>
The 1st thread got below error:

<pre>Set-MsolUserPassword : You must call Connect-MsolService before calling any other Office 365 cmdlets.
At line:6 char:21
+ Set-MsolUserPassword <<<< -UserPrincipalName user@mytest.com -NewPassword "N3wP4$$w0rd" -ForceChangePassword $false -WarningVariable warningVar -OutVariable outVar | Out-Null;
+ CategoryInfo : OperationStopped: (:) [Set-MsolUserPassword], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.MicrosoftOnlineException,Microsoft.Online.Administration.Automation.SetUserPassword

Connect-MsolService : Exception of type Microsoft.Online.Administration.Automat

ion.MicrosoftOnlineException was thrown.
At line:5 char:20
+ Connect-MsolService <<<< -Credential $Livecred;
+ CategoryInfo : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException
+ FullyQualifiedErrorId : 0x80048882,Microsoft.Online.Administration.Automation.ConnectMsolService[/code]

While the 2nd thread successful executed without error.

Really appreciate any help.

Cheers,
Eddy
<br/>

View the full article
 
Back
Top