Testing remote connectivity for PowerShell

Some things to keep in mind:

The account you run this with needs to be a member of either Local Administrators or Remote Desktop Users, and needs to be a member of SharePoint Shell admin access – assuming you want to access the SharePoint Farm programmatically via powershell.

 

If you have access to an account like this and this account is both a local admin and has Shell Admin access for the SharePoint farm, you can use this powershell to test remote connectivity to your farm using powershell.

You’ll want to run powershell as this account, so right click on the icon and choose the run as option

Servers="2016APPSRCH","2016WFEDC"   # enter the servername here, or specify a group of servers like this "Server1","Server2"

function Test-PsRemoting 
{ 
    param( 
        [Parameter(Mandatory = $true)] 
        $computername 
    ) 
     
    try 
    { 
        $errorActionPreference = "Stop" 
        $result = Invoke-Command -ComputerName $computername { 1 } 
    } 
    catch 
    { 
        Write-Verbose $_ 
        return $false 
    } 
     
    ## I’ve never seen this happen, but if you want to be thorough…. 
    if($result -ne 1) 
    { 
        Write-Verbose "`tRemoting to $computerName returned an unexpected result." 
        return $false 
    } 
     
    return $true  
}

#Validate all servers are available before starting
Write-Host "Testing connectivity to servers"
foreach($s in $Servers){
    if (!(Test-PSRemoting $s)) {
        Write-Host "`tThere was a problem connecting to $s"
        Write-Host "`tScript Terminated"
        Exit #Terminates script if a server is unavailable
    } #end if
    Write-Host "`tSuccessfully connected to $s"
} #end foreach

#Instantiate variables and make clean up company name, if needed


Write-Host "`nScript Complete it is OK to close the window now, have a great day!" -ForegroundColor Cyan

You might not know the name of your server, if that is the case just follow these steps:

 

Open a command prompt

Type NSlookup and then press enter

Type in the name of a site that you know exists on the server, for example portal.mycompany.com and then press enter

The command will return an ip address, e.g. 10.0.0.1 or something

Type exit and then press enter

Type tracert 10.0.0.1 (NOTE: the 10.0.0.1 is just an example, you will use whatever IP you got back in the earlier step) and press enter and you’ll get the name of the server that the IP is related to, unless the site, portal.mycompany.com is behind a load balancer.  You will need to modify the list of servers with you server’s name.  If you know all the servers in the farm (Get-SPFarm).servers, you can make the comma separated list as show in the examples on this post and if you have a single server farm on a server named DansSharePoint.glorifyIT.com, you would type this $Servers=”DansSharePoint” versus $Servers=”2016APPSRCH”,”2016WFEDC”

 

If you want to build in some logic to make sure that you’re logged in with the setup account, add the following lines to your script

$SharePointSetupAcct = "wingtip\2016sp_installer"  #You could replace sp_admin with your setup account name throughout the script
 
 
#Verify you are logged in as the SharePoint setup account
If (!("$env:UserDomain\$env:UserName" -eq $SharePointSetupAcct)){
    Throw "Must be logged in as $SharePointSetupAcct please log off and logon with SharePoint setup account domain\SP_Admin"
}

 

Your script would then look like this:

#################################################################
##              Written by Stacy Simpkins                      ##
##                       for                                   ##
##                   Free Sharing                              ##
#################################################################

$Servers="2016APPSRCH","2016WFEDC"   # enter the servername here, or specify a group of servers like this "Server1","Server2"
$SharePointSetupAcct = "wingtip\2016sp_installer"  #You could replace sp_admin with your setup account name throughout the script
 
 
#Verify you are logged in as the SharePoint setup account
If (!("$env:UserDomain\$env:UserName" -eq $SharePointSetupAcct)){
    Throw "Must be logged in as $SharePointSetupAcct please log off and logon with SharePoint setup account domain\SP_Admin"
}
 

function Test-PsRemoting 
{ 
    param( 
        [Parameter(Mandatory = $true)] 
        $computername 
    ) 
     
    try 
    { 
        $errorActionPreference = "Stop" 
        $result = Invoke-Command -ComputerName $computername { 1 } 
    } 
    catch 
    { 
        Write-Verbose $_ 
        return $false 
    } 
     
    ## I’ve never seen this happen, but if you want to be thorough…. 
    if($result -ne 1) 
    { 
        Write-Verbose "`tRemoting to $computerName returned an unexpected result." 
        return $false 
    } 
     
    return $true  
}

#Validate all servers are available before starting
Write-Host "Testing connectivity to servers"
foreach($s in $Servers){
    if (!(Test-PSRemoting $s)) {
        Write-Host "`tThere was a problem connecting to $s"
        Write-Host "`tScript Terminated"
        Exit #Terminates script if a server is unavailable
    } #end if
    Write-Host "`tSuccessfully connected to $s"
} #end foreach

#Instantiate variables and make clean up company name, if needed


Write-Host "`nScript Complete it is OK to close the window now, have a great day!" -ForegroundColor Cyan