当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 Azure 中自动安装 PowerShell 通用仪表板

作者:精品下载站 日期:2024-12-14 12:39:18 浏览:15 分类:玩电脑

在 Azure 中自动安装 PowerShell 通用仪表板


如果您知道如何编写 PowerShell 并且需要构建代表几乎任何数据的图形仪表板,请查看 PowerShell 通用仪表板 (UD)。 UD 是一种仅使用 PowerShell 即可构建美观的仪表板甚至表单的直观方法。幸运的是,通用 Powershell 仪表板可以在 Azure 中运行!

通用 Powershell Dashboard 是一个 PowerShell 模块,可通过运行 Install-Module -Name UniversalDashboard.Community 来安装。社区模块是免费的,但我鼓励您购买完整版本。

我不打算详细介绍 UD,Adam Driscoll(UD 的开发人员)已经就该主题编写了大量文档。

UD 需要 Web 服务器才能运行。您可以选择在 IIS 或 Azure Web App 中运行 UD。我讨厌搞乱本地基础设施,因此我总是选择尽可能将资源部署到云中。由于 UD 本身支持在 Azure Web 应用程序中运行,因此它是完美的云候选者。

我发现,尽管 UD 有在 Azure 中进行设置的文档,但我仍然在努力寻找一种简单的方法来实现它。我设法想出了一个粗略的 PowerShell 脚本来一次性为大家设置最新的实例。

它可以使用一些工作,但我只需要这个来完成我正在学习的即将到来的 Pluralsight 课程。您可以根据需要随意改进它。希望里面的评论能够很好地解释一切。

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

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

#region Create the Azure web app
$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 Download UD
Save-Module UniversalDashboard.Community -Path $env:TEMP -AcceptLicense
$udModulePath = (Get-ChildItem -Path "$env:TEMP\UniversalDashboard.Community" -Directory).FullName
#endregion

# Get publishing profile for the web app
$pubProfile = Get-AzWebAppPublishingProfile -Name $Webappname -ResourceGroupName $AzureResourceGroup
$ftpPubProfile = ([xml]$pubProfile).publishData.publishProfile | Where-Object { $_.publishMethod -eq 'FTP' }

## Build the cred to authenticate
$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 Create all folders
	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

	## Create a simple dashboard to bring the site up
	Set-Content -Path "$udModulePath\dashboard.ps1" -Value 'Start-UDDashboard -Wait'

	#region Upload all of the files
	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 {
	## Cleanup
	$webclient.Dispose()
}

我调用了脚本 New-UDAzureInstance.ps1 并按以下方式启动它:

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

在 Azure 中设置 UD 后,您将修改 dashboard.ps1 文件以构建您需要的任何类型的仪表板。

浏览到 Azure Web App 的 URL,享受新安装的通用 Powershell 仪表板实例的辉煌。

[玩转系统] 在 Azure 中自动安装 PowerShell 通用仪表板

我希望这可以节省一些人在 Azure 中设置通用 Powershell 仪表板的时间!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯