When I deploy with ARM template with both template and parameter file in local folder, it works with no issues. But, same file if in blob container, it prompts me for all parameters. Quite strange and confused. :(
This structure works!
Parameter Set: Deployment via template file and template parameters file
New-AzureRmResourceGroupDeployment -ResourceGroupName -TemplateFile >> -TemplateParameterFile [-Force] [-Mode >>{Incremental | Complete} ] [-Name ] [-StorageAccountName ] [->>TemplateVersion ] [ ]
My above trial is as below and it works:
$TemplateFile = "F:\servicerequestsnippets\FW__Code\Orig\Orig\trial\azuredeploy.json"
$TemplateParameterFile = "F:\servicerequestsnippets\FW__Code\Orig\Orig\trial\azuredeploy.parameters.json"
.
.
.
Test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParameterFile `
-Verbose
But, Parameter file uri approach as below do not work. :(
$TemplateFile = "F:\servicerequestsnippets\FW__Code\Orig\Orig\trial\azuredeploy.json"
$TemplateParametersFile = "https://hbala.blob.core.windows.net/test-container/"
.
.
{
foreach ($blob in $blobs)
{
$TemplateParameterFileLink = $TemplateParametersFile + $blob.Name
write-host $TemplateParameterFileLink
Test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParameterFileLink `
-Verbose
}
}
Note:
a. I even tried TemplateParameterUri but got same Debugmessage as:
cmdlet Test-AzureRmResourceGroupDeployment at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
**administratorLogin:** << *
it is not saying of conflict , and paramfromTemplate.
It would great to get some pointers or what you feel I am missing. Thanks in advance.
Regards,
H Bala
Related
Is there any way to catch an error when creating a new Hyper-V VM with Powershell?
New-VM -Name $vmname `
-MemoryStartupBytes $memorySize `
-Path D:\Hyper-V\ `
-NewVHDPath D:\Hyper-V\$vmname\$vmname.vhdx `
-NewVHDSizeBytes $diskSize `
-Generation 2 `
-SwitchName "vSwitch"
As far as I can see, there is no way to add -ErrorAction Stop to New-VM.
Errors can occur, for example, if the virtual switch does not exist. In this case no further tasks should be processed and the script should be terminated.
What you are saying is not error. You have to have a conditional check. I have given a rough skeleton for you to proceed along with error handling:
try ##runs everything in the try block to capture and error in the catch block
{
If(Get-VM -Name $vmname) ##checks if the VM is existing or not by fetching the VM information. It has further parameters. Kindly google about them for better understanding
{
"$vmname is already present"
}
else
{
## Only creates the VM when it doesnt exist
New-VM -Name $vmname `
-MemoryStartupBytes $memorySize `
-Path D:\Hyper-V\ `
-NewVHDPath D:\Hyper-V\$vmname\$vmname.vhdx `
-NewVHDSizeBytes $diskSize `
-Generation 2 `
-SwitchName "vSwitch"
}
}
catch
{
$_.Exception.Message ## Only captures the error exception message and not the entire error.
}
I have given explanations in the Comment block for you to understand it better.
Looking for a script to autogenerate a new storage account
with a prefix dev something like dev01.... and when i rerun the template should increment as dev02.. on second run and so on.
I tried giving the parameters / using default templates in github.
Issue is if i pass on a value under the value the system deploys it fine ,if i give the same name it would rerun and update the existing storage.
Instead i would like it to check if storage account exists and if not create a new account , please advise any pointers if any
Sample Parameters.Json file im using :
"parameters": {
"storageAccountName": {
"value": "dev01"
},
I recommand that you could use the Azure powershell script to customize your logic to do that.
The following is the demo code:
$resourceGroup = "rgName"
$storageAccount = "accountName"
$location = "Central US"
$SkuName = "Standard_LRS"
$kind = "StorageV2"
$i = 0;
while(1)
{
$storage = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount
if($storage -ne $null)
{
$i++
$storageAccount = $storage.StorageAccountName + $i
$storage = New-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount -SkuName $SkuName -Location $location -Kind $kind
}
else
{
$storage = New-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount -SkuName $SkuName -Location $location -Kind $kind
$storageAccount = $storageAccount +$i;
}
if ($storage -ne $null)
{
break;
}
}
Task:
I am trying to deploy an ARM template.
here are the template and parameters file path:
[string]
$templateFilePath = "C:\Users\Desktop\template_abc.json",
[string]
$parametersFilePath = "C:\Users\Desktop\parameters_xyz.json"
)
Deployment commands:
Write-Host "Starting deployment...";
if(Test-Path $parametersFilePath) {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath -TemplateParameterFile $parametersFilePath;
}
else {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath;
}
I want to add one more parameter file to my deployment, like : parameters_lmn.json, how can I accommodate both xyz and lmn parameter file in a single deployment?
You could use https://powersnippets.com/join-object/ to merge the two json object.
You'll need to decide which parameter file overwrites the other in case both define the same parameter.
I have hunted around for an answer to this, but I am not having much luck. All the articles I can find are either setting up a Point-to-Site or are instructions for classic Azure, not Azure 2.0 (Resource Group)
Currently, we are dialing up a whole new resource group everytime we do a new built. This consists of Web apps and SQL DBs. When we have a new build we start up the new and del the old resource group. Simple. To minimize the start-up time we have a static resource group that isn't deleted that houses the VPN connection to our on Prem resources.
The problem I'm having is when I add the new websites using AzureRM Powershell cmd's to the Point-to-site it says it's successful. The Azure Portal says its good but it does let me communicate. If I remove and add it from one of the 8 WebApps they all start working.
I am out of ideas. Any help would be greatly appreciated.
Azure VPN
Below is the function I have put togeather from what I can find out there.
function AddExistingVnet{
param(
[string] $subscriptionId,
[string] $resourceGroupName,
[string] $webAppName
)
$Vnet = Get-AzureRmVirtualNetwork | Where-Object {$_.ResourceGroupName -like "*Static*"}
IF($Vnet.Name.count -gt 1) {write-host 'Two or networks have been returned. Unable to continue ' return}
$gatewaySubnet = $vnet.Subnets | Where-Object { $_.Name -eq "GatewaySubnet" }
$vnetName = $vnet.Name
$uriParts = $gatewaySubnet.IpConfigurations[0].Id.Split('/')
$gatewayResourceGroup = $uriParts[4]
$gatewayName = $uriParts[8]
$gateway = Get-AzureRmVirtualNetworkGateway -ResourceGroupName $vnet.ResourceGroupName -Name $gatewayName
Write-Host "Creating App association to VNET"
$propertiesObject = #{
"vnetResourceId" = "/subscriptions/$($subscriptionId)/resourceGroups/$($vnet.ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/$($vnetName)"
}
$virtualNetwork = New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -Force
# Now finish joining by getting the VPN package and giving it to the App
Write-Host "Retrieving VPN Package and supplying to App"
$packageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $vnet.ResourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64
# Put the VPN client configuration package onto the App
$PropertiesObject = #{
"vnetName" = $vnet.Name; "vpnPackageUri" = $packageUri
}
New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)/primary" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections/gateways" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -WarningAction silentlyContinue -Force
}
So after 2 weeks of going back and forth with Microsoft (had a really good guy Charles) we managed to find the problem.
When requesting
$packageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $vnet.ResourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64
It was giving me an output of:
"https://mdsbrketwprodsn1prod.blob.core.windows.net/cmakexe/xxx~xxx/amd64/xxxx~xxxx&sp=r&fileExtension=.exe"
For some reason (that Microsoft could explain) why it kept adding in " to the beginning and end of the variable.
I find it odd that it lets the script work with " and allows the WebApps to join to the VPN.
Any why here is the fix which basicly removes the " from the begining and end of $packageUri :
$packageUri = $packageUri.ToString();
$packageUri = $packageUri.Substring(1, $packageUri.Length-2);
So hope that helps someone else out there who is banging there head agaist the same problem.
Here is the complete function if any one is intrested:
function AddExistingVnet{
param(
[string] $subscriptionId,
[string] $resourceGroupName,
[string] $webAppName
)
$Vnet = Get-AzureRmVirtualNetwork | Where-Object {$_.ResourceGroupName -like "*Static*"}
IF($Vnet.Name.count -gt 1) {write-host 'Two or networks have been returned. Unable to continue ' return}
$gatewaySubnet = $vnet.Subnets | Where-Object { $_.Name -eq "GatewaySubnet" }
$vnetName = $vnet.Name
$uriParts = $gatewaySubnet.IpConfigurations[0].Id.Split('/')
$gatewayResourceGroup = $uriParts[4]
$gatewayName = $uriParts[8]
$gateway = Get-AzureRmVirtualNetworkGateway -ResourceGroupName $vnet.ResourceGroupName -Name $gatewayName
$webApp = Get-AzureRmResource -ResourceName $webAppName -ResourceType "Microsoft.Web/sites" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName
$location = $webApp.Location
Write-Host "Creating App association to VNET"
$propertiesObject = #{
"vnetResourceId" = "/subscriptions/$($subscriptionId)/resourceGroups/$($vnet.ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/$($vnetName)"
}
$virtualNetwork = New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -Force
# Now finish joining by getting the VPN package and giving it to the App
Write-Host "Retrieving VPN Package and supplying to App"
$packageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $vnet.ResourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64
$packageUri = $packageUri.ToString();
$packageUri = $packageUri.Substring(1, $packageUri.Length-2);
# Put the VPN client configuration package onto the App
$PropertiesObject = #{
"vnetName" = $vnet.Name; "vpnPackageUri" = $packageUri.ToString()
}
$date = Get-Date -format "HH:mm tt"
New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)/primary" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections/gateways" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -WarningAction silentlyContinue -Force
}
Enjoy
Login-AzureRmAccount
$params = #{SqlServerName="myServer";SqlDatabaseName="myDb";SqlUserName="myDbUser"}
Register-AzureRmAutomationScheduledRunbook `
–Name "AutoExport" `
-ResourceGroupName "myAzureAutomation" `
–AutomationAccountName "myAutomation" `
–ScheduleName "myScedule" `
–Parameters $params `
-ErrorAction Stop;
And when I go on the Azure Portal parameters contain quoted string
Example:
Eventually my Workbook will crash because values expected no quotes. I know, I can remove quotes into script of my Workbook but it seem to me that is not an elegant solution.
What I doing wrong!