Get location value of Resource Group in a variable Powershell - powershell

I have a powershell script to deploy a service bus namespace
This is the command i use to deploy it .
New-AzureRmServiceBusNamespace -ResourceGroup $ResourceGroupName -NamespaceName $ServiceBusNamespace -Location $Location
The above command expects a location parameter to be entered. I want that location value as the Location where my resource group is located . How do i extract the Location value of my resource Group ?
I tried various methods and failed. it shows #{location=west us} , when i tried this
$location = Set-AzureRmResourceGroup -Name $ResourceGroupName -Tag #{} | Select-Object Location
Write-Host $location
Need help . Thanks !

$loc = Get-AzureRMResourceGroup -Name $ResourceGroupName | select-object -expandproperty location
Well, if you want to GET something use appropriate verb.
or like this:
$rg = Get-AzureRMResourceGroup -Name $ResourceGroupName
$rg.location

The below code works with Az powershell module.
$loc= Get-AzResourceGroup -Name $resourceGroupName #Gets the resource group details and stores in $loc variable
$location= $loc.location #Gets the location name from resource group properties and stores in $location variable
Write-Host $location #outputs the resource group location

Related

need to invite multiple guest users, and need to create multiple resource group

We require an PowerShell script to find which Resource Group is not having IAM Role Assigned.
Please find below the PowerShell scripts for your reference, and let me know the PowerShell script for to finding which Resource Group is not having IAM Role Assigned.
Inviting Multiple Uers.
Install-Module AzureADPreview Connect-AzureAD -TenantId
$invitations = import-csv
C:\Users\dummy\Desktop\users.csv $messageInfo = New-Object
Microsoft.Open.MSGraph.Model.InvitedUserMessageInfo
$messageInfo.customizedMessageBody = "Hello. You are invited to the
xyz.onmicrosoft.com." foreach ($email in $invitations)
{New-AzureADMSInvitation -InvitedUserEmailAddress $email.InvitedUserEmailAddress
-InvitedUserDisplayName $email.Name -InviteRedirectUrl https://myapps.microsoft.com
-InvitedUserMessageInfo $messageInfo `
-SendInvitationMessage $true }
Creating multiple resource groups.
Connect-AzureRmAccount -Tenant
-Subscription import-csv C:\Users\dummy\Desktop\excel02.csv | ForEach-Object
{New-AzureRmResourceGroup -Name $_.Name -Location centralIndia
Assigning iam role in bulk
Connect-AzureRmAccount -Tenant
-Subscription import-csv C:\Users\dummy\Desktop\excel05.csv | ForEach-Object
{New-AzureRMRoleAssignment -SignInName $.Users -RoleDefinitionName
"Owner" -ResourceGroupName $.RG}
Getting Role Assignment in bulk.
Connect-AzureRmAccount -Tenant
-Subscription
#Get-AzureRmRoleAssignment -ResourceGroupName MridulAggarwal -SignInName xyz#xyz.onmicrosoft.com import-csv C:\Users\nawabjanm\Desktop\excel05.csv | ForEach-Object
{Get-AzureRmRoleAssignment -ResourceGroupName $.RG -SignInName
$.Users} | FL > D:\r20.csv
Please find the below script to determine the resource groups with role assignments and their details in a particular subscription. So, those resource groups which are not listed in below are surely the ones which do not have any IAM role assignments.
Get-AzResourceGroup | ForEach-Object {Get-AzRoleAssignment -ResourceGroupName
$_.ResourceGroupName} | export-csv 'C:\res.csv'
Thus, you can be rest assured of those resource groups with the IAM role assignments.
Please find below links for more information: -
https://learn.microsoft.com/en-us/powershell/module/az.resources/get-azresourcegroup?view=azps-6.3.0
https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-powershell

Question about azure powershell, defining a veriable twice, and disk creation

I'm currently studying for az-104 azure exam and going through some scripting exercises.
I can confirm the script below works, but I don't understand how...
in the last few lines of the script the variable $vm is defined twice. how is this possible?
also when you define a variable is it actually running the commands that are being defined? i didn't realize that was the case, but it definitely seems to be.. can someone please explain?
$resourcegroup = "2019dc_group"
$machinename = "2019dc"
$location = "east us"
$storagetype = "Standard_LRS"
$name = "newdisk"
$size = 20
$datadiskconfig = new-azdiskconfig -SkuName $storagetype -location $location -createoption empty -DiskSizeGB $size
$datadisk01 = new-azdisk -diskname $name -disk $datadiskconfig -ResourceGroupName $resourcegroup
$vm = Get-AzVM -name $machinename -resourcegroupname $resourcegroup
$vm = add-azvmdatadisk -vm $vm -name $name -createoption attach -ManagedDiskId $datadisk01.id -lun 1
update-azvm -vm $vm -resourcegroupname $resourcegroup
Variable types in PowerShell are dynamic. They are automatically "adjusted" to the type of the object they are assigned to.
Yes, the commands are being executed first and then the object is placed in the variable. This is also why you are able to use the vm variable in the command and assign the result back to that variable. When the "add-azvmdatadisk" command is executed, the type of the variable is still an Azure VM. When the assignment takes places, it is an Azure VM Data Disk
You can use Get-Member to get the type, methods, properties, events etc. for any object.
$a = 1 #System.Int32
$a | Get-Member
$a = "1" #System.String
$a | Get-Member
$a = Get-Service | Select-Object -First 1 #System.ServiceProcess.ServiceController
$a | Get-Member
The $VM variable is not defined twice. It's being assigned a value twice. One doesn't need to define variables in Powershell, assignment will automatically create one.
Let's review the code. First off,
$vm = Get-AzVM -name $machinename -resourcegroupname $resourcegroup
After the Get-AzVM, you'll have its output in $vm. Nothing fancy here. Note that before this statement, $vm didn't exist, and trying to work with it would generate an error about using null value.
$vm = add-azvmdatadisk -vm $vm -name $name -createoption attach -ManagedDiskId $datadisk01.id -lun 1
Here $vm is used twice. The first usage is when you pass it as an argument to Add-AzVmDataDisk. The cmdlet returns a PSVirtualMachine object, so after adding the disk, you'd get an updated version of $vm into the variable. This might or might not be important a difference. If the Azure VM objects are lazy evaluated, the old $vm would contain hardware information without the new disk. This often is the case, as it improves performance. By assigning the output of the later cmdlet into $vm, you are sure to have up-to-date version of your VM.
If you have a lab environment, try printing $vm before the last disk addition cmdlet call and after it. See what's different on the outputs.

Azure Powershell VM Stopped status in variable

I am trying to write a PowerShell script that will look for shutdown VMs in my Resource Group and deallocate them. The output of the below script does not give me the VM name "clean" when I attempt tp assign the below as a variable. The end result is to execute the Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force
So for more context, lets say AVGJOE-DC1 is in a stopped state and I run the below line in Azure Powershell it will display
Name
----
AVGJOE-DC1
If I then if I tried to use $VM to call AVGJOE-DC1 in the
Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force
it fails due to the variable being set to a longer string something like
MicroSoftComputerResource\Resourcegroup[#Name=AVGJOE-DC1].
Hopefully that makes sense.
$VM = Get-AzureRmVM -ResourceGroupName LAB | get-azurermvm -Status | ?{$_.statuses.displaystatus -eq "VM stopped"} | select name
Just like #Theo said in the comment, select name gives you an object with property name. If you want the string value of the name property, you can use Select-Object -ExpandProperty name instead of select name.

Import Azure Automation RunBook using Azure Automation

I am trying to create an Azure Automation job to create a new Azure Automation Runbook. I am using the following to try to get it to work.
$Context = New-AzureStorageContext $storageAccountName $storageAccountKey
$Path = Get-AzureStorageFile -ShareName "qdrive" -Path "TestWorkFlow.ps1" -Context $Context |Select-object Name |Out-String
Import-AzureRMAutomationRunbook -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Path $Path -Type PowerShellWorkflow -Force -Name $Name -Published
I get an error message of
Import-AzureRMAutomationRunbook:Cannot find path 'C:\Windows\System32\
Name
------
TestWorkFlow.ps1
I need help figuring out how to send the path of the file to the $path variable in a UNC and not a URI.
Thanks!
The cmdlet needs to take a fully qualified path to the runbook .ps1 file, where the local machine has access to that path via normal local file system referencing. It looks like in this case $Path contains “Name ------ TestWorkFlow.ps1” – so therefore you are not storing the path in $Path correctly, hence the failure.
The $path variable for the -Path switch to the cmdlet needs to contain the full path, Including the filename itself. Like, "C:\Users\Johndoe\TestWorkFlow.ps1". Hope this helps.

current azure storage doesn't allow create new VM

I have the script, which create vitrual machine, and install google chrome via dsc. I created current storage account, and executed script. But in next time I have error. This is my script:
$SubscriptionName = "subscription_name"
Select-AzureSubscription -SubscriptionName $SubscriptionName
#Replace the variable values as needed
$VMName = "CSETest"
$StorageAccount = 'googleChrome'
$StorageKey = 'key'
$StorageContainer = 'dscarchives'
$ServiceName="ChromeInstaller"
#Get the OS image reference
$arrayWindows=( Get-AzureVMImage | where-object { $_.ImageName -like "*Windows*Server*2012*R2*en.us*" } )
$locationAllow=$arrayWindows[$arrayWindows.Count-1].Location
$locationAllow=$locationAllow.Split(";")
$locationAllow=$locationAllow | where-object { $_ -like "* US*"}
$Localization=(Get-Random -InputObject $locationAllow)
#Create VM Config with last windows update
$vmConfig = New-AzureVMConfig -Name $VMName -ImageName $arrayWindows[$arrayWindows.Count-1].ImageName -InstanceSize Small
#Create Provisioning Configuration
$vmProvisioningConfig = Add-AzureProvisioningConfig -VM $vmConfig -Windows -AdminUsername "login" -Password "password"
$StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccount -StorageAccountKey $StorageKey
Publish-AzureVMDscConfiguration -ConfigurationPath .\ChromeInstaller.ps1 -ContainerName $StorageContainer -StorageContext $StorageContext -Force
#Set the Azure VM DSC Extension to run the LCM meta-configuration
$vmAzureExtension = Set-AzureVMDscExtension -VM $vmProvisioningConfig -ConfigurationArchive ChromeInstaller.ps1.zip -ConfigurationName OpenChrome -Verbose -StorageContext $StorageContext -ContainerName $StorageContainer -Force
#Create a VM
New-AzureVM -ServiceName $ServiceName -VMs $vmAzureExtension -Location $Localization -WaitForBoot
In the last line, script throw exception:
New-AzureVM : BadRequest: The location or affinity group Central US of the storage account where the source image
a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201410.01-en.us-127GB.vhd resides is not in the same location or affinity group as
the specified cloud service. The source image must reside in a storage account that has the same affinity group or location as the cloud
service West US.
So, as you can see, my script generate random location. And I can't link with on location. So, how can I fix this bug?
The source image VHD must exist in the same location as the new virtual machine. Use AzCopy to copy the image to storage accounts in all locations listed in $locationAllow.
The Simple answer is to create your VM in the same location as your storage account. This is telling you that your Azure OS Image in the storage account location does not exist in your cloud service location.
You can also try this
$AzureLocation = (get-azurelocation)
$Localization=(Get-Random -InputObject $AzureLocation)
$WindowsImage=( Get-AzureVMImage | where-object { $_.ImageName -like "*Windows*Server*2012*R2*en.us*" -and $_.Location -Match $Localization.DisplayName } | sort PublishedDate | select -first 1 ).ImageName
#Create VM Config with last windows update
$vmConfig = New-AzureVMConfig -Name $VMName -ImageName $WindowsImage -InstanceSize Small
With this method, we are selecting a location, then an image that resides within that location. Previously you were selecting an image (which may reside in Location West US) then selecting a location .
Alternativly, get the location of your storage account and use that location to get the image and create the cloud service in the same location of the storage account