c# - Error connect to exchange online using of Office365 a/c whencreated property get-mailbox cmdlet -


error : winrm client cannot complete operation within time spe cified. check if machine name valid , reachable on network , firewall exception windows remote management service enabled. error number: -2144108250

ps1 code:

param ( [parameter(mandatory=$true)][system.management.automation.pscredential]$credentials )  $session = new-pssession -configurationname microsoft.exchange -connectionuri https://outlook.office365.com/powershell-liveid/ -credential $credentials -authentication basic –allowredirection import pssession $session get-mailbox 

c# code:

pscredential credential; private runspace runspace; private string username = configurationmanager.appsettings["office365username"]; private string password = configurationmanager.appsettings["office365password"];  internal pshelper() {     //create object of pscredentials class     credential = new pscredential(this.username, createsecurepassword(this.password));     initialsessionstate sessionstate = initialsessionstate.createdefault();     sessionstate.importpsmodule(new[] { "msonline" });     initialsessionstate session = initialsessionstate.createdefault();     sessionstate.importpsmodule(new[] { "pssession" });     //create new runspace     runspace = runspacefactory.createrunspace(sessionstate);     runspace.open(); }  public getrecentuserscountresponse getrecentusercount() {     getrecentuserscountresponse response = new getrecentuserscountresponse();     try     {      int countvalue = 0;     datetime begindateforweek;     datetime enddateforweek;     string script = readpowershellscript("getrecentusercount.ps1");     command cmd = new command(script, true);     cmd.parameters.add(new commandparameter("credentials", credential));     pipeline pipeline = runspace.createpipeline();     pipeline.commands.add(cmd);     pipeline.input.close();     collection<psobject> collectionpsobject = pipeline.invoke();     if (collectionpsobject.count > 0)     {         foreach (psobject itemuser in collectionpsobject)         {             //check if user if licensed,isactive,whencreated             if (itemuser.properties["islicensed"] != null && itemuser.properties["isactive"] != null && itemuser.properties["whencreated"] != null)             {                 if (convert.toboolean(itemuser.properties["islicensed"].value) && convert.toboolean(itemuser.properties["isactive"].value) && itemuser.properties["whencreated"] != null)                 {                     begindateforweek = datetime.now;                     enddateforweek = convert.todatetime(itemuser.properties["whencreated"].value);                     timespan differenceofweekdate = begindateforweek - enddateforweek;                     int differnceofdays = convert.toint32(differenceofweekdate.days);                     //count if  created users last 7 days                     if (differnceofdays <= 30)                     {                         countvalue++;                     }                  }              }         }     }     pipeline.stop();     if (pipeline.error.count > 0)     {         stringbuilder builder = new stringbuilder();          foreach (object item in pipeline.error.readtoend())         {             builder.append(item.tostring());             builder.append(" - ");         }         response.errormessage = builder.tostring();     }      if (countvalue <= 7)     {         response.recentusercountweek = countvalue;     }      if (countvalue <= 30)     {         response.recentusercountmonth = countvalue;     }     }      catch (exception ex)     {     response.errormessage = ex.message;     }         {     runspace.dispose();     }     //return response;     return response; } 

i want connect exchange , return recent users past 7 , 30 days.

i've developed powershell script run this. can change whencreated attribute date attribute if needed. run commands remote in separately these commands

$cred = get-credential (enter full email address , password) $session = new-pssession -configurationname microsoft.exchange    -connectionuri https://ps.outlook.com/powershell/    -credential $cred -authentication basic -allowredirection import-pssession $session 

but put them in script if want. run script manually. need change number of days (make sure stays negative), put in correct file path , add other attributes want get.

$fpath = "c:\users\(currentuser)\desktop\usercreatedates.csv" $numdays = -30  $logfilepath = (test-path $fpath) if (($logfilepath) -ne "true") {     new-item $fpath -itemtype file     add-content $fpath "name,emailaddress,created" } else {     clear-content $fpath     add-content $fpath "name,emailaddress,created" }  $date1 = [datetime]::today.adddays($numdays) $mailboxes = get-mailbox -resultsize unlimited -filter {recipienttype -eq "usermailbox"}   foreach ($mailbox in $mailboxes) { $displayname = """" + $mailbox.displayname + """" $upn = """" + $mailbox.userprincipalname + """" $mailboxcreated = $mailbox.whencreated if ($mailboxcreated -gt $date1)     {         $displayname + "," + $upn + ",""" +          $mailboxcreated + """" | out-file -filepath $fpath -append     } } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -