Convert powershell azure command into CLI - azure

I need to convert below into a cli command into a CLI command and need some help.
New-AzDnsRecordSet -Name www -RecordType A -ZoneName host.com -ResourceGroupName devdnsgroup -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "10.10.10.10")
I have started with:
az network dns record-set a add-record -g devdnsgroup -z host.com --ttl 3600
However I am unsure on how to accomplish the last part: -DnsRecords (New-AzDnsRecordConfig -IPv4Address "10.10.10.10")
Here is my reference
https://learn.microsoft.com/en-us/cli/azure/network/dns/record-set/a?view=azure-cli-latest#az-network-dns-record-set-a-add-record

You can follow the command here
az network dns record-set a add-record -g MyResourceGroup -z contoso.xyz -n www -a 10.10.10.10

Related

OS Type Conflict running Azure CLI command from PowerShell

I am running an Azure CLI command in a Windows 10 Professional PowerShell script and I receive this error:
(Conflict) Run command OS type 'Windows' does not match the target OS Linux.
PowerShell version:
Major Minor Build Revision
----- ----- ----- --------
5 1 19041 1237
The failing PowerShell script:
$ResourceGroup= "Development"
$VmName = "ubuntu-test"
az vm run-command invoke `
--resource-group $ResourceGroup `
--name $VmName `
--command-id RunPowerShellScript `
--scripts "ufw disable"
Note: The ` character is the backtick. The one on the same key as the tilde ~
The same command without line continuation backticks works:
az vm run-command invoke --resource-group Development --name ubuntu-test --command-id RunShellScript --scripts "ufw disable"
If I do a Write-Host the output is a single line with the correct arguments minus the quotes around the --script command.
Write-Host az vm run-command invoke `
--resource-group $ResourceGroup `
--name $VmName `
--command-id RunPowerShellScript `
--scripts "ufw disable"
az vm run-command invoke --resource-group Development --name ubuntu-test --command-id RunPowerShellScript --scripts ufw disable
The documentation for the AZ CLI invoke command mentions nothing about setting the OS Type.
az VM run-command invoke
I think the use of line-continuations (` at the very end of lines) is incidental to your problem.
Apart from the use of variables vs. literals, the crucial difference between your multi-line command and your working single-line command is:
--command-id RunPowerShellScript vs. --command-id RunShellScript.
It looks like the VM you're targeting is a Linux machine, and that --command-id RunPowerShellScript isn't supported there, whereas --command-id RunShellScript is.
az vm run-command list ... can apparently be used to discover supported --command-id values.

Powershell script for Azure WebApp Firewall

I have a script that removes and then adds firewall restrictions on Azure WebApp before doing a deployment. Below you will find the script
az webapp config access-restriction remove -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.103.203/32 --priority 1011
az webapp config access-restriction remove -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.173.703/32 --priority 1012
az webapp config access-restriction add -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.103.203/32 --priority 1011
az webapp config access-restriction add -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.173.703/32 --priority 1012
The issue with the above command is that, suppose someone has manually removed the firewall or the firewall does not exist for that user, then the script fails with an error in this case.
Is there a way to first check all the firewalls enabled for different users, then traverse and remove each of them and then finally again add all the firewall rules for the removed users.
Can someone please help me create this script as I am just learning scripting
Thanks
Firstly, you are using Azure CLI command rather than Power Shell command.
Here is the command for removing access restriction rule using power shell:
Remove-AzWebAppAccessRestrictionRule -ResourceGroupName "Default-Web-WestUS" -WebAppName "ContosoSite" -Name IpRule
For checking the rule exist or not, you could use Get-AzWebAppAccessRestrictionConfig.
If you want check and remove automatically, try this:
$results = (Get-AzWebAppAccessRestrictionConfig -ResourceGroupName "ResourceGroup" -Name "yourweb").MainSiteAccessRestrictions
$results
foreach($result in $results)
{
if($result){
Remove-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup" -WebAppName "yourweb" -Name $result.RuleName
sleep 10
}
}

How to mount storage to app service using Az powershell module

I am running the following command
az webapp config storage-account add -g appxxx-dfpg-dev2-web-eastus2 --name appxxx-dfpg-dev2-web-eastus2-backoffice-apsvc --storage-type AzureBlob --share-name central-imports-dev4 --access-key XXXXXXXXXXXXXXXXXXX== -a appxxxdfpgg --mount-path /central-imports -i CentralImports
And it works fine.
How I can achieve the same result using Az powershell module, I guess I need to use Az.Websites module.
What you are looking for is New-AzWebAppAzureStoragePath command, use it to create an Azure Storage path, then use Set-AzWebApp to update the web app.
Sample:
$storagePath1 = New-AzWebAppAzureStoragePath -Name "RemoteStorageAccount1" -AccountName "myaccount.files.core.windows.net" -Type AzureFiles -ShareName "someShareName" -AccessKey "some access key"
-MountPath "C:\myFolderInsideTheContainerWebApp"
$storagePath2 = New-AzWebAppAzureStoragePath -Name "RemoteStorageAccount2" -AccountName "myaccount2.files.core.windows.net" -Type AzureFiles -ShareName "someShareName2" -AccessKey "some access key 2"
-MountPath "C:\myFolderInsideTheContainerWebApp2"
Set-AzWebApp -ResourceGroup myresourcegroup -Name myapp -AzureStoragePath $storagepath1, $storagePath2

Azure VMSS with Custom Image using Powershell returned error: DiskProcessingError

I am having problem deploying vmss using custom images via powershell. The following is my code for the powershell deployment:
#New-AzureRmResourceGroup -Location southeastasia -Name arkgenegroup1
# Resource group name from above
$rg = "myvmss"
$location = "southeastasia"
# Create a config object
$vmssConfig = New-AzureRmVmssConfig -Location $location -SkuCapacity 2 -SkuName Standard_A0 -UpgradePolicyMode Automatic
# Reference a virtual machine image from the gallery
Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmssConfig -OsDiskCreateOption FromImage -ManagedDisk StandardLRS -OsDiskCaching "None" -OsDiskOsType Linux -ImageReferenceId (Get-AzureRmImage -ImageName image200817 -ResourceGroupName $rg).id
# Set up information for authenticating with the virtual machine
Set-AzureRmVmssOsProfile $vmssConfig -AdminUsername admin -AdminPassword adminpass -ComputerNamePrefix myvmss
# Create the virtual network resources
## Basics
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name "my-subnet" -AddressPrefix 10.0.0.0/24
$vnet = New-AzureRmVirtualNetwork -Name "my-network" -ResourceGroupName $rg -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
## Load balancer
$publicIP = New-AzureRmPublicIpAddress -Name "PublicIP" -ResourceGroupName $rg -Location $location -AllocationMethod Static -DomainNameLabel "myuniquedomain"
$frontendIP = New-AzureRmLoadBalancerFrontendIpConfig -Name "LB-Frontend" -PublicIpAddress $publicIP
$backendPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "LB-backend"
$probe = New-AzureRmLoadBalancerProbeConfig -Name "HealthProbe" -Protocol Tcp -Port 80 -IntervalInSeconds 15 -ProbeCount 2
$inboundNATRule1= New-AzureRmLoadBalancerRuleConfig -Name "webserver" -FrontendIpConfiguration $frontendIP -Protocol Tcp -FrontendPort 80 -BackendPort 80 -IdleTimeoutInMinutes 15 -Probe $probe -BackendAddressPool $backendPool
$inboundNATPool1 = New-AzureRmLoadBalancerInboundNatPoolConfig -Name "RDP" -FrontendIpConfigurationId $frontendIP.Id -Protocol TCP -FrontendPortRangeStart 53380 -FrontendPortRangeEnd 53390 -BackendPort 3389
New-AzureRmLoadBalancer -ResourceGroupName $rg -Name "myLB" -Location $location -FrontendIpConfiguration $frontendIP -LoadBalancingRule $inboundNATRule1 -InboundNatPool $inboundNATPool1 -BackendAddressPool $backendPool -Probe $probe
## IP address config
$ipConfig = New-AzureRmVmssIpConfig -Name "my-ipaddress" -LoadBalancerBackendAddressPoolsId $backendPool.Id -SubnetId $vnet.Subnets[0].Id -LoadBalancerInboundNatPoolsId $inboundNATPool1.Id
# Attach the virtual network to the IP object
Add-AzureRmVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmssConfig -Name "network-config" -Primary $true -IPConfiguration $ipConfig
# Create the scale set with the config object (this step might take a few minutes)
New-AzureRmVmss -ResourceGroupName $rg -Name "myvmss" -VirtualMachineScaleSet $vmssConfig
Error Code
New-AzureRmVmss : Long running operation failed with status 'Failed'.
ErrorCode: DiskProcessingError
ErrorMessage: One or more errors occurred while preparing VM disks. See disk instance view for details.
StartTime: 8/21/2017 4:59:40 PM
EndTime: 8/21/2017 5:00:02 PM
OperationID: xxxxxxx-fda7-4f37-acbb-xxxxxxxx
Status: Failed
At line:1 char:1
+ New-AzureRmVmss -ResourceGroupName $rg -Name "myvmss" -VirtualMa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureRmVmss], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Common.ComputeCloudException,Microsoft.Azure.Commands.Compute.Automation.NewAzureRmVmss
I can't seems to figure out what exactly causing the problem, the same image was able to be used to create standalone VM.
I have test it in my lab, your script works for me, here is my result:
Get-AzureRmImage -ImageName image200817 -ResourceGroupName $rg
Does this image create by Azure VM?
If yes, we should use waagent command to delete machine specific files and data. SSH to your VM then type the following command:
sudo waagent -deprovision+user
Note:
Only run this command on a VM that you intend to capture as an image.
It does not guarantee that the image is cleared of all sensitive
information or is suitable for redistribution. The +user parameter
also removes the last provisioned user account. If you want to keep
account credentials in the VM, just use -deprovision to leave the user
account in place.
After run this command completed, we can use CLI to create VM image, we can follow those steps:
1.Deallocate the VM
az vm deallocate \
--resource-group myResourceGroup \
--name myVM
2.Mark the VM as generalized
az vm generalize \
--resource-group myResourceGroup \
--name myVM
3.create an image of the VM
az image create \
--resource-group myResourceGroup \
--name myImage --source myVM
After those script run completed, we can use your powershell script to deploy VMSS with that image.
More information about create an image of a virtual machine or VHD, please refer to this article.

Azure Custom script extension for Linux

I am trying to provision and configure a Linux VM on Windows Azure with the following command:
$PublicConfiguration = '{"fileUris":["http://path/deployment/ubuntu-elasticsearch-installation.sh"], "commandToExecute": "sh ./ubuntu-elasticsearch-installation.sh -n Node1 -n Node2 -c cluster"}'
The script works when I execute it on any Linux VM in the same format. But when i do this only the -c parameter is passed to the script during provisioning.
On the other hand when I do it this way just for test (the script taked parameters and installs them):
$PublicConfiguration ='{"fileUris":["http://path/deployment/test.sh"], "commandToExecute": "sh test.sh apache2 unzip"}'
it installs both.
Then I do the following for both:
$ExtensionName = 'CustomScriptForLinux'
$Publisher = 'Microsoft.OSTCExtensions'
$Version = '1.2'
$vm = Set-AzureVMExtension -ExtensionName $ExtensionName -VM $vm -Publisher $Publisher -Version $Version -PublicConfiguration $PublicConfiguration
New-AzureVM -ServiceName $servicename -Location $location -VMs $vm
Does anyone have experience with Custom Scripts for Linux?
Thanks,
Vangel

Resources