Dashboard Universale di PowerShell: La Tua Guida su Azure

Se conosci il linguaggio PowerShell e hai bisogno di creare un cruscotto grafico che rappresenti praticamente qualsiasi dato, dai un’occhiata a PowerShell Universal Dashboard (UD). UD è un modo intuitivo per creare cruscotti di ottimo aspetto e persino moduli utilizzando solo PowerShell. Per fortuna, Universal PowerShell Dashboard funziona anche in Azure!

Universal PowerShell Dashboard è un modulo PowerShell disponibile per l’installazione eseguendo Install-Module -Name UniversalDashboard.Community. Il modulo della community è gratuito, ma ti incoraggio ad acquistare la versione completa.

I’m not going to go into the ins and outs of UD, Adam Driscoll (UD’s developer) has written extensive documentation already on the topic.

UD ha bisogno di un server web per funzionare. Puoi scegliere di eseguire UD su IIS o in un’app Web di Azure. Non mi piace manipolare l’infrastruttura in loco, quindi scelgo sempre di distribuire risorse nel cloud ogni volta che posso. Poiché UD supporta nativamente l’esecuzione in un’app Web di Azure, è il candidato ideale per il cloud.

I found that even though UD has docs for setting it up in Azure, I still was struggling with an easy way to get it going. I managed to come up with a rough PowerShell script to setup the latest instance for you all in one swoop.

Può essere migliorato, ma ne avevo bisogno solo per un prossimo corso Pluralsight su cui stavo lavorando. Sentiti libero di migliorarlo come desideri. Spero che i commenti all’interno spieghino tutto abbastanza bene.

param(
	[Parameter(Mandatory)]
	[ValidateNotNullOrEmpty()]
	[string]$WebAppName,
	
	[Parameter(Mandatory)]
	[ValidateNotNullOrEmpty()]
	[string]$AzureLocation,

	[Parameter(Mandatory)]
	[ValidateNotNullOrEmpty()]
	[string]$AzureResourceGroup
)

#region Creare l'app Web di Azure
$appSrvPlan = New-AzAppServicePlan -Name "$WebAppName-AppSrv" -Location $AzureLocation -ResourceGroupName $AzureResourceGroup -Tier Free
$null = New-AzWebApp -Name $WebAppName -AppServicePlan $appSrvPlan.Name -ResourceGroupName $AzureResourceGroup -Location $AzureLocation
#endregion

#region Scaricare UD
Save-Module UniversalDashboard.Community -Path $env:TEMP -AcceptLicense
$udModulePath = (Get-ChildItem -Path "$env:TEMP\UniversalDashboard.Community" -Directory).FullName
#endregion

# Ottenere il profilo di pubblicazione per l'app Web
$pubProfile = Get-AzWebAppPublishingProfile -Name $Webappname -ResourceGroupName $AzureResourceGroup
$ftpPubProfile = ([xml]$pubProfile).publishData.publishProfile | Where-Object { $_.publishMethod -eq 'FTP' }

## Costruire le credenziali per l'autenticazione
$password = ConvertTo-SecureString $ftpPubProfile.userPWD -AsPlainText -Force
$azureCred = New-Object System.Management.Automation.PSCredential ($ftpPubProfile.userName, $password)

try {
	$webclient = New-Object -TypeName System.Net.WebClient
	$webclient.Credentials = $azureCred

	#region Creare tutte le cartelle
	Get-ChildItem -Path $udModulePath -Directory -Recurse | foreach {
		$path = $_.FullName.Replace("$udModulePath\", '').Replace('\', '/')
		$uri = "$($ftpPubProfile.publishUrl)/$path"
		$makeDirectory = [System.Net.WebRequest]::Create($uri)
		$makeDirectory.Credentials = $azureCred
		$makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory
		Write-Host "Creating folder [$path]..."
		$null = $makeDirectory.GetResponse()
	}
	#endregion

	## Creare un cruscotto semplice per avviare il sito
	Set-Content -Path "$udModulePath\dashboard.ps1" -Value 'Start-UDDashboard -Wait'

	#region Caricare tutti i file
	Get-ChildItem -Path $udModulePath -File -Recurse | foreach {
		$path = $_.FullName.Replace("$udModulePath\", '').Replace('\', '/').Replace('.\', '')
		$uri = New-Object System.Uri("$($ftpPubProfile.publishUrl)/$path")
		Write-Host "Uploading file [$path]..."
		$null = $webclient.UploadFile($uri, $_.FullName)
	}
	#endregion
} catch {
	$PSCmdlet.ThrowTerminatingError($_)
} finally {
	## Pulizia
	$webclient.Dispose()
}

I called the script New-UDAzureInstance.ps1 and launch it like:

PS> ./New-UDAzureInstance.ps1 -WebAppName ADBPoshUD -AzureResourceGroup 'Course-PowerShellDevOpsPlaybook' -AzureLocation 'East US'

Una volta configurato UD in Azure, modificherai quindi il file dashboard.ps1 per creare qualsiasi tipo di cruscotto di cui hai bisogno.

Visita l’URL della tua app Web di Azure e goditi la gloria di un’istanza appena installata di Universal PowerShell Dashboard.

PowerShell Universal Dashboard in Azure

I hope this saves some people some time setting up Universal Powershell Dashboard in Azure!

Source:
https://adamtheautomator.com/powershell-universal-dashboard/