protected override void BeginProcessing() { Guid runspId; var runsp = PowerShell.Create(RunspaceMode.CurrentRunspace); runspId = runsp.Runspace.InstanceId; Connection = (Connection)TokenCollection.Get(runspId); if (Connection == null) { throw new ArgumentException("No Active Connection"); } if (string.IsNullOrEmpty(Connection.AccessToken)) { throw new ArgumentException("No Valid Connection"); } var datenow = DateTime.Now.ToUniversalTime(); var expiredate = Connection.ExpiresAt; if (DateTime.Compare(datenow, expiredate) == 1) { throw new ArgumentException("Connection Token Refresh needed"); } }
protected override void BeginProcessing() { Guid runspId; var runsp = PowerShell.Create(RunspaceMode.CurrentRunspace); runspId = runsp.Runspace.InstanceId; Connection = (Connection)TokenCollection.Get(runspId); if (Connection == null) { throw new ArgumentException("No Active Connection"); } if (string.IsNullOrEmpty(Connection.AccessToken)) { throw new ArgumentException("No Valid Connection"); } }
protected override void BeginProcessing() { Guid runspId; var runsp = PowerShell.Create(RunspaceMode.CurrentRunspace); runspId = runsp.Runspace.InstanceId; Connection = (Connection)TokenCollection.Get(runspId); if (Connection == null) { throw new ArgumentException("No Active Connection"); } if (string.IsNullOrEmpty(Connection.AccessToken)) { throw new ArgumentException("No Valid Connection"); } // checkcon = new CompanyService(Connection); // checkcon.OnRefreshConnectionRaised += Service_OnRefreshConnectionRaised; // var currentcompany = Task.Run(() => checkcon.GetCurrentAsync()).Result; if (Connection.TenantId == Guid.Empty) { throw new ArgumentException("No Tenant Selected"); } var datenow = DateTime.Now.ToUniversalTime(); var expiredate = Connection.ExpiresAt; if (DateTime.Compare(datenow, expiredate) == 1) { TokenService.Refresh(Connection.LogonUrl, Connection.RefreshToken); Connection.RefreshToken = TokenService.RefreshToken; TokenCollection.Replace(runspId, Connection); // throw new ArgumentException("Connection Token Refresh needed"); } }
protected override void ProcessRecord() { Guid runspId; var runsp = PowerShell.Create(RunspaceMode.CurrentRunspace); runspId = runsp.Runspace.InstanceId; var con = (Connection)TokenCollection.Get(runspId); if (con == null) { throw new ArgumentException("No Active Connection"); } if (string.IsNullOrEmpty(con.AccessToken)) { throw new ArgumentException("No Valid Connection"); } TenantService tenantService = new TenantService(con); try { Task <Result <List <Tenant> > > callTask = Task.Run(() => tenantService.GetByPlatformAsync(PlatformId)); callTask.Wait(); if (callTask.Result.Object != null) { if (callTask.Result.Object.Count == 0) { Console.WriteLine("No Tenant existing"); } else if (callTask.Result.Object.Count == 1) { tenant = callTask.Result.Object.First(); con.TenantId = tenant.Id; WriteObject(new ConnectionResult { UserName = con.UserName, ApiUrl = con.ApiUrl, LogonUrl = con.LogonUrl, TenantId = con.TenantId }); } else { if (callTask.Result.Object.Any(x => x.Id == Id)) { tenant = callTask.Result.Object.First(x => x.Id == Id); con.TenantId = tenant.Id; WriteObject(new ConnectionResult { UserName = con.UserName, ApiUrl = con.ApiUrl, LogonUrl = con.LogonUrl, TenantId = con.TenantId }); } else { Console.WriteLine("As multiple Tenant exists please Set Tenant focus."); WriteObject(callTask.Result.Object); } } using (runsp = PowerShell.Create(RunspaceMode.CurrentRunspace)) { runspId = runsp.Runspace.InstanceId; TokenCollection.Replace(runspId, con); } } else if (callTask.Result.Error != null) { throw new RemoteException("Conflict Error: " + callTask.Result.Error.ErrorType + "\r\n" + callTask.Result.Error.FaultyValues); } else { throw new RemoteException("API returns: " + callTask.Result.Code.ToString()); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }