I would like to run a script which creates a VM in Azure.
These are the mandatory parameters:
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
$RG = 'AZ-PS-RG'
$VM_NAME = 'AZ-PS-VM'
$location = 'francecentral'
$image = 'UbuntuLTS'
$size = 'Standard_D2s_v3'
$vnetName='myVNet'
$SubnetName = 'mySubnet-ps'
I would also like to provide two optional parameters for PublicIpAddressName and DomainNameLabel:
$dnsName=''
$reservedIP= ''
The idea is that the script should check if those parameters are empty or not. If they are empty then simply create a VM with only mandatory parameters:
$vmParams = @{
ResourceGroupName = $RG
Name = $VM_NAME
Location = $location
ImageName = $image
Credential = $cred
VirtualNetworkName = $vnetName
SubnetName = $SubnetName
Size = $size
# these two I would like to run only if they are provided
PublicIpAddressName = $reservedIP
DomainNameLabel = $dnsName
}
$newVM1 = New-AzVM @vmParams
If they are not, I would like to create the VM with these two additional parameters.
Does anyone know how to do it in Powershell?
New-AzVMwould you like to bind/map$dnsNameand$reservedIPto, if present?New-AzVMhas neither a-dnsNamenor a-reservedIPparameter defined?-PublicIpAddressName = $reservedIPand-DomainNameLabel = $dnsName