azure - 如何将一组 powershell 命令转换为我可以在需要时运行的脚本?
问题描述
我有一组可以在 azure powershell 中使用的命令。这些命令创建了一个资源组、应用程序服务等。我想将它们捆绑起来,这样我就可以在终端中输入一个命令并一次性运行所有部署。
# Ask user for work item id
$workItemId = Read-Host -Prompt "Enter the Work Item ID"
# Set Variables
$appdirectory="C:\Users\Charles\Desktop\Timesheet App\Discover\Client\build"
$webappname="discoverTest$workItemId"
$location="West Europe"
# Create a resource group.
New-AzResourceGroup -Name discoverTest$workItemId -Location $location
# Create an App Service plan in `Free` tier.
New-AzAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName discoverTest$workItemId -Tier Free
# Create a web app.
New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName discoverTest$workItemId
# Get publishing profile for the web app
$xml = [xml](Get-AzWebAppPublishingProfile -Name $webappname `
-ResourceGroupName discoverTest$workItemId `
-OutputFile null)
# Extract connection information from publishing profile
$username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
$password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
$url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value
# Upload files recursively
Set-Location $appdirectory
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$files = Get-ChildItem -Path $appdirectory -Recurse #Removed IsContainer condition
foreach ($file in $files)
{
$relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/')
$uri = New-Object System.Uri("$url/$relativepath")
if($file.PSIsContainer)
{
$uri.AbsolutePath + "is Directory"
$ftprequest = [System.Net.FtpWebRequest]::Create($uri);
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory
$ftprequest.UseBinary = $true
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$response = $ftprequest.GetResponse();
$response.StatusDescription
continue
}
"Uploading to " + $uri.AbsoluteUri + " from "+ $file.FullName
$webclient.UploadFile($uri, $file.FullName)
}
$webclient.Dispose()
$workItemId = Read-Host -Prompt "Enter the Work Item ID"
Remove-AzResourceGroup -Name "discoverTest$workItemId" -Force
# print variable
Write-Host $variable
我希望能够运行单个命令并执行完整的部署过程。
解决方案
有两种方法可以实现您的需求,如下所示。
提取您在这些 PowerShell 命令行中使用的所有参数作为 PowerShell 脚本的参数,该脚本
<your-script-name>.ps1
包含与您的所有相同命令。请参考现有的 SO 线程How to handle command-line arguments in PowerShell了解如何操作。然后,您只需<your-script-name>.ps1
在预安装了 Azure PowerShell 模块的终端中使用这些参数运行。按照博客
Four ways to package a non-GUI PowerShell script as an executable file
使用当前命令集制作可执行文件。
通常,我认为第一种方式更好,值得推荐。
推荐阅读
- r - R case_when 和 %in%
- typescript - TypeScript: is there a way to convert the union type of strings to an enum
- javascript - Vue.js form page loses its data when coming back from a new page
- javascript - 将数据传递给 Vue 子组件而不重新挂载它
- python - 如何从scrapy中的后续函数获取数据
- python - How handle errors on AioHttp using Connexion
- scala - Spark scala finding value in another dataframe
- optaplanner - Design optaplanner constraints of booking system
- bootstrap-4 - 如何修复使用引导程序创建的下拉菜单
- powershell - use PS script to replace text for each line from input file