c#中的Get-MsolUser命令错误

本文关键字:错误 命令 Get-MsolUser 中的 | 更新日期: 2023-09-27 18:05:49

我正在尝试执行一段Azure脚本,以检查用户对象是否从本地AD同步到Azure,如下所示。

username遵循UPN的模式。示例:john.Smith@ed.com

 //Check Azure to see if user is synced to office 365
        private static bool IsAccountSyncedToOffice365(string username)
        {
            StringBuilder cmd = CreateAzureConnectScript();
            //cmd.AppendLine("Get-MsolUser -UserPrincipalName " + username + " -ErrorAction SilentlyContinue");
            cmd.AppendLine("$global:res = '"false'"");
            cmd.AppendLine("$global:user = '"false'"");
            cmd.AppendLine("try{ if(($global:user=Get-MsolUser -UserPrincipalName " + username + " -ErrorAction Stop).ImmutableId -ne $null) { $global:res = '"true'"} } Catch { $global:errorMessage = $_.Exception.Message}");
            try
            {
                Collection<PSObject> results;
                string output, error, errorMessageAzureCnn = "";
                do
                {
                    results = null;
                    output = "";
                    error = "";
                    var rs = CreateAzureRunspace();
                    var pipe = rs.CreatePipeline();
                    pipe.Commands.AddScript(cmd.ToString());
                    results = pipe.Invoke();
                    output = (rs.SessionStateProxy.PSVariable.GetValue("res")) != null ? rs.SessionStateProxy.PSVariable.GetValue("res").ToString() : "false";
                    error = (rs.SessionStateProxy.PSVariable.GetValue("errorMessage")) != null ? rs.SessionStateProxy.PSVariable.GetValue("errorMessage").ToString() : "null";
                    errorMessageAzureCnn = (rs.SessionStateProxy.PSVariable.GetValue("errorMessageAzureCnn")) != null ? rs.SessionStateProxy.PSVariable.GetValue("errorMessageAzureCnn").ToString() : "null";
                    ExceptionManager.Publish(new Exception("LOG: Queried Azure at:" + DateTime.Now + " for user:" + username + " Result: " + output + " Error: " + error + " errorMessageAzureCnn: " + errorMessageAzureCnn));
                    Thread.Sleep(60000); //sleep for 60 seconds
                    pipe.Dispose();
                    rs.Close();
                    rs.Dispose();
                } while (output.Trim().ToLower() != "true");
                ExceptionManager.Publish(new Exception("LOG: " + username + " is found synced to Azure at: " + DateTime.Now));
                cmd.Clear();
                return true;
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(new Exception("Error checking Azure to see if the user is synced to office 365 or not.. " + ex.Message));
                throw ex;
            }
        }

private static StringBuilder CreateAzureConnectScript()
{
    StringBuilder ss = new StringBuilder();
    MSCredential cred = new MSCredential();
    var username = cred.username;
    var pwd = cred.password;
    try
    {
        ss.AppendLine("try {");
        ss.AppendLine("$password = ConvertTo-SecureString '"" + pwd + "'" -AsPlainText –Force");
        ss.AppendLine("$credential = New-Object  System.Management.Automation.PsCredential('"" + username + "'",$password)");
        ss.AppendLine("$cred = Get-Credential -cred $credential");
        ss.AppendLine("Import-Module MSOnline");
        ss.AppendLine("Start-Sleep -s 10");
        ss.AppendLine("Connect-Msolservice -cred $cred");
        ss.AppendLine("} Catch { $global:errorMessageAzureCnn = $_.Exception.Message }");
        //ExceptionManager.Publish(new Exception("LOG:pwd: " + pwd + " uname:" + username));
        return ss;
    }
    catch (Exception ex)
    {
        ExceptionManager.Publish(new Exception("Error enabling the remote mailbox.. " + ex.Message));
        throw ex;
    }
}

当脚本通过Powershell窗口在安装了所有最新版本模块的同一服务器上成功执行时。当试图从c#代码中执行相同的命令时,它会抛出以下从powershell异常处理$global:errorMessage = $_.Exception.Message中收集的异常。

Show Details Exception (0): [] LOG:查询Azure at:7/30/2015 12:00:55 PM for user:testuser0385@xxx.com Result: false Error: You must call the Connect-MsolService cmdlet before calling any other cmdlets。errorMessageAzureCnn: null

值得一提的是,我已经在一台服务器上获得了与下面相同的代码,但它在生产服务器(Windows server 2008 R2数据中心)上抛出了下面的错误,并且只有通过代码才会发生。通过powershell窗口,它工作得非常好。

很高兴知道你的想法,什么是错误的,什么是需要调查的。

谢谢!

c#中的Get-MsolUser命令错误

这表明登录失败,但您仍在继续使用Get-MsolUser。如果Connect-MsolService cmdlet无法与Microsoft在线服务登录助手通信,则该cmdlet失败。裁判:https://community.office365.com/en-us/f/156/t/252201

生产服务器已经安装了所有的先决条件:Microsoft Online Services Sign-In Assistant和。net 3.5吗?我们在生产(Azure PAAS)中遇到了一个问题,客户操作系统映像自动更新,缺少。net 3.5,这破坏了我们的Azure AD PowerShell进程。