[string[]]$computers=@(
	'SERVER01',
	'SERVER02',
	'SERVER03'
)
[string]$chocoAppName='notepad2'
[version]$minVersion='4.2.25.20160422'

$results=[hashtable]@{}
foreach ($computer in $computers){
	$result=invoke-command -computer $computer -scriptblock{
		param($appName,$minVersion)
        # Install Chocolatey
        if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            Set-ExecutionPolicy Bypass -Scope Process -Force;
            iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}
        $chocoList=choco list -l
        $installedApps=$chocoList|%{
            $appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
            if($appLine){$appLine}
        }
        $appsList=.{
            $apps=[hashtable]@{}    
            foreach($x in $installedApps){                        
                if($null -ne $x){
                    $y=$x.Split(' ')
                    $apps+=@{$y[0]=$y[1]}
                }
                }
            return $apps
            }
        [bool]$alreadyInstalled=$appName -in $appsList.GetEnumerator().Name
        [version]$installedVersion=if($alreadyInstalled){$appsList.$appName}else{$null}
		if($alreadyInstalled -and $installedVersion -ge $minVersion){
			write-host "$appName $installedVersion has already been installed on $env:computername" -ForegroundColor Green
			return $true		
		}else{
			write-host "Installing $appName on $env:computername..."
			$null=choco install notepad2 -y;
			$chocoList=choco list -l
            $installedApps=$chocoList|%{
                $appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
                if($appLine){$appLine}
            }
            $appsList=.{
                $apps=[hashtable]@{}    
                foreach($x in $installedApps){                        
                    if($null -ne $x){
                        $y=$x.Split(' ')
                        $apps+=@{$y[0]=$y[1]}
                    }
                    }
                return $apps
                }
			[bool]$installedSuccess=$appName -in $appsList.GetEnumerator().Name
			if($installedSuccess){
                write-host "$appName has been installed on $env:computername successfully" -ForegroundColor Green
                return $true
			}else{
                write-host "$appName has NOT been installed on $env:computername successfully" -ForegroundColor Yellow
				return $false
            }
        }} -Args $chocoAppName,$minVersion
    }
	$results+=@{$computer=$result}
}
foreach ($item in $results.GetEnumerator()){
	write-output "$($item.Name): $($item.value)"
}