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

如何从命令行使用.NET 5.0配置Azure VM?,azure,powershell,command-line-interface,virtual-machine,Azure,Powershell,Command Line Interface,Virtual Machine,我正在设置一些虚拟机来运行我的服务。可能有几个,所以我正在尝试自动化这个过程。我有一个PowerShell脚本,它成功地构建了虚拟机,但现在我想在同一个脚本中安装.NET Core Web应用程序所需的依赖软件 我要安装的第一个依赖项是.NET5.0运行时。我已经在浏览器中多次这样做了,但现在我想将其提交到一个在构建VM后运行的脚本中。正如@amit\u g所建议的,您可以使用直接运行来满足您的要求。通过使用安装并最终确定参数,在本地或测试VM上进行测试。然后使用自定义脚本扩展安装该脚本。代码如

我正在设置一些虚拟机来运行我的服务。可能有几个,所以我正在尝试自动化这个过程。我有一个PowerShell脚本,它成功地构建了虚拟机,但现在我想在同一个脚本中安装.NET Core Web应用程序所需的依赖软件


我要安装的第一个依赖项是.NET5.0运行时。我已经在浏览器中多次这样做了,但现在我想将其提交到一个在构建VM后运行的脚本中。

正如@amit\u g所建议的,您可以使用直接运行来满足您的要求。

通过使用安装并最终确定参数,在本地或测试VM上进行测试。然后使用自定义脚本扩展安装该脚本。代码如下所示(未测试)

$Command=“&powershell-NoProfile-ExecutionPolicy unrestricted-Command”[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12&([scriptblock]::创建((调用WebRequest-UseBasicParsing'https://dot.net/v1/dotnet-install.ps1("))


您还可以用于运行自定义脚本。

这似乎非常有效:

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"
#VM的名称作为第一个参数传入。
参数($machineName)
如果($machineName-eq$null)
{
写入主机“用法:配置机器-机器名”;
出口
}
#下载代理安装文件。
$agentZip=“agent.zip”;
调用WebRequest-Uri“https://vstsagentpackage.azureedge.net/agent/2.181.1/vsts-agent-win-x64-2.181.1.zip“-输出文件$agentZip
#打开包装。
$agentDirectory=“$env:SystemDrive\aAgent”;
添加类型-AssemblyName System.IO.Compression.FileSystem;
[System.IO.Compression.ZipFile]::extractodirectory($agentZip,$agentDirectory);
#将计算机配置为作为DevOps代理工作。
&“$agentDirectory\config.cmd”--无人参与--deploymentgroup--deploymentgroupname“生产”--代理“$machineName”--运行服务--工作“\u工作”--url”https://dev.azure.com/theta-rex/--projectname“openbook”-auth PAT--token te64yuv36tina2rvc2lsvwcsvctpWormiewz5fxIHcubbdzaasoka
#安装完成后删除代理Zip文件。
删除$agentZip项;
#下载并安装.NET 5.0
调用WebRequest-Uri“https://dot.net/v1/dotnet-install.ps1-OutFile“dotnet安装.ps1”
&“/dotnet install.ps1”-通道5.0-运行时aspnetcore-InstallDir”C:\Program Files\dotnet”

最终的结果是一台配置为参与Azure DevOps和ASP.NET 5.0的机器。

Use@amit\u g-eh,我明白了要点。我正在寻找如何在目标机器上获取脚本,然后调用它的总体思路。我现在已经完成了大部分基于Invoke-AzureRmVMRunCommand的脚本。不过,dotnet安装脚本似乎是关键因素之一。谢谢。我相信也可以通过任何方式(即手动)创建一个映像,然后将其作为映像来创建新的VM
# 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"