Skip to content

Commit cc3c556

Browse files
committed
Added ping to check if destination server exists
1 parent 2f4518d commit cc3c556

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/Commands/Base/ConnectOnline.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.IO;
1010
using System.Linq;
1111
using System.Management.Automation;
12+
using System.Net.NetworkInformation;
1213
using System.Reflection;
1314
using System.Security;
1415
using System.Security.Cryptography.X509Certificates;
@@ -54,7 +55,7 @@ public class ConnectOnline : BasePSCmdlet
5455
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SYSTEMASSIGNEDMANAGEDIDENTITY)]
5556
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYCLIENTID)]
5657
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYPRINCIPALID)]
57-
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
58+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
5859
public SwitchParameter ReturnConnection;
5960

6061
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_CREDENTIALS, ValueFromPipeline = true)]
@@ -214,7 +215,7 @@ public class ConnectOnline : BasePSCmdlet
214215
public string UserAssignedManagedIdentityClientId;
215216

216217
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
217-
public string UserAssignedManagedIdentityAzureResourceId;
218+
public string UserAssignedManagedIdentityAzureResourceId;
218219

219220
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_CREDENTIALS)]
220221
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ENVIRONMENTVARIABLE)]
@@ -276,6 +277,11 @@ protected void Connect(ref CancellationToken cancellationToken)
276277
credentials = Credentials.Credential;
277278
}
278279

280+
if (PingHost(new Uri(Url).Host) == false)
281+
{
282+
throw new PSArgumentException("Host not reachable");
283+
}
284+
279285
// Connect using the used set parameters
280286
switch (ParameterSetName)
281287
{
@@ -480,7 +486,7 @@ private PnPConnection ConnectAppOnlyWithCertificate()
480486
{
481487
throw new FileNotFoundException("Certificate not found");
482488
}
483-
489+
484490
X509Certificate2 certificate = CertificateHelper.GetCertificateFromPath(this, CertificatePath, CertificatePassword);
485491
if (PnPConnection.Current?.ClientId == ClientId &&
486492
PnPConnection.Current?.Tenant == Tenant &&
@@ -696,6 +702,32 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati
696702
#endregion
697703

698704
#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+
}
699731
private PSCredential GetCredentials()
700732
{
701733
var connectionUri = new Uri(Url);

0 commit comments

Comments
 (0)