$WshShell = New-Object -ComObject wscript.shell # Import the Royal TS PowerShell module if (-not (Get-Module -Name RoyalDocument.PowerShell)) { Install-Module -Name RoyalDocument.PowerShell Import-Module RoyalDocument.PowerShell } <# Connect to environment Function #> Function Select-Folder($message = 'Select a folder', $path = 0) { $object = New-Object -comObject Shell.Application $folder = $object.BrowseForFolder(0, $message, 0, $path) if ($null -ne $folder) { $folder.self.Path } } connect-AzAccount $action = "Create New", "Update Existing" | Out-GridView -PassThru -Title "Choose action " if ($action -eq "Update Existing") { $RoyalDocumentPath = Select-Folder -message "Select folder to save file in" #$RoyalDocumentPath = "C:\Users\skadlec\OneDrive\Royal TS" $files = get-childitem -Path $RoyalDocumentPath $RoyalDocumentfilename = ($files | Out-GridView -PassThru -Title "Choose file ").fullname } Else { $RoyalDocumentPath = Select-Folder -message "Select folder to save file in" $filename = read-host "Enter file name" $RoyalDocumentfilename = $RoyalDocumentPath + "\" + $filename + ".rtsz" } $Store = New-RoyalStore -UserName ($env:USERDOMAIN + '\' + $env:USERNAME) if (get-childitem -path $RoyalDocumentfilename) { $RoyalDocument = Open-RoyalDocument -FileName $RoyalDocumentfilename -Store $Store } else { $RoyalDocument = New-RoyalDocument -Store $Store -Name "Azure VM Test" -FileName $RoyalDocumentfilename } $folder = Get-RoyalObject -Store $Store -type RoyalFolder | where-object { $_.name -like 'connections' } $folders = Get-RoyalObject -Store $Store -type RoyalFolder $credentials = Get-RoyalObject -Store $Store -type RoyalCredential $connections = Get-RoyalObject -Store $Store -type RoyalRDSConnection $rgs = Get-AzResourceGroup $selectedrgs = $rgs | Out-GridView -PassThru -Title "Choose resource group(s)" foreach ($rg in $selectedrgs) { $newFolder = New-RoyalObject -Folder $folder -Name $rg.ResourceGroupName -Type RoyalFolder Write-Host "Enumerating VMs. Please wait... " -ForegroundColor Yellow $vms = Get-AzVM -ResourceGroupName $rg.ResourceGroupName Write-Host "Enumerating Network Interfaces. Please wait... " -ForegroundColor Yellow $nics = Get-AzNetworkInterface -ResourceGroupName $rg.ResourceGroupName Write-Host "Enumerating Public IP Addresses. Please wait... " -ForegroundColor Yellow $PIPs = Get-AzPublicIpAddress -ResourceGroupName $rg.ResourceGroupName | Where-Object { $_.IpConfiguration.id -match "networkInterfaces" } | Select-Object IpConfiguration, IpAddress, DnsSettings $vms | ForEach-Object { $vm = $_.name $vmid = $_.Id $nic = $nics | Where-Object { $_.VirtualMachine.id -eq $vmid } $PipFqDN = ($pips | Where-Object { $_.IpConfiguration.id -match $nic.id }).dnssettings.fqdn $azureObject = New-Object PSObject $azureObject | Add-Member -MemberType NoteProperty -Name "Name" -Value $_.Name $azureObject | Add-Member -MemberType NoteProperty -Name "ExternalFQDN" -Value $PipFqDN $newconnection = new-royalobject -Type RoyalRDSConnection -name $vm -Folder $newFolder $ConnectionSettings = New-Object PSObject $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "URI" -Value $azureObject.ExternalFQDN $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "CredentialMode" -Value 1 $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "ConnectToAdministerOrConsole" -Value $True $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "ColorDepth" -Value 24 $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "SmartReconnect" -Value $True $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "SmartSizing" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AllowFontSmoothing" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AllowFullWindowDrag" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AllowMenuAnimations" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AllowMouseCursorShadow" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AllowTextCursorBlinking" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "CacheSize" -Value 1 $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AllowThemes" -Value $False $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "AudioRedirectionMode" -Value 2 $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "RedirectDrives" -Value $True $ConnectionSettings | Add-Member -MemberType NoteProperty -Name "RedirectClipboard" -Value $True $ConnectionSettings.PSObject.Properties | foreach-object { $name = $_.Name $value = $_.value #"$name = $value" $setvalue = Set-RoyalObjectValue -Object $newconnection -Property $name -Value $value } } } Out-RoyalDocument -Document $RoyalDocument Close-RoyalDocument -Document $RoyalDocument