|
9 | 9 | using System.IO;
|
10 | 10 | using System.Linq;
|
11 | 11 | using System.Management.Automation;
|
| 12 | +using System.Net.NetworkInformation; |
12 | 13 | using System.Reflection;
|
13 | 14 | using System.Security;
|
14 | 15 | using System.Security.Cryptography.X509Certificates;
|
@@ -54,7 +55,7 @@ public class ConnectOnline : BasePSCmdlet
|
54 | 55 | [Parameter(Mandatory = false, ParameterSetName = ParameterSet_SYSTEMASSIGNEDMANAGEDIDENTITY)]
|
55 | 56 | [Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYCLIENTID)]
|
56 | 57 | [Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYPRINCIPALID)]
|
57 |
| - [Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)] |
| 58 | + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)] |
58 | 59 | public SwitchParameter ReturnConnection;
|
59 | 60 |
|
60 | 61 | [Parameter(Mandatory = false, ParameterSetName = ParameterSet_CREDENTIALS, ValueFromPipeline = true)]
|
@@ -214,7 +215,7 @@ public class ConnectOnline : BasePSCmdlet
|
214 | 215 | public string UserAssignedManagedIdentityClientId;
|
215 | 216 |
|
216 | 217 | [Parameter(Mandatory = true, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
|
217 |
| - public string UserAssignedManagedIdentityAzureResourceId; |
| 218 | + public string UserAssignedManagedIdentityAzureResourceId; |
218 | 219 |
|
219 | 220 | [Parameter(Mandatory = false, ParameterSetName = ParameterSet_CREDENTIALS)]
|
220 | 221 | [Parameter(Mandatory = false, ParameterSetName = ParameterSet_ENVIRONMENTVARIABLE)]
|
@@ -276,6 +277,11 @@ protected void Connect(ref CancellationToken cancellationToken)
|
276 | 277 | credentials = Credentials.Credential;
|
277 | 278 | }
|
278 | 279 |
|
| 280 | + if (PingHost(new Uri(Url).Host) == false) |
| 281 | + { |
| 282 | + throw new PSArgumentException("Host not reachable"); |
| 283 | + } |
| 284 | + |
279 | 285 | // Connect using the used set parameters
|
280 | 286 | switch (ParameterSetName)
|
281 | 287 | {
|
@@ -480,7 +486,7 @@ private PnPConnection ConnectAppOnlyWithCertificate()
|
480 | 486 | {
|
481 | 487 | throw new FileNotFoundException("Certificate not found");
|
482 | 488 | }
|
483 |
| - |
| 489 | + |
484 | 490 | X509Certificate2 certificate = CertificateHelper.GetCertificateFromPath(this, CertificatePath, CertificatePassword);
|
485 | 491 | if (PnPConnection.Current?.ClientId == ClientId &&
|
486 | 492 | PnPConnection.Current?.Tenant == Tenant &&
|
@@ -696,6 +702,32 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati
|
696 | 702 | #endregion
|
697 | 703 |
|
698 | 704 | #region Helper methods
|
| 705 | + |
| 706 | + private static bool PingHost(string nameOrAddress) |
| 707 | + { |
| 708 | + bool pingable = false; |
| 709 | + Ping pinger = null; |
| 710 | + |
| 711 | + try |
| 712 | + { |
| 713 | + pinger = new Ping(); |
| 714 | + PingReply reply = pinger.Send(nameOrAddress); |
| 715 | + pingable = reply.Status == IPStatus.Success; |
| 716 | + } |
| 717 | + catch (PingException) |
| 718 | + { |
| 719 | + // Discard PingExceptions and return false; |
| 720 | + } |
| 721 | + finally |
| 722 | + { |
| 723 | + if (pinger != null) |
| 724 | + { |
| 725 | + pinger.Dispose(); |
| 726 | + } |
| 727 | + } |
| 728 | + |
| 729 | + return pingable; |
| 730 | + } |
699 | 731 | private PSCredential GetCredentials()
|
700 | 732 | {
|
701 | 733 | var connectionUri = new Uri(Url);
|
|
0 commit comments