Create Azure VMs Function help

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 {
    [CmdletBinding()]
param(
     [String[]]$vmname,
     [String[]]$svcname 
)
#subscription name ( run get-azuresubscription: subscriptionname property)
$subscr="Enterprise"

#storage account name ( run get-azuresbuscription: label property)
$staccount="******"
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
#$vmname="test1"
# Set the size of the VM ( only specify one)
$vmsize="Standard_D2"


#$svcname="azr-d-test1"
$vnetname="VNET1"


# 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."
$domaindns="test.com"
$domain="testdomain"


$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!

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.

Don,

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

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.