4

I want to create the virtual machine on VM in ABC resource group from Image1 which is also in ABC resource group using powershell.

Now The problem I'm facing is that the virtual network is in different Resource Group say XYZ.So I have tried something like this, but didn't work.

$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

I have searched on this, but there are answers if you are using Angular CLI or creating a VM from templates. But I want to write in powershell without templates.

Edit

I have tried this, but no able to proceed further

$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" --> was not sure what to enter. So from the Azure portal, I have checked the Subnet1's AddressRange, and put that same value.

When I run this command, it shows the below error.

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
halfer
  • 19,824
  • 17
  • 99
  • 186
CrazyCoder
  • 2,194
  • 10
  • 44
  • 91

1 Answers1

4

Yes, you can use the Vnet in the different resource group. But the limitation is your Vnet and VM must be in the same region.

Update

Use the PowerShell cmdlet to create a VM using the Vnet in another resource group, you can reference the steps below.

  1. Get the Vnet in another resource group using the command Get-AzureRmVirtualNetwork -ResourceGroupName vnetResourceGroupName -Name VnetName.
  2. Create a network interface associated with the Vnet.
  3. Set other options as usuall.
  4. Create the VM.

The script below is just for reference.

$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 
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • 1
    Yes, Vnet and VM Image are in the same location. Can you tell me what will be the command for that? – CrazyCoder Aug 28 '18 at 06:24
  • @CrazyCoder You can try this [link](https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/new-azurermvm?view=azurermps-6.7.0#examples). It will create a network interface and associate the nic with the subnet in another resource group and set the vm config. – Charles Xu Aug 28 '18 at 06:40
  • Hi, I have tried the method mentioned in the link. I have updated the question based on the trials I have done. Something is not right in my cmdlet. Can you please have a look. I know I'm asking for more, but I really need to solve this. – CrazyCoder Aug 28 '18 at 07:42
  • @CrazyCoder Actually, you just to create a public IP and a network interface associated with the subnet in another resource group. – Charles Xu Aug 28 '18 at 07:53
  • @CrazyCoder I update the answer, you can take a try. – Charles Xu Aug 28 '18 at 08:22
  • If I do not want public IP for the VM, can I remove this parameter --> -PublicIpAddressId $PIP.Id – CrazyCoder Aug 28 '18 at 08:44
  • @CrazyCoder Yes, you can try it. I succeeded in creating the nic without a public IP. But I do not try it until the end of creating the vm. – Charles Xu Aug 28 '18 at 08:49