New-AzureRmVM : Parameter set cannot be resolved using the specified named parameters - powershell

I am new to Powershell, I am running following script and getting the error as mentioned in the subject line:
$LocationName = "West Europe"
$VNet_ResourceGroupName = "Mydeveloper_resource_group"
$VM_ResourceGroupName = "MyDeveloperResourceGroup"
$PublicIpAddress = "[]"
$PrivateIpAddress = "10.199.120.30"
$VirtualNetworkName = "developer_vnet"
$SubnetName = "MySubnet"
$SubnetId = "/subscriptions/....../resourceGroups/developer_resource_group/providers/Microsoft.Network/virtualNetworks/developer_vnet/subnets/MySubnet"
$VMName = "AWE4DVM022"
$VMSize = "Standard_D4s_v3"
$AllocationMethod = "Static"
$ImageId = "/subscriptions/................................./resourceGroups/MyIMAGELIBRARY/providers/Microsoft.Compute/images/developer-desktop-image-...................."
$cred = Get-Credential -Message "Type the name and password of the local administrator account."
$nic = New-AzureRmNetworkInterface -Name TestNIC -ResourceGroupName $VM_ResourceGroupName -Location $LocationName -SubnetId $SubnetId -PrivateIpAddress $PrivateIpAddress
$vm = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Linux -ComputerName $VMName -Credential $cred
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
New-AzureRmVM -ResourceGroupName $VM_ResourceGroupName -ImageName $ImageId -VM $vm

Powershell has this concept of parameter sets. When you call a script, function, or commandlet that uses parameter sets (like New-AzureRMVM does), you can't pass parameters for arguments in different sets. For example, New-AzureRmVM has three parameters sets. ResourceGroupName is in parameter sets 1, 2, and 3. ImageName is in parameter set 1, and VM is in parameter set 2. Powershell can't figure out what set to use because ImageName is on one set while VM is in another.

Related

Set-AzureRmVMCustomScriptExtension before Creating VM

When running the below Azure Powershell, I get error:
Set-AzureRmVMCustomScriptExtension : Can not perform requested
operation on nested resource. Parent resource 'mycomputer' not found.
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Set-AzureRmVMSourceImage -VM $vm -Id $image.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -StorageAccountType $vmStorageType -DiskSizeInGB $vmDiskSize -CreateOption FromImage -Caching ReadWrite
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Add-AzureRmVMSecret -VM $vm -SourceVaultId $vaultId -CertificateStore "My" -CertificateUrl $certURL
$vm = Set-AzureRmVMCustomScriptExtension -ResourceGroupName $rsgName -VMName $vmName -Name $extenstionName -Location $location -StorageAccountName $storageName -StorageAccountKey $storageKey -FileName $fileName -ContainerName $containerName -Run $fileName
$vm = New-AzureRmVM -VM $vm -ResourceGroupName $rsgName -Location $location
Can I not set the CustomScriptExtension as part of building the New VM?
The Set-AzureRmVMCustomScriptExtension command can be used to add the Custom Script extension to an existing virtual machine. Existing VM. so no, you cannot do that before creating a VM.
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/extensions-customscript#powershell-deployment
VM custom script extension is a separate task that need create VM firstly. So, just adjust the execution order of your script.
##create a VM, no need $vm
New-AzureRmVM -VM $vm -ResourceGroupName $rsgName -Location $location
##create custom script extension
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $rsgName -VMName $vmName -Name $extenstionName -Location $location -StorageAccountName $storageName -StorageAccountKey $storageKey -FileName $fileName -ContainerName

Trying to create Palo Alto VM with Azure Powershell

I'm currently trying to create a Palo Alto VM-Series firewall with Azure Powershell to later incorporate some changes.
I have the following relevant code
$VM = Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$VM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
Set-AzureRmVMPlan -VM $VM -Publisher paloaltonetworks -Product vmseries1 -Name "bundle2"
# Specify the OS disk name and create the VM
$DiskName='OSDisk-'+$VMName
$SA = Get-AzureRmStorageAccount -Name $SAName -ResourceGroupName $RGName
$OSDiskUri = $SA.PrimaryEndpoints.Blob.ToString() + "vhds/" + $VMName+".vhd"
$VM = Set-AzureRmVMOSDisk -VM $VM -Name $DiskName -VhdUri $OSDiskUri -CreateOption fromImage
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $VNIC01.Id -Primary
New-AzureRmVM -ResourceGroupName $RGName -Location $Region -VM $VM -Verbose
I get the error
New-AzureRmVM : Changing property 'osDisk.createOption' is not allowed.
ErrorCode: PropertyChangeNotAllowed
ErrorMessage: Changing property 'osDisk.createOption' is not allowed.
StatusCode: 409
ReasonPhrase: Conflict
OperationID : 882848ca-7053-4098-9599-d25d58b4b3fe
At C:\Users\IEUser\Desktop\DeployMultipleNicPANWv0.2.ps1:192 char:1
+ New-AzureRmVM -ResourceGroupName $RGName -Location $Region -VM $VM -V ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand
I had previously tried some changes since it tells me the origin vhd was created with Premium Storage and I have a Standard Storage.
Could someone point me to the right direction?
Thanks
According to your code, I found it the scripts contain
“$VM = Set-AzureRmVMOSDisk -VM $VM -Name $DiskName -VhdUri $OSDiskUri -CreateOption fromImage”.
And the error code is 409. This issue may caused by this :VM created from Image cannot have blob based disks. All disks have to be managed disks.
So, you should decide what source do you want to use to create VM. If you just want to use image to create VM, you should not add -VhdUri $OSDiskUri
More about how to use image to create a new VM with Powershell, refer to this document.
Also, you can use templates to create Palo Alto VM easily. More about how to create Palo Alto VM with templates, refer to this link.
----------Update----------
Ensure your image and other requirements had properly been configured ,and then you can use following Powershell scripts to create:
$VM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VM = Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
Set-AzureRmVMPlan -VM $VM -Publisher paloaltonetworks -Product vmseries1 -Name "bundle2"
$VM = Set-AzureRmVMOSDisk -VM $VM -CreateOption fromImage -Caching ReadWrite
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $VNIC01.Id -Primary
New-AzureRmVM -ResourceGroupName $RGName -Location $Region -VM $VM -Verbose
NOTE: With managed disk, we cannot change OSdiskname when we use image or vdi to create VM. If you still cannot create it, I suggest you use the templates to create it. Just click deploy on Azure and then supply required information.

How does one convert PSVirtualMachineObjects?

I'm trying to deploy Azure VM's thru a workflow so it could be done in parallel. The code works fine outside of a workflow. But getting this error when trying to do it thru a workflow.
I'm importing the VM parameters thru a csv file.
Are there additional considerations for deploying Azure VM's thru a Workflow?
Workflow Deploy-VMs {
$cred1= New-Object System.Management.Automation.PSCredential "User",$(ConvertTo-SecureString "Password" -asplaintext -force)
$b=Import-Csv Y:\NLG\vms1.csv -Verbose|? type -eq 'VM'
foreach ($c in $b) {
AzureRM.Resources\Login-AzureRmAccount -Credential $cred1 -SubscriptionId subscription id
$nic = New-AzureRmNetworkInterface -Name $c.Name -ResourceGroupName nlg -Location $c.Location -SubnetId $c.SubnetID
$cred= New-Object System.Management.Automation.PSCredential "nladmin",$(ConvertTo-SecureString $c.Password -asplaintext -force)
$vmConfig = New-AzureRmVMConfig -VMName $c.Name -VMSize "Standard_D1"
$vmConfig = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $c.Name -Credential $cred
$vmConfig = Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter-smalldisk" -Version "latest"
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $c.Name -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $c.RG -Location $c.Location -VM $vmConfig
}
}
and getting this error
Cannot bind parameter 'VM'. Cannot convert value
"Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine" to type
"Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine". Error:
"Cannot convert the
"Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine" value of
type
"Deserialized.Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine"
to type "Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine"."
+ CategoryInfo : InvalidArgument: (:) [Set-AzureRmVMOperatingSystem], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.Compute.SetAzureVMOperatingSystemCommand
+ PSComputerName : [localhost]
Resolved using inline script for the incompatible cmdlets.
Workflow Deploy-VMs {
$cred1 = New-Object System.Management.Automation.PSCredential "User", $(ConvertTo-SecureString "Password" -AsPlainText -Force)
$b = Import-Csv Y:\NLG\vms1.csv -Verbose|? type -eq 'VM'
foreach -Parallel ($c in $b) {
AzureRM.Resources\Login-AzureRmAccount -Credential $cred1 -SubscriptionId c2d7e81b-ed6a-4de9-a4cd-36e679ec4259
$nic = New-AzureRmNetworkInterface -Name $c.Name -ResourceGroupName nlg -Location $c.Location -SubnetId $c.SubnetID
$cred = New-Object System.Management.Automation.PSCredential "nladmin", $(ConvertTo-SecureString $c.Password -AsPlainText -Force)
InlineScript {
$vmConfig = New-AzureRmVMConfig -VMName $using:c.Name -VMSize "Standard_D1"
$vmConfig = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $using:c.Name -Credential $using:cred
$vmConfig = Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter-smalldisk" -Version "latest"
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $using:nic.Id
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $using:c.Name -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $using:c.RG -Location $using:c.Location -VM $vmConfig
}
}
}

How to assign a network interface to a newly created VM with ARM Powershell?

I'm getting started with ARM in our Azure tenancy...
Firstly, I created a resource group, TEST-RG.
I've created a Network Interface in my resource group using the Portal. Let's call it TEST-NIC in resource group TEST-RG.
I've also created a DS2_V2 VM called TEST-VM, running Windows 2012 R2 and also in TEST-RG.
How do I associate TEST-NIC with TEST-VM? I can't see a way of doing that in the Portal, nor can I see a way in the Portal of creating a VM with no network interface, such that I can add TEST-NIC later.
So, I assume this can only be done in PS, but I'm not at all clear how...
Have I lost the plot? Any guidance would be appreciated.
Thanks
For now, Azure does not support add a NIC to an existing VM.
can I see a way in the Portal of creating a VM with no network
interface, such that I can add TEST-NIC later
We can't create a VM without NIC, but we can use PowerShell to create a new VM with this NIC.
Here is my script to create a new VM with existing NIC.
$ResourceGroupName = "nic"
$Location = "Eastus"
$StorageName = "jasondisk321"
$StorageType = "Standard_LRS"
$VMName = "myvm"
$VMSize = "Standard_DS2_v2"
$OSDiskName = $VMName + "OSDisk"
$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -Type $StorageType -Location $Location
$nic = Get-AzureRmNetworkInterface -Name jasonnic -ResourceGroupName $rgname
$nicId = $nic.Id
$Credential = Get-Credential
$vm = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$vm = Set-AzureRmVMOperatingSystem -VM $vm -ComputerName $VMName -Windows -Credential $Credential
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $vm
Firstly, you can't deploy an ARM VM without a NIC.
If you look at this script
It has the lines
$Interface = New-AzureRmNetworkInterface -Name $InterfaceName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-SubnetId $VNet.Subnets[0].Id `
-PublicIpAddressId $PIp.Id
Which is later used by
$VirtualMachine = Add-AzureRmVMNetworkInterface `
-VM $VirtualMachine `
-Id $Interface.Id
So if you want to use an existing NIC you just need to pass the $interface.id (from Get-AzureRmVMNetworkInterface)
When you wrap those two around the rest of a VM deployment script you'll build a VM with a NIC

Azure: Error Deploying VM from Generalised Image (Image Undeployable)

I've been trying to use Powershell (following MS docs) to deploy a new VM from a generalised image but I keep getting this error.
New-AzureRmVM : Long running operation failed with status 'Failed'.
ErrorCode: OSProvisioningClientError
ErrorMessage: OS provisioning for VM 'MyVM' failed. Error details: This installation of Windows is undeployable. Make sure the image has been properly prepared (generalized).
Instructions for Windows: https://azure.microsoft.com/documentation/articles/virtual-machines-windows-upload-image/
StartTime: 22/03/2017 10:06:24
EndTime: 22/03/2017 10:10:37
OperationID: 549f97d1-ca39-4bc5-bd6c-65e37a8d398f
Status: Failed
At C:\Visual Studio Projects\Deployment\VmSetup.ps1:97 char:1
+ New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand
Trouble is that I did run a sysprep and generalised the image.
Code : OSState/generalized
Level : Info
DisplayStatus : VM generalized
Message :
Time :
I'm also unable to start the VM again because it's told me it's generalised.
Failed to start virtual machine 'OtherVM'. Error: Operation 'start' is not allowed on VM 'OtherVM' since the VM is generalized.
Image saving script:
#Script to save image
$vmName = "OtherVM"
$rgName = "MyResourceGroup"
#Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName
Set-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Generalized
$vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Status
$vm.Statuses
#Save VM Image
Save-AzureRmVMImage -ResourceGroupName $rgName -Name $vmName `
-DestinationContainerName "generalisedimages" -VHDNamePrefix "gen" `
#-Path "C:\VMTemplate\VmTemplate.json"
Write-Output "Imaged saved."
And the deploy script.
#
# VmDeploy.ps1
#
#Sign into Azure account
#Login-AzureRmAccount
# Name of the virtual machine. This example sets the VM name as "myVM".
$vmName = "MyVM"
#Set resource group and subnet name
$rgName = "MyResourceGroup"
$subnetName = "default"
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
#Set location and vNet name
$location = "UK South"
$vnetName = "{0}-vnet" -f $rgName
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet
#Create public IP
$ipName = "{0}-ip" -f $vmName
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
#Create NIC
$nicName = "{0}-nic" -f $vmName
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
#Create network security group and allow RDP
$nsgName = "{0}-nsg" -f $vmName
$rdpRule = New-AzureRmNetworkSecurityRuleConfig -Name Rdp -Description "Allow RDP" `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 110 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 3389
$httpRule = New-AzureRmNetworkSecurityRuleConfig -Name Http -Description "Allow HTTP" `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 120 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 80
$httpsRule = New-AzureRmNetworkSecurityRuleConfig -Name Https -Description "Allow HTTPS" `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 130 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 443
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName -Location $location -Name $nsgName -SecurityRules $rdpRule, $httpRule, $httpsRule
#Get completed virtual network
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -Name $vnetName
#Uri of VM image
$imageURI = "https://*******.blob.core.windows.net/system/Microsoft.Compute/Images/generalisedimages/genosDisk.86g419f6-0de6-4331-hi54-32hse8de6bd4.vhd"
# Enter a new user name and password to use as the local administrator account
# for remotely accessing the VM.
$cred = Get-Credential
# Name of the storage account where the VHD is located. This example sets the
# storage account name as "myStorageAccount"
$storageRgName = $rgName
$storageAccName = "mystorage"
# Size of the virtual machine. This example creates "Standard_D2_v2" sized VM.
# See the VM sizes documentation for more information:
# https://azure.microsoft.com/documentation/articles/virtual-machines-windows-sizes/
$vmSize = "Standard_A1"
# Computer name for the VM. This examples sets the computer name as "myComputer".
$computerName = "New VM"
# Name of the disk that holds the OS. This example sets the
# OS disk name as "myOsDisk"
$osDiskName = "OsDisk"
# Assign a SKU name. This example sets the SKU name as "Standard_LRS"
# Valid values for -SkuName are: Standard_LRS - locally redundant storage, Standard_ZRS - zone redundant
# storage, Standard_GRS - geo redundant storage, Standard_RAGRS - read access geo redundant storage,
# Premium_LRS - premium locally redundant storage.
$skuName = "Standard_LRS"
# Get the storage account where the uploaded image is stored
$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $storageRgName -AccountName $storageAccName
Write-Output $storageAcc
# Set the VM name and size
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
#Set the Windows operating system configuration and add the NIC
$vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName `
-Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
# Create the OS disk URI
$osDiskUri = '{0}vhds/{1}-{2}.vhd' `
-f $storageAcc.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $osDiskName
Write-Output "OS Disk URI:" $osDiskUri
# Configure the OS disk to be created from the existing VHD image (-CreateOption fromImage).
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $imageURI -Windows
Write-Output $vm
# Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm
$vmList = Get-AzureRmVM -ResourceGroupName $rgName
$vmList.Name
OS provisioning for VM 'MyVM' failed. Error details: This installation
of Windows is undeployable. Make sure the image has been properly
prepared (generalized).
According to the error message, it seems you have not generalized VM correctly.
we should RDP to Azure VM, and run sysprep, in the System Preparation Tool dialog box, select Enter System Out-of-Box Experience (OOBE), and make sure that the Generalize check box is selected. In Shutdown Options, select Shutdown.
More information about Generalize a Windows virtual machine, please refer to this link.
I'm also unable to start the VM again because it's told me it's
generalised
Capture an image of an Azure widnows VM, this process deletes the original virtual machine after it is captured.
Prior to capturing an image of an Azure virtual machine, it is recommended the target virtual machine be backed up