在 Azure 中创建 VM 时,虚拟网络能否位于不同的资源组中
Can virtual network be in different Resource Group while creating a VM in Azure
我想在来自 Image1
的 ABC
资源组中的 VM 上创建虚拟机,该虚拟机也在 ABC
资源组中使用 powershell。
现在我面临的问题是虚拟网络在不同的资源组中 XYZ
。所以我尝试过类似的方法,但没有成功。
$UserName = "admin"
$Password = ConvertTo-SecureString "admin@123" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-AzureRmVm `
-ResourceGroupName "ABC" `
-Name "VName" `
-ImageName "Image1" `
-Location "West US" `
-VirtualNetworkName "XYZ\Vnetname" `
-SubnetName "default" `
-Credential $psCred `
-OpenPorts 3389
我已经对此进行了搜索,但如果您正在使用 Angular CLI 或从模板创建 VM,那么会有答案。但是我想在没有模板的情况下用 powershell 编写。
编辑
我试过了,但无法继续
$UserName = "admin"
$Password = ConvertTo-SecureString "admin@123" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$ResourceGroupName = "ABC"
$VMSize = "Standard_B2s"
$VMName = "VName1"
$LocationName = "West US"
$NICName = "VName123"
$VirtualNetworkName = "VNetName"
$VirtualNetworkResourceGroup = "XYZ"
$VnetAddressPrefix = "10.3.5.0/25"
$SubnetAddressPrefix = "10.3.5.0/25"
$SubnetName = "Subnet1"
$SingleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = New-AzureRmVirtualNetwork -Name $VirtualNetworkName -ResourceGroupName $VirtualNetworkResourceGroup -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
$NIC = New-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $VirtualNetworkResourceGroup -Location $LocationName -SubnetId $Vnet.Subnets[0].Id
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $VMName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine -Verbose
$VnetAddressPrefix = "10.3.5.0/25" and $SubnetAddressPrefix = "10.3.5.0/25" --> 不确定要输入什么。因此,我从 Azure 门户检查了 Subnet1 的 AddressRange,并输入了相同的值。
当我运行这个命令时,它显示以下错误。
Cannot index into a null array.
At line:17 char:1
+ $NIC = New-AzureRmNetworkInterface -Name $NICName -ResourceGroupName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
WARNING: New-AzureRmVMConfig: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return
Standard_LRS and Premium_LRS
WARNING: Set-AzureRmVMOperationSystem: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return
Standard_LRS and Premium_LRS
Add-AzureRmVMNetworkInterface : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:21 char:73
+ ... chine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-AzureRmVMNetworkInterface], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.AddAzureVMNetworkInterfaceCommand
WARNING: New-AzureRmVM: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return Standard_LRS and
Premium_LRS
WARNING: Error occurred when choosing existing standard storage account for boot diagnostics: Sequence contains no matching element
WARNING: Since the VM is created using premium storage, new standard storage account, corpcpscps082813020, is created for boot diagnostics.
VERBOSE: Performing the operation "New" on target "Vname1".
New-AzureRmVM : Required parameter 'networkProfile' is missing (null).
ErrorCode: InvalidParameter
ErrorMessage: Required parameter 'networkProfile' is missing (null).
StatusCode: 400
ReasonPhrase: Bad Request
OperationID : b1d281a7-a1df-48f6-9983-67319267f11d
At line:23 char:1
+ New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Locati ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand
是的,您可以在不同的资源组中使用 Vnet。但限制是您的 Vnet 和 VM 必须在同一区域。
更新
使用PowerShell cmdlet在其他资源组中使用Vnet创建虚拟机,可参考以下步骤。
- 使用命令
Get-AzureRmVirtualNetwork -ResourceGroupName vnetResourceGroupName -Name VnetName
. 获取另一个资源组中的Vnet
- 创建与 Vnet 关联的网络接口。
- 照常设置其他选项。
- 创建虚拟机。
以下脚本仅供参考
$UserName = userName
$Password = ConvertTo-SecureString password -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-AzureRmResourceGroup -Name resourceGroupName -Location location
$PIP = New-AzureRmPublicIpAddress -Name publicIPName -DomainNameLabel publicIPDomain -ResourceGroupName resourceGroupName -Location location -AllocationMethod Dynamic
$Vnet = $(Get-AzureRmVirtualNetwork -ResourceGroupName vnetResourceGroupName -Name VnetName)
$NIC = New-AzureRmNetworkInterface -Name NICNName -ResourceGroupName resourceGroupName -Location location -SubnetId $Vnet.Subnets[0].Id -PublicIpAddressId $PIP.Id
$VirtualMachine = New-AzureRmVMConfig -VMName vmName -VMSize vmSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName conputerName -Credential $psCred -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version latest
New-AzureRmVm -ResourceGroupName resourceGroupName -Location location -VM $VirtualMachine
我想在来自 Image1
的 ABC
资源组中的 VM 上创建虚拟机,该虚拟机也在 ABC
资源组中使用 powershell。
现在我面临的问题是虚拟网络在不同的资源组中 XYZ
。所以我尝试过类似的方法,但没有成功。
$UserName = "admin"
$Password = ConvertTo-SecureString "admin@123" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-AzureRmVm `
-ResourceGroupName "ABC" `
-Name "VName" `
-ImageName "Image1" `
-Location "West US" `
-VirtualNetworkName "XYZ\Vnetname" `
-SubnetName "default" `
-Credential $psCred `
-OpenPorts 3389
我已经对此进行了搜索,但如果您正在使用 Angular CLI 或从模板创建 VM,那么会有答案。但是我想在没有模板的情况下用 powershell 编写。
编辑
我试过了,但无法继续
$UserName = "admin"
$Password = ConvertTo-SecureString "admin@123" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$ResourceGroupName = "ABC"
$VMSize = "Standard_B2s"
$VMName = "VName1"
$LocationName = "West US"
$NICName = "VName123"
$VirtualNetworkName = "VNetName"
$VirtualNetworkResourceGroup = "XYZ"
$VnetAddressPrefix = "10.3.5.0/25"
$SubnetAddressPrefix = "10.3.5.0/25"
$SubnetName = "Subnet1"
$SingleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = New-AzureRmVirtualNetwork -Name $VirtualNetworkName -ResourceGroupName $VirtualNetworkResourceGroup -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
$NIC = New-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $VirtualNetworkResourceGroup -Location $LocationName -SubnetId $Vnet.Subnets[0].Id
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $VMName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine -Verbose
$VnetAddressPrefix = "10.3.5.0/25" and $SubnetAddressPrefix = "10.3.5.0/25" --> 不确定要输入什么。因此,我从 Azure 门户检查了 Subnet1 的 AddressRange,并输入了相同的值。
当我运行这个命令时,它显示以下错误。
Cannot index into a null array.
At line:17 char:1
+ $NIC = New-AzureRmNetworkInterface -Name $NICName -ResourceGroupName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
WARNING: New-AzureRmVMConfig: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return
Standard_LRS and Premium_LRS
WARNING: Set-AzureRmVMOperationSystem: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return
Standard_LRS and Premium_LRS
Add-AzureRmVMNetworkInterface : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:21 char:73
+ ... chine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-AzureRmVMNetworkInterface], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.AddAzureVMNetworkInterfaceCommand
WARNING: New-AzureRmVM: A property of the output of this cmdlet will change in an upcoming breaking change release. The StorageAccountType property for a DataDisk will return Standard_LRS and
Premium_LRS
WARNING: Error occurred when choosing existing standard storage account for boot diagnostics: Sequence contains no matching element
WARNING: Since the VM is created using premium storage, new standard storage account, corpcpscps082813020, is created for boot diagnostics.
VERBOSE: Performing the operation "New" on target "Vname1".
New-AzureRmVM : Required parameter 'networkProfile' is missing (null).
ErrorCode: InvalidParameter
ErrorMessage: Required parameter 'networkProfile' is missing (null).
StatusCode: 400
ReasonPhrase: Bad Request
OperationID : b1d281a7-a1df-48f6-9983-67319267f11d
At line:23 char:1
+ New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Locati ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand
是的,您可以在不同的资源组中使用 Vnet。但限制是您的 Vnet 和 VM 必须在同一区域。
更新
使用PowerShell cmdlet在其他资源组中使用Vnet创建虚拟机,可参考以下步骤。
- 使用命令
Get-AzureRmVirtualNetwork -ResourceGroupName vnetResourceGroupName -Name VnetName
. 获取另一个资源组中的Vnet
- 创建与 Vnet 关联的网络接口。
- 照常设置其他选项。
- 创建虚拟机。
以下脚本仅供参考
$UserName = userName
$Password = ConvertTo-SecureString password -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-AzureRmResourceGroup -Name resourceGroupName -Location location
$PIP = New-AzureRmPublicIpAddress -Name publicIPName -DomainNameLabel publicIPDomain -ResourceGroupName resourceGroupName -Location location -AllocationMethod Dynamic
$Vnet = $(Get-AzureRmVirtualNetwork -ResourceGroupName vnetResourceGroupName -Name VnetName)
$NIC = New-AzureRmNetworkInterface -Name NICNName -ResourceGroupName resourceGroupName -Location location -SubnetId $Vnet.Subnets[0].Id -PublicIpAddressId $PIP.Id
$VirtualMachine = New-AzureRmVMConfig -VMName vmName -VMSize vmSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName conputerName -Credential $psCred -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version latest
New-AzureRmVm -ResourceGroupName resourceGroupName -Location location -VM $VirtualMachine