Orchestrator Workflow Action to Perform Pings and Port Checks Using PowerShell

by Simon Sparks · 1 October 2013

The following script enables pings and port checks to be performed using PowerShell.

$objPing = New-Object System.Net.NetworkInformation.Ping;

$objResult = $objPing.Send($strVirtualServerFQDN);

if ( $objResult.Status.ToString() –eq Success )
{
	try
	{	
    	$objSocket = New-Object System.Net.Sockets.TcpClient($strVirtualServerFQDN, $objVirtualServerPort);
		
		if ( $objSocket –eq $null )
		{
			Write-Host $strVirtualServerFQDN ( $objVirtualServerIP ) is UP and Responding to Pings but DOWN On Port $objVirtualServerPort -NoNewLine;
		}
		else
		{
			Write-Host $strVirtualServerFQDN ( $objVirtualServerIP ) is UP and Responding to Pings and UP On Port $objVirtualServerPort -NoNewLine;
		}
	}
	catch
	{
		Write-Host $strVirtualServerFQDN ( $objVirtualServerIP ) is UP and Responding to Pings but DOWN On Port $objVirtualServerPort -NoNewLine;
	}
}
else
{
	Write-Host $strVirtualServerFQDN ( $objVirtualServerIP ) is DOWN and NOT Responding to Pings -NoNewLine;
}
var strVirtualServerFQDN = "fqdn.cloudbuildtools.com";

var strPowerShellScript = '';	
	strPowerShellScript += '$objPing = New-Object System.Net.NetworkInformation.Ping\n';
	strPowerShellScript += '$objResult = $objPing.Send("' + strVirtualServerFQDN + '")\n';
	strPowerShellScript += 'if ( $objResult.Status.ToString() –eq “Success” )';
	strPowerShellScript += '{';	
	strPowerShellScript += '	try';
	strPowerShellScript += '	{';	
	strPowerShellScript += '    	$objSocket = New-Object System.Net.Sockets.TcpClient("' + strVirtualServerFQDN + '", ' + objVirtualServer.Port + ')\n';
	strPowerShellScript += '		if ( $objSocket –eq $null )\n';
	strPowerShellScript += '		{';
	strPowerShellScript += '			Write-Host "' + strVirtualServerFQDN + ' ( ' + objVirtualServer.IP + ' ) is UP and Responding to Pings but DOWN On Port ' + objVirtualServer.Port + '" -NoNewLine';
	strPowerShellScript += '		}';
	strPowerShellScript += '		else';
	strPowerShellScript += '		{';
	strPowerShellScript += '			Write-Host "' + strVirtualServerFQDN + ' ( ' + objVirtualServer.IP + ' ) is UP and Responding to Pings and UP On Port ' + objVirtualServer.Port + '" -NoNewLine';
	strPowerShellScript += '		}';
	strPowerShellScript += '	}';
	strPowerShellScript += '	catch';
	strPowerShellScript += '	{';
	strPowerShellScript += '		Write-Host "' + strVirtualServerFQDN + ' ( ' + objVirtualServer.IP + ' ) is UP and Responding to Pings but DOWN On Port ' + objVirtualServer.Port + '" -NoNewLine';
	strPowerShellScript += '	}';
	strPowerShellScript += '}';
	strPowerShellScript += 'else';
	strPowerShellScript += '{';
	strPowerShellScript += '	Write-Host "' + strVirtualServerFQDN + ' ( ' + objVirtualServer.IP + ' ) is DOWN and NOT Responding to Pings" -NoNewLine';
	strPowerShellScript += '}';

System.log ("===== Attempting to Ping Virtual Server FQDN = '" + strVirtualServerFQDN + "'");

var objPowerShellInvocationResult = objPowerShellHost.invokeScript(strPowerShellScript);

if (objPowerShellInvocationResult.invocationState  == 'Failed')
{
	var arrPowerShellInvocationResultErrors = objPowerShellInvocationResult.getErrors();

	for ( var iii = 0; iii < arrPowerShellInvocationResultErrors.length; iii++ )
	{
		var strPowerShellInvocationResultError = arrPowerShellInvocationResultErrors[iii];
	}

	System.error ("PowerShell Invocation Error: "  + arrPowerShellInvocationResultErrors);
}
else
{
	var strPowerShellInvocationResultHostOutput = objPowerShellInvocationResult.getHostOutput();
		strPowerShellInvocationResultHostOutput = strPowerShellInvocationResultHostOutput.replace(/\n/, "");

	System.log ("===== " + strPowerShellInvocationResultHostOutput );

	objJsonRequest.VirtualServers[i].PingResult = strPowerShellInvocationResultHostOutput;
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like