public void Connect(PSCredential credentials, CultureInfo sessionUiCulture) { this.CheckDisposed(); try { Dns.GetHostEntry(this.targetServer); } catch (SocketException ex) { throw new CouldNotResolveServerException(this.targetServer, ex, ex); } Uri uri = null; string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; switch (this.connectionType) { case PowershellConnectionType.WSMan: uri = new Uri(string.Format("{0}{1}/wsman", this.useSSL ? "https://" : "http://", this.targetServer)); shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell"; break; case PowershellConnectionType.OnPrem: this.useSSL = false; uri = new Uri(string.Format("{0}{1}/powershell?serializationLevel=Full", this.useSSL ? "https://" : "http://", this.targetServer)); break; case PowershellConnectionType.Tenant: { string uriString = string.Format("{0}{1}/powershell-liveid?serializationLevel=Full", this.useSSL ? "https://" : "http://", this.targetServer); uri = new Uri(uriString); break; } } WSManConnectionInfo wsmanConnectionInfo = new WSManConnectionInfo(uri, shellUri, credentials); if (this.connectionType == PowershellConnectionType.Tenant) { wsmanConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; } else if (this.connectionType == PowershellConnectionType.OnPrem) { wsmanConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; } PowershellHostUI hostUI = new PowershellHostUI(); RemotePowershellHost host = new RemotePowershellHost(hostUI); wsmanConnectionInfo.Culture = sessionUiCulture; wsmanConnectionInfo.MaximumConnectionRedirectionCount = 5; this.runspace = RunspaceFactory.CreateRunspace(host, wsmanConnectionInfo); this.logger.LogInformation(HybridStrings.HybridInfoOpeningRunspace(uri.ToString())); try { this.runspace.Open(); } catch (PSRemotingTransportException ex2) { throw new CouldNotOpenRunspaceException(ex2, ex2); } this.openedRunspace = true; }
public RemotePowershellHost(PowershellHostUI hostUI) { this.hostUI = hostUI; }