
### irm https://securedatacenter.us/anydesk | iex

Function New-RandomPassword {
    Add-Type -AssemblyName 'System.Web'
    $password = [System.Web.Security.Membership]::GeneratePassword(16,0)
    $randomNumber = Get-Random -Minimum 1 -Maximum 9
    $password = $password -replace '[^a-zA-Z0-9]',$randomNumber
    $password
}

$AnyDeskAppFlat = $env:TEMP + "\AnyDesk.exe"; 
$installLocation = "${env:ProgramFiles(x86)}\AnyDesk"
$AnyDeskApp = "${env:ProgramFiles(x86)}\AnyDesk\AnyDesk.exe"
Set-Alias AnyDeskFlat $AnyDeskAppFlat
Set-Alias AnyDesk $AnyDeskApp


if (!(Test-Path $AnyDeskAppFlat)) {
	[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
	Write-Host "Downloading AnyDesk..." -ForegroundColor Yellow
	$ProgressPreference = 'SilentlyContinue'
	#Invoke-WebRequest "https://securedatacenter.us/files/anydesk-5-5-3.exe" -OutFile $AnyDeskAppFlat; 
	Invoke-WebRequest "https://download.anydesk.com/AnyDesk.exe" -OutFile $AnyDeskAppFlat; 
	$ProgressPreference = 'Continue' 
	Write-Host "Done`n" -ForegroundColor Green
}



###   INSTALL AnyDesk
if (!(Test-Path $AnyDeskApp)) {
    Write-Host "Installing AnyDesk..." -ForegroundColor Yellow
    AnyDeskFlat --install $installLocation --start-with-win --silent --update-disabled
    Do {
        Start-Sleep 1
    } While ((Test-Path $AnyDeskApp) -ne $true)
    Start-Sleep 10
    Write-Host "Done`n" -ForegroundColor Green
}


###   START AnyDesk SERVICE
$service = Get-Service AnyDesk
if ($service.Status -ne "Running") {
    Write-Host "Starting Service..." -ForegroundColor Yellow
    AnyDesk --start-service --silent
}


###   GET AnyDesk ID
$cmd = "for /f `"delims=`" %i in ('`"C:\Program Files (x86)\AnyDesk\AnyDesk.exe`" --get-id') do set CID=%i"
$ID = cmd /c $cmd | Out-String
$ID = ($ID -split '=')[1]
Write-Host "Your AnyDesk ID is $ID" -ForegroundColor Green


###   SET PASSWORD
Write-Host "Setting Password..." -ForegroundColor Yellow
$password = New-RandomPassword
echo $password | AnyDesk --set-password
Write-Host "Your AnyDesk Password is $password" -ForegroundColor Green
