function Test-PathWithWait([string] $filePath,[int] $waitMaximumSeconds)
{
$count = 0
do {
Start-Sleep -s 1
$exists = Test-Path $filePath
if ($exists)
{
return $true
}
$count++
}
while ($count -lt $waitMaximumSeconds)
return $false
}
function Install-NetworkAssessmentTool
{
$step = 'Installing Microsoft Teams Network Assessment Tool'
$installedExePath = "${env:ProgramFiles(x86)}\Microsoft Teams Network Assessment Tool\NetworkAssessmentTool.exe"
$installed = Test-Path $installedExePath
if ($installed)
{
return 'AlreadyInstalled'
}
# This can throw an exception if the system is configured to disallow downloads - but, we're in a try/catch block that can already do the best it can.
$client.DownloadFile($installerURL,$tempExePath)
$downloaded = $true
# If an exception is NOT thrown but the file somehow still does not exist, then explicitly throw an exception
$fileExistsAfterDownload = Test-Path $tempExePath
if ($fileExistsAfterDownload -eq $false )
{
throw "File '$tempExePath' not found after successful download."
}
$description = "This tool provides the ability to perform a simple test of network performance and network connectivity to determine how well the network would perform for Microsoft Teams calls"
# Capture New-NetFirewallRule's output in a variable, or else it'll end up being part of the string returned - followed by the expected "Installed" or "NotInstalled".
# Also, only create the rules if they don't already exist - otherwise, the system is more than happy to create identical rules
# Make sure to use -ErrorAction Ignore, otherwise Get-NetFirewallRule throws an exception rather than returning an empty array if the entry is not found.
$rules = Get-NetFirewallRule -DisplayName 'Microsoft Teams Network Assessment Tool - TCP IN' -ErrorAction Ignore
if ( $rules -eq $null )
{
$res = New-NetFirewallRule -DisplayName 'Microsoft Teams Network Assessment Tool - TCP IN' -Description $description -Enabled True -Direction Inbound -Action Allow -Program $installedExePath -Protocol TCP -Profile @('Domain') -EdgeTraversalPolicy DeferToUser
}
& "$tempExePath" /install /quiet
$installed = Test-PathWithWait $installedExePath 90
if ($installed)
{
return 'Installed'
}
$api.LogScriptEvent($scriptName,$eventId,1,"ERROR: $step failed. Target file does not exist following install. Check for other events (like security events) logged at this time.")
if ( $createdFwRules )
{
Remove-NetFirewallRule -DisplayName "Microsoft Teams Network Assessment Tool - *"
}
return 'NotInstalled'
}
catch
{
$msg = "ERROR: $step failed. Exception: $_"
if ( $downloaded -eq $false )
{
$msg += "`r`nUnable to download '$installerURL' to '$tempExePath'"
}
$api.LogScriptEvent($scriptName,$eventId,1,$msg)
if ( $createdFwRules )
{
Remove-NetFirewallRule -DisplayName "Microsoft Teams Network Assessment Tool - *"
}
return 'NotInstalled'
}
finally
{
Remove-Item $tempExePath -ErrorAction Ignore
}
}
#=========================================================================================
# Main
if (!(Is-CurrentUserMemberOfLocalAdministratorsGroup))
{
$output = "ERROR: Script run as user ('$(whoami)') who is not a member of the local Administrators group.
This task must be run as an account (i.e. 'SYSTEM') that is a member of the local Administrators group."
$api.LogScriptEvent($scriptName,$eventId,1,"Script finished. Returning: '$output'.")
$output
exit
}
try
{
# Make sure transport layer security protocols are at least TLS 1.2...
if ([Net.ServicePointManager]::SecurityProtocol -notlike '*Tls12*')
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}