Skip to content

Commit

Permalink
Added ping to check if destination server exists
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinvanhunen committed May 22, 2023
1 parent 2f4518d commit cc3c556
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/Commands/Base/ConnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Security;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class ConnectOnline : BasePSCmdlet
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SYSTEMASSIGNEDMANAGEDIDENTITY)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYCLIENTID)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYPRINCIPALID)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
public SwitchParameter ReturnConnection;

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

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID)]
public string UserAssignedManagedIdentityAzureResourceId;
public string UserAssignedManagedIdentityAzureResourceId;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_CREDENTIALS)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ENVIRONMENTVARIABLE)]
Expand Down Expand Up @@ -276,6 +277,11 @@ protected void Connect(ref CancellationToken cancellationToken)
credentials = Credentials.Credential;
}

if (PingHost(new Uri(Url).Host) == false)
{
throw new PSArgumentException("Host not reachable");
}

// Connect using the used set parameters
switch (ParameterSetName)
{
Expand Down Expand Up @@ -480,7 +486,7 @@ private PnPConnection ConnectAppOnlyWithCertificate()
{
throw new FileNotFoundException("Certificate not found");
}

X509Certificate2 certificate = CertificateHelper.GetCertificateFromPath(this, CertificatePath, CertificatePassword);
if (PnPConnection.Current?.ClientId == ClientId &&
PnPConnection.Current?.Tenant == Tenant &&
Expand Down Expand Up @@ -696,6 +702,32 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati
#endregion

#region Helper methods

private static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;

try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}

return pingable;
}
private PSCredential GetCredentials()
{
var connectionUri = new Uri(Url);
Expand Down

0 comments on commit cc3c556

Please sign in to comment.