# Create Azure VMs Function help

**URL:** <https://forums.powershell.org/t/create-azure-vms-function-help/4343>\
**Category:** PowerShell Help\
**Created:** [June 10, 2015, 2:56am UTC](https://forums.powershell.org/t/create-azure-vms-function-help/4343 "2015-06-10T02:56:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![windows-liveuser20](https://avatars.discourse-cdn.com/v4/letter/w/ebca7d/32.png) [@windows-liveuser20](https://forums.powershell.org/u/windows-liveuser20)\
**Post date:** [June 10, 2015, 2:56am UTC](https://forums.powershell.org/t/create-azure-vms-function-help/4343/1 "2015-06-10T02:56:59Z")

</div>

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 …
- 

```auto
  + CategoryInfo : CloseError: (:) [New-AzureVM], CloudException
  + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.PersistentVMs.NewAzureVMCommand

```

Any help would be great. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 10, 2015, 4:55am UTC](https://forums.powershell.org/t/create-azure-vms-function-help/4343/2 "2015-06-10T04:55:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![windows-liveuser20](https://avatars.discourse-cdn.com/v4/letter/w/ebca7d/32.png) [@windows-liveuser20](https://forums.powershell.org/u/windows-liveuser20)\
**Post date:** [June 10, 2015, 5:23am UTC](https://forums.powershell.org/t/create-azure-vms-function-help/4343/3 "2015-06-10T05:23:59Z")

</div>

Don,

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

---

<div class="post-metadata">

**Author:** ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)\
**Post date:** [June 10, 2015, 6:05am UTC](https://forums.powershell.org/t/create-azure-vms-function-help/4343/4 "2015-06-10T06:05:43Z")

</div>

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.

> **[Search Results for “automating server builds in azure” – The Last Word In Nerd](https://lastwordinnerd.com/?s=automating%2Bserver%2Bbuilds%2Bin%2Bazure)**

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

> **[Automation Tails from the FoxHole](https://www.foxdeploy.com/)**
>
> FoxDeploy.com, Stephen Owen's technical blog about
> PowerShell, Systems Administration, GUI Design and Programming.
> .

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/create-azure-vms-function-help/4343/5 "2024-05-16T20:45:12Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


