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!