簡體   English   中英

如何從命令行使用 .NET 5.0 配置 Azure VM?

[英]How do you configuring Azure VMs with .NET 5.0 from the command line?

我正在設置一些虛擬機來運行我的服務。 可能有幾個,所以我試圖自動化這個過程。 我有一個成功構建虛擬機的 PowerShell 腳本,但現在我想在同一腳本中安裝我的 .NET Core Web 應用程序所需的依賴軟件。

我要安裝的第一個依賴項是 .NET 5.0 Runtime。 我已經通過瀏覽器多次執行此操作,但現在我想將其提交給在構建 VM 后運行的腳本。

通過使用dotnet-install-script 安裝並最終確定參數,在本地或測試 VM 上進行測試。 然后使用Set-AzVMExtension使用自定義腳本擴展安裝該腳本。 代碼看起來像這樣(未經測試)

$Command = "&powershell -NoProfile -ExecutionPolicy unrestricted -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) <additional install-script args>"
$Settings = @{"commandToExecute" = "Powershell $Command";};

Set-AzVMExtension `
    -ResourceGroupName "ResourceGroupName" `
    -Location "Location" `
    -VMName "VirtualMachineName" `
    -Name "ExtensionName" `
    -Publisher "Contoso.Compute" `
    -Type "CustomScriptExtension" `
    -TypeHandlerVersion "1.1" `
    -Settings $Settings

設置自定義腳本窗口的完整詳細信息和架構

您還可以使用Set-AzureVMCustomScriptExtension運行自定義腳本。

正如@amit_g 建議的那樣,您可以使用Azure VM 運行命令功能直接運行install .net 和 PowerShell 的腳本來滿足您的要求。

這似乎工作得很好:

Invoke-AzureRmVMRunCommand -ResourceGroupName "$resourceGroupName" -Name "$machineName" -CommandId "RunPowerShellScript" -ScriptPath "configureMachine.ps1" -Parameter @{"machineName" = "$machineName"}

Powershell 腳本的內容如下所示:

# The name of the VM is passed in as the first parameter.
param ($machineName)
if ($machineName -eq $null)
{
    Write-Host "Usage: configureMachine -machineName <machineName>";
    Exit;
}

# Download the agent installation files.
$agentZip="agent.zip";
Invoke-WebRequest -Uri "https://vstsagentpackage.azureedge.net/agent/2.181.1/vsts-agent-win-x64-2.181.1.zip" -OutFile $agentZip

# Unpack them.
$agentDirectory="$env:SystemDrive\azagent";
Add-Type -AssemblyName System.IO.Compression.FileSystem;
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentZip, $agentDirectory);

# Configure the machine to work as a DevOps Agent.
&"$agentDirectory\config.cmd" --unattended --deploymentgroup --deploymentgroupname "Production" --agent "$machineName" --runasservice --work "_work" --url "https://dev.azure.com/theta-rex/" --projectname "openbook" --auth PAT --token te64yuv36tina2rvc2lsvwcsvctpwomiewz5fxihcubbdzaasoka

# Remove the Agent Zip files when installation is complete.
Remove-Item $agentZip;

# Download and install .NET 5.0
Invoke-WebRequest -Uri "https://dot.net/v1/dotnet-install.ps1" -OutFile "dotnet-install.ps1"
&"./dotnet-install.ps1" -Channel 5.0 -Runtime aspnetcore -InstallDir "C:\Program Files\dotnet"

最終結果是一台機器配置為參與 Azure DevOps 和 ASP.NET 5.0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM