0
votes

I'm trying to set up an architecture in azure with a script Powershell. But I have an unexpected error telling me that the request is : "ReasonPhrase: Bad Request"

My only documentation for this project is the microsoft doc. This is where I found this command and some exemple of script for creating a VM with powershell.

My real problem is that this cmdlet works when I use a script which create one VM in one run. But in this script I am looking to first create all network interfaces before hand and then merge them to their VM.

$LocationName = "West Europe"
$ResourceGroupName = "RGPackage01","RGLab01","RGFileStock01"

######################Variable des machines##################################

<#
.Explication:
Toutes les variables relatives aux configuration des os  
#>

$VMSize = "Standard_DS3"

###################### Variable de réseau ####################################

<#
.Explication:
Il faut un nom DNS pour chaque machine,
mettre en place le nom et la configuration des réseau et VLAN
#>

$DNSNameLabel = "cdsvm" # .westeurope.cloudapp.azure.com
$NetworkName = "VnCDS"
$NICName = "MyNIC1","MyNIC2","MyNIC3"
$PublicIPAddressName = "MyPIP"
$SubnetName = "SubnetPacakger","SubnetTestPacakger","SubnetLABSCCM","SubnetLABLibre"
$SubnetAddressPrefix = "10.30.40.0/26","10.30.40.64/26","10.30.40.128/26","10.30.40.192/26"
$VnetAddressPrefix = "10.30.40.0/24"

##################### Création des groupes de Ressources ###########################

<#
.Explication:
Utilisation d'une boucle pour créer les différents groupe de ressources defini dans la nomenclature
#>

for($i=0;$i -le 2;$i++){
Create-NewRG -matricule $ResourceGroupName[$i] -localisation $LocationName
write-host "Creating RG : $($ResourceGroupName[$i])" -ForegroundColor Green
}


##################### Création du réseau ##########################################

<#
.Explication:
Utilisation de boucle pour créer les différents sous réseau défini dans l'adressage reseau
#>



$SubnetPacakger = New-AzureRmVirtualNetworkSubnetConfig 
-Name $Subnetname[0] 
-AddressPrefix $SubnetAddressPrefix[0]

$SubnetTestPacakger = New-AzureRmVirtualNetworkSubnetConfig 
-Name $Subnetname[1] 
-AddressPrefix $SubnetAddressPrefix[1]

$SubnetLABSCCM = New-AzureRmVirtualNetworkSubnetConfig 
-Name $Subnetname[2] 
-AddressPrefix $SubnetAddressPrefix[2]

$SubnetLABLibre = New-AzureRmVirtualNetworkSubnetConfig 
-Name $Subnetname[3] 
-AddressPrefix $SubnetAddressPrefix[3]

$SubnetGlossaire = $SubnetPacakger,$SubnetTestPacakger,$SubnetLABSCCM,$SubnetLABLibre

$Vnet = New-AzureRmVirtualNetwork 
-Name $NetworkName 
-ResourceGroupName $ResourceGroupName[0] 
-Location $LocationName 
-AddressPrefix $VnetAddressPrefix 
-Subnet $SubnetGlossaire


###################### Création des interfaces réseau ##############################

<#
.Explication:
Creation des interfaces réseau/carte réseau des machines virtuels 
#>

$PIP = New-AzureRmPublicIpAddress 
-Name $PublicIPAddressName 
-DomainNameLabel $DNSNameLabel 
-ResourceGroupName $ResourceGroupName[0] 
-Location $LocationName 
-AllocationMethod Dynamic

$NIC = New-AzureRmNetworkInterface 
-Name $NICName[0] 
-ResourceGroupName $ResourceGroupName[0] 
-Location $LocationName 
-SubnetId $Vnet.Subnets[0].Id 
-PublicIpAddressId $PIP.Id

This is now working, the issue was from the fact that an network interface need a subnet to be created. I didn't created subnet but only a virtual network. So I firts create the config of 4 subnet with New-AzureRmVirtualNetworkSubnetConfig and I link them to the virtual network when i created it New-AzureRmVirtualNetwork -Subnet.

Now In the command New-AzureRmNetworkInterface I can right in the argument -SubnetId $Vnet.Subnets[0].Id. It's going to look for the first subnet id in the virtual network.

1
That's it I edited my question including your solution, thank you for the helpBruno SENELLART

1 Answers

0
votes

$subnetid[1].Id is the id of virtual network, not the id of the subnet. The value of SubnetId should be something like

-SubnetId "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ResourceGroup1/providers/Microsoft.Network/virtualNetworks/VirtualNetwork1/subnets/Subnet1"

You missed /subnets/Subnet1.

You should use $subnetid[1].Subnets[0].Id as the value of SubnetId.