Connect to Azure Stack Hub PEP

Microsoft.SystemCenter.AzureStack.Deployment.ConnectToPEP.Task (ConsoleTask)

This task establishes a remote PS-session to Microsoft Azure Stack Hub privileged endpoint (PEP).

Element properties:

TargetMicrosoft.SystemCenter.AzureStack.Region
Applicationpowershell.exe
WorkingDirectory
isRequireOutputFalse
AccessibilityPublic
EnabledTrue

Source Code:

<ConsoleTask ID="Microsoft.SystemCenter.AzureStack.Deployment.ConnectToPEP.Task" Accessibility="Public" Enabled="true" Target="Microsoft.SystemCenter.AzureStack.Region" RequireOutput="false">
<Assembly>Res.Microsoft.SystemCenter.AzureStack.Deployment.ConnectToPEP.Task</Assembly>
<Handler>ShellHandler</Handler>
<Parameters>
<Argument Name="Application">powershell.exe</Argument>
<Argument>-noprofile -noexit -Command "&amp; { param($peps)
if($peps -eq $null -or $peps.length -le 1 -or $peps -eq 'null') {
Write-Warning ('There are no PEP IP addresses on the selected region.')
return;
}
Write-Host "Loading...";
function IsServerAvailable($server)
{
$res = $true;
try
{
Test-Connection $server -Count 3 -ErrorAction Stop | Out-Null
}
catch
{
$res =$false;
}
return $res;
}

function CheckRemoteConnectionAvailability($server, $creds)
{
$res = -2;
if($creds -eq $null -or [string]::IsNullOrEmpty($creds.GetNetworkCredential().password))
{
return -1;
}
try
{
Test-WSMan -ComputerName $server -Credential $creds -Authentication Default -ErrorAction Stop | Out-Null;
$res = 0;

}
catch [System.Management.Automation.ParameterBindingException]
{
$res = -1;
}
catch
{
if($_.Exception.Message -like '*Access is denied*')
{
$res = -1;
}

}
return $res;
}

$pep = $peps -split ' ';
$cred = $null;
$connected = $false;
$addedToTrusted = $true;
$isServerAvailable = $false;
foreach($p in $pep) {

$address = $p.Trim();

Write-Host ('Checking availability of '+$address+'...');
$isServerAvailable = IsServerAvailable($address);

if($isServerAvailable -eq $false)
{
Write-Warning ('Unable to connect to the ERCS VM: '+$address+'. Please verify the ERCS VM is running and reachable.');
continue;
}
$a = Get-Item WSMan:\localhost\Client\TrustedHosts;
if($address.Equals($a.Value) -eq $false) {
Write-Host "Setting $address to Trusted Hosts."
try {
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $address -Force -ErrorAction SilentlyContinue;
}
catch
{
Write-Warning ('Task cannot add ERCS VM '+$address+' to Trusted Hosts. Please restart SCOM console with local administrator permissions.');
$addedToTrusted = $false;
break;
}
}
Write-Host ('Connecting to '+$address+'...');

if($cred -eq $null)
{
Write-Host 'Please enter credentials for ERCS VM:';
try
{
$cred = Get-Credential -Credential $null;
}
catch [System.Management.Automation.ParameterBindingException]
{
Write-Warning ('Credentials were missed.');
}
}

$remoteConnectionState = CheckRemoteConnectionAvailability -server $address -creds $cred;
if($remoteConnectionState -eq -1)
{
Write-Warning ('Unable to connect to the ERCS VM: '+$address+'.');
Write-Warning 'Login failed: invalid username or password.'
Write-Warning 'Please close the current window and re-run the Task.'
break;
}
if($remoteConnectionState -eq -2)
{
Write-Warning ('Unable to connect to the ERCS VM: '+$address+'.');
continue;
}
try
{
$session = New-PSSession -ComputerName $address -ConfigurationName PrivilegedEndpoint -Credential $cred -SessionOption (New-PSSessionOption -Culture en-US -UICulture en-US) -ErrorAction SilentlyContinue;
if($session -ne $null)
{
Enter-PSSession $session;
$connected = $true;
break;
}
else
{
Write-Warning ('Unable to connect to the ERCS VM: '+$address+' due to invalid credentials or remote PS-session is disabled on '+$address+'.');
}
}
catch
{
Write-Warning ('Cannot connect to '+$address+'.');
Write-Warning ($_);
}
}
if($connected -eq $false -and $addedToTrusted -eq $true -and $remoteConnectionState -ne -1) {
Write-Warning ('None of ERCS VMs is available. Please verify at least one of the ERCS VMs is running and reachable. Fix connectivity issue, close the current window and re-run the Task.');
} }"</Argument>
<Argument>"$Target/Property[Type='Microsoft.SystemCenter.AzureStack.Region']/PepIpAddresses$"</Argument>
</Parameters>
</ConsoleTask>