I'm trying to 1) test whether PowerShell remoting is functioning and 2) get the PowerShell execution policy on several remote systems.
This is what I have so far:
foreach ($server in $servers)
{
function Test-PsRemoting
{
param(
[Parameter(Mandatory = $true)]
$computername
)
try
{
$errorActionPreference = "Stop"
$result = Invoke-Command -ComputerName $computername { 1 }
}
catch
{
Write-Verbose $_
return $false
}
if($result -ne 1)
{
Write-Verbose "Remoting to $computerName returned an unexpected result."
return Write-Host "PS-Remoting is disabled for $computername"
}
Write-Host "PS-Remoting is enabled for $computername"
}
Test-PsRemoting $server
Invoke-Command -ComputerName $server -ScriptBlock {
$ExecutionPolicy = get-executionpolicy
}
Write-Host "The PowerShell execution policy on $server is $ExecutionPolicy"
}
The desired output is:
PS-Remoting is enabled/disabled for SERVERNAME
The Powershell execution policy on SERVERNAME is set to Restricted/Unrestricted/etc.
But the script above does not return any output.
If I define the "Test-PSRemoting" function and call it with a single parameter (e.g., Test-PSRemoting COMPUTERNAME1), then it works fine.
However, I'm not sure how to call it several times, each time with a different parameter from servers.txt.
Also, I'm not sure if I can use the $ExecutionPolicy variable in the last Write-Host command, since the value of that variable is set inside the -ScriptBlock.
Any clarification would be appreciated.
Thank you! 🙂