AZURE-POWERSHELL
Ir para navegação
Ir para pesquisar
Tool
WEB API installer
Configure Azure on PowerShell on Linux
Install-Package -Name AzureRM.NetCore.Preview -Source https://www.powershellgallery.com/api/v2 -ProviderName NuGet -ExcludeVersion -Destination /home/user/path
Import-Module AzureRM.Netcore.psd1
Install-Module AzureRM
Login-AzureRmAccount
RBAC
$role = Get-AzureRmRoleDefinition "Virtual Machine Contributor"
$role.Id = $null
$role.Name = "Virtual Machine without Public IP TESTE "
$role.Description = "Não consegue atribuir IP Publico."
$role.Actions.Remove("Microsoft.Network/publicIPAddresses/join/action")
$role.AssignableScopes.Clear()
$role.AssignableScopes.Add("/subscriptions/<id>")
New-AzureRmRoleDefinition -Role $role
Basic Commands
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
Install-Module AzureRM
Import-Module AzureRM.Netcore.psd1
Add-AzureRmAccount
Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionName "subscription name"
New-AzureRmResourceGroup -Name Damato -Location "east US 2"
New-AzureRmStorageAccount -ResourceGroupName Damato -Name damatostorage -Type Standard_LRS -Location "East US 2"
New-AzureRmVirtualNetwork -name VNET-Damato -ResourceGroupName Damato -Location "East US 2" -AddressPrefix 192.168.5.0/24
$virtualNetwork = Get-AzureRmVirtualNetwork -name VNET-Damato -ResourceGroupName Damato
Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd -VirtualNetwork $virtualNetwork -AddressPrefix 192.168.5.0/26
Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd -VirtualNetwork $virtualNetwork -AddressPrefix 192.168.5.64/26
Add-AzureRmVirtualNetworkSubnetConfig -Name DMZ -VirtualNetwork $virtualNetwork -AddressPrefix 192.168.5.128/28
Set-AzureRmVirtualNetwork -VirtualNetwork $virtualNetwork
$availset="damato-availset"
$rgname="Damato"
$region="East US 2"
$storageaccount="damatostorage"
$nsg="damato-vm-01"
$auth=Get-Credential -Message "Type username and password"
$vnetname="VNET-Damato"
$subnetindex=0
$vnet=Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $rgname
$availset="damato-availset"
$vmname="damato-vm"
$vmSize="Standard_A3"
$diskNewSize=128
$nicName="damato-nic-01"
New-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name $availset-managed -Location $region -PlatformFaultDomainCount 3 -PlatformUpdateDomainCount 5 -Managed
New-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name $availset-classic -Location $region
$avSet = Get-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name $availset-classic
Update-AzureRmAvailabilitySet -AvailabilitySet $avSet -Managed
$vnet=Get-AzureRmVirtualNetworkSubnetConfig -Name Backend -VirtualNetwork $vnet
$nic=New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgname -Location $region -SubnetId $vnet.Id
$rules = New-AzureRmNetworkSecurityRuleConfig -Name 'default-allow-rdp' -Direction Inbound -Priority 1000 -Access Allow -SourceAddressPrefix '*' -SourcePortRange '*' -DestinationAddressPrefix '*' -DestinationPortRange 3389 -Protocol Tcp
$nsg = New-AzureRmNetworkSecurityGroup -Name $nsg -ResourceGroupName $rgname -Location $region -SecurityRules $rules
$vm=New-AzureRmVMConfig -VMName $vmname -VMSize $vmSize
$pubName="MicrosoftWindowsServer"
$offerName="WindowsServer"
$skuName="2016-Datacenter"
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmname -Credential $auth -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$diskName="damato-root"
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $storageaccount
$osDiskUri=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmname + $diskName + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureRmVM -ResourceGroupName $rgname -Location $region -VM $vm
(Get-AzureRmVM -Name $vmname -ResourceGroupName $rgname).StorageProfile.OsDisk
stop-azurermvm -ResourceGroupName $rgname -Name $vmname –Force
$VM = Get-AzureRmVM -ResourceGroupName $rgname -Name $vmname
$VM.StorageProfile[0].OsDisk[0].DiskSizeGB = $diskNewSize
Update-AzureRmVM -ResourceGroupName $rgname -VM $VM
start-azurermvm -ResourceGroupName $rgname -Name $vmname
Delete Orphan Disks
Get-AzureRmDisk | where { $_.OwnerId -eq $null } | Remove-AzureRmDisk -whatif
Script
Sample 1
$rg1Name="Damato"
$locName="East US 2"
$saName="damatostorage"
$nsgName ="damato-vm-01"
# Pedir credenciais VM
$cred=Get-Credential -Message "Digite o nome e a senha da conta de administrador local."
# Definir o índice de rede e sub-rede virtual existente.
$vnetName="VNET-Damato"
$subnetIndex=0
$vnet=Get-AzureRMVirtualNetwork -Name $vnetName -ResourceGroupName $rg1Name
# Criar o NIC com IP público.
$nicName="VM-01"
$pip=New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $rg1Name -Location $locName -AllocationMethod Dynamic
$nic=New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rg1Name -Location $locName -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $pip.Id
# Criar o NIC sem IP público.
$nicName="VM-01"
$vnet=Get-AzureRmVirtualNetworkSubnetConfig -Name Backend -VirtualNetwork $vnet
$nic=New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rg1Name -Location $locName -SubnetId $vnet.Id
$pip.Id
#Adicionando NSG(Grupo de Segurança)
$rules = New-AzureRmNetworkSecurityRuleConfig -Name 'default-allow-rdp' -Direction Inbound -Priority 1000 -Access Allow -SourceAddressPrefix '*' -SourcePortRange '*' -DestinationAddressPrefix '*' -DestinationPortRange 3389 -Protocol Tcp
$nsg = New-AzureRmNetworkSecurityGroup -Name $nsgName -ResourceGroupName $rg1Name -Location $locName -SecurityRules $rules
# Especificar o Nome e tamanho.
$vmName="VM-01"
$vmSize="Standard_A3"
$vm=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
# Especificar a imagem e a conta de administrador local e em seguida, adicione o NIC.
$pubName="MicrosoftWindowsServer"
$offerName="WindowsServer"
$skuName="2016-Datacenter"
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
# Especifique o nome de disco do sistema operacional e criar a VM
$diskName="OSDisk"
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $rg1Name -Name $saName
$osDiskUri=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureRmVM -ResourceGroupName $rg1Name -Location $locName -VM $vm
Sample 2
Grupo de Recurso
New-AzureRmResourceGroup -Name Demo_Rede -Location "East US 2"
New-AzureRmResourceGroup -Name Demo_Storage -Location "East US 2"
New-AzureRmResourceGroup -Name Demo_VMs -Location "East US 2"
Storage
New-AzureRmStorageAccount -ResourceGroupName Demo_Storage -Name strdemojatoba -Type Standard_LRS -Location "East US 2"
Virtual Network
New-AzureRmVirtualNetwork -Name V-NET -ResourceGroupName Demo_Rede -Location "East US 2" -AddressPrefix 192.168.50.0/24
$VirtualNetwork = Get-AzureRmVirtualNetwork -Name V-NET -ResourceGroupName Demo_Rede
Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd -VirtualNetwork $VirtualNetwork -AddressPrefix 192.168.50.0/26
Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd -VirtualNetwork $VirtualNetwork -AddressPrefix 192.168.50.64/26
Add-AzureRmVirtualNetworkSubnetConfig -Name DMZ -VirtualNetwork $VirtualNetwork -AddressPrefix 192.168.50.128/28
Add-AzureRmVirtualNetworkSubnetConfig -Name GatewaySubnet -VirtualNetwork $VirtualNetwork -AddressPrefix 192.168.50.144/28
Set-AzureRmVirtualNetwork -VirtualNetwork $VirtualNetwork
Máquina Virtual e seus serviços
Variaveis
$rgName="Demo_VMs"
$rgName1="Demo_Rede"
$rgName2="Demo_Storage"
$locName="East US 2"
$saName="strdemojatoba"
$nsgName ="NSG-SRV-DEMO"
$nsgName1 ="NSG-BackEnd"
$nsgName2 ="NSG-FrontEnd"
$nsgName3 ="NSG-DMZ"
$VMLocalAdminUser = "administrador"
$VMLocalAdminSecurePassword = ConvertTo-SecureString "Micheljatoba@2018" -AsPlainText -Force
$cred=New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword);
$vnetName="V-NET"
$subnetIndex=0
$vnet=Get-AzureRMVirtualNetwork -Name $vnetName -ResourceGroupName $rgName1
$nicName="SRV-DEMO"
$pip=New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $rgName -Location $locName -AllocationMethod Dynamic
$nic=New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $pip.Id
$rules = New-AzureRmNetworkSecurityRuleConfig -Name 'default-allow-rdp' -Direction Inbound -Priority 1000 -Access Allow -SourceAddressPrefix '*' -SourcePortRange '*' -DestinationAddressPrefix '*' -DestinationPortRange 3389 -Protocol Tcp
$nsg = New-AzureRmNetworkSecurityGroup -Name $nsgName -ResourceGroupName $rgName -Location $locName -SecurityRules $rules
$nsg1 = New-AzureRmNetworkSecurityGroup -Name $nsgName1 -ResourceGroupName $rgName1 -Location $locName
$nsg2 = New-AzureRmNetworkSecurityGroup -Name $nsgName2 -ResourceGroupName $rgName1 -Location $locName
$nsg3 = New-AzureRmNetworkSecurityGroup -Name $nsgName3 -ResourceGroupName $rgName1 -Location $locName
$vmName="SRV-DEMO"
$vmSize="Standard_A3"
$vm=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$pubName="MicrosoftWindowsServer"
$offerName="WindowsServer"
$skuName="2016-Datacenter"
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$diskName="OSDisk"
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $rgName2 -Name $saName
$osDiskUri=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
Criar máquina virtual
New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $vm
Sample 3
Criando Availability Set
New-AzureRmAvailabilitySet -ResourceGroupName "aula02" -Name "Gerenciado" -Location "East US 2" -PlatformFaultDomainCount 3 -PlatformUpdateDomainCount 5 -Managed
New-AzureRmAvailabilitySet -ResourceGroupName "aula02" -Name "Classic" -Location "East US 2"
$rgName = "Aula-02"
$avSetName = "Gerenciadosss"
$avSet = Get-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $avSetName
Update-AzureRmAvailabilitySet -AvailabilitySet $avSet -Managed
Size VHD S.O
$RG = “DC” (Nome do Grupo de recurso)
$VMNAME = “DC01” (Nome da máquina virtual)
$NEWSIZE = “1023” (Tamanho do VHD)
stop-azurermvm -ResourceGroupName $RG -Name $VMNAME –Force
$VM = get-azurermvm -ResourceGroupName $RG -Name $VMNAME # Você tinha Razão.
$VM.StorageProfile[0].OsDisk[0].DiskSizeGB = $NEWSIZE
Update-AzureRmVM -ResourceGroupName $RG -VM $VM
start-azurermvm -ResourceGroupName $RG -Name $VMNAME
Convertendo Discos Baseados em Blob para Discos Gerenciados
$rgName = "Aula02"
$vmName = "Aula-02"
Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName –Force –Verbose
ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $rgName -VMName $vmName
Verificar Statuts VM no Resource Group
$RGs = Get-AzureRMResourceGroup
foreach($RG in $RGs)
{
$VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName
foreach($VM in $VMs)
{
$VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status
foreach ($VMStatus in $VMDetail.Statuses)
{
if($VMStatus.Code.CompareTo("PowerState/deallocated") -eq 0)
{
$VMStatusDetail = $VMStatus.DisplayStatus
}
}
write-output $VM.Name $VMStatusDetail
}
}
Criando Snapshot Microsoft Azure
$rgName = "Aula02"
$location = "East US 2"
$snapshotName = "WS2016-Snapshot"
$imageName = "WS2016-ImagemDefault"
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName(Validar)
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -SnapshotId $snapshot.Id
New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig –Verbose
Enable more than 12 instances on application gateway
$agw = Get-AzureRmApplicationGateway -Name appgwname -ResourceGroupName resourgrouname
$agw.Sku.Capacity = 12
Set-AzureRmApplicationGateway -ApplicationGateway $agw