Create Azure VMs Function help

This topic contains 3 replies, has 3 voices, and was last updated by  Will Anderson 3 years ago.

  • Author
  • #26265

    E Mesh

    The script runs with no issues but when I put in a function and add parameters it stops working. The parameters are not passed to the variables and get an error. The script is to provision Azure VMs.

    Here is the script:

    function Create-AzureVm {
    #subscription name ( run get-azuresubscription: subscriptionname property)
    #storage account name ( run get-azuresbuscription: label property)
    Select-AzureSubscription -SubscriptionName $subscr –Current
    Set-AzureSubscription -SubscriptionName $subscr -CurrentStorageAccountName $staccount
    #Set the VM image Family you would like. 
    $family="Windows Server 2012 R2 Datacenter"
    $image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1
    # Set the name of the VM
    # Set the size of the VM ( only specify one)
    # Add VM to Domain
    $cred1=Get-Credential –Message "Type the name and password of the local administrator account."
    $cred2=Get-Credential –Message "Now type the name (not including the domain) and password of an account that has permission to add the machine to the domain."
    $vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image |
        Add-AzureProvisioningConfig -WindowsDomain -JoinDomain $domaindns -Domain $domain -DomainUserName $cred2.GetNetworkCredential().Username -DomainPassword $cred2.GetNetworkCredential().Password -AdminUsername $cred1.GetNetworkCredential().Username -Password $cred1.GetNetworkCredential().Password |
        Set-AzureSubnet -SubnetNames "Servers" 
    New-AzureVM –ServiceName $svcname -location "North Central US" -VMs $vm1 -VNetName $vnetname

    The error:
    New-AzureVM : ResourceNotFound: The hosted service does not exist.
    At C:\Users\******\Documents\Azure- Create VM.ps1:40 char:1
    + New-AzureVM –ServiceName $svcname -location "North Central US" -VMs $vm1 -VNetNa ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [New-AzureVM], CloudException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.PersistentVMs.NewAzureVMCommand

    Any help would be great. Thanks in advance!

  • #26280

    Don Jones

    You've declared the parameters as arrays, but within the script you're using them as if they contain only a single value. Try using [string] instead of [string[]].

    Also, please consider formatting your code when you post. Instructions for doing so are displayed right above the post textbox.

  • #26283

    E Mesh


    thanks for your response. I have also tried using [string] but I'm still encountering the same error.

  • #26288

    Will Anderson

    Hey there LiveUser20. You need to create the Azure Cloud Service before deploying the VM. I've actually done some work for creating functions to deploy VMs in Azure. You might want to give it a read. I ran into quite a few challenges.

    You might also want to check out Stephen Owen's blogs on Azure deployments. He covers a lot of stuff too.

You must be logged in to reply to this topic.