Now I'm no powershell expert , i would describe myself as a beginner but I'm able to re-use concepts from other scripting tools/languages to build what i want to do (i'm still not sure if powershell is a scripting or programming language sometimes - it seems a bit of a mash up of lots of things). Anyway one of the things i do find frustrating when building things is that it often needs extra modules, it needs some extra permissions to run some stuff that i cant grant without local admin and it can be a bit fiddly to log into azure. That aside though it's really growing on me - it's a very powerful tool.
Now to address some of the points above I've been trying to do some stuff in the powershell version in azure cloud shell - this removes a lot of constraints mentioned and means i can run the scripts from anywhere that has a web browser.
In this simple case i just wanted to be able to get a list of all my windows servers and find out if they were BYOL or PAYG - this should be a fairly simple thing to do - so here is how i went about that.
So first up how to access cloud shell in the first place? 2 ways to do this:
1) click the icon from the normal azure portal to activate it:
2) Directly go to shell.azure.com to open a full screen version of the same thing
Once there you have a powershell window with azure modules pre-loaded and the session already autheticated to your Azure account - so thats already made things a lot easier than having to use the 'full' version.
In fact this cloud version has some extra features in it that are not even possible in the normal powershell - further muddying the lines of what powershell actually is.......
As an example of that just type dir....
At the top level that gives you a list of all your subscription names/id's
You can then cd 'into' a subscription
Then you can do various things against resources - but thats not what i want to talk about.....
Anyway here is what i wanted to show - here is a simple (well fairly simple) powershell script that loops through all my subscriptions and collects some basic info including the windows licence type (BYOL or PAYG) (thanks To Ajith for the main central part of this script by the way)
$Array = @() $subscriptions = dir |select subscriptionid foreach ($sub in $subscriptions) { Select-AzureRmSubscription -SubscriptionId $sub.subscriptionid | Out-Null $SubName = (Get-AzureRmSubscription -SubscriptionId $sub.subscriptionid).name $RMVMs=Get-AzurermVM foreach ($vm in $RMVMs) { $VMStatus = Get-AzurermVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Status $VMOS = $vm.StorageProfile.OsDisk.OsType if ($VMOS -eq "Windows") { $PowerState = $License = $OS = $ComputerName = $Size = $Null $ComputerName = $vm.Name $PowerState = (get-culture).TextInfo.ToTitleCase(($vmStatus.statuses)[1].code.split("/")[1]); $License = (Get-AzurermVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName).licensetype $Size = $vm.HardwareProfile.VmSize $Array += New-Object PSObject -Property @{` Subscription = $SubName ComputerName = $ComputerName License = $License Size = $Size PowerState = $PowerState } } } } $Date = Get-Date -Format ddMMMyyHHmmss $Array | Select-Object Subscription,ComputerName,License,Size,PowerState |Export-Csv c:\users\containeradministrator\clouddrive\Windows_vm_License_$Date.csv -NoTypeInformation
So a quick description of whats going on here to explain
The first part create an empty array and then i build a powershell object containing all my subscription id's using the dir function i already mentioned
Then i loop through setting my context to each subscription in turn then within that loop go through all the VM's and extract the basic information i'm interested in - the key bit of info being the licensetype property
Once i have that data i put a 'row' into the array continue looping round til everything is done then dump the array out in csv format to my 'clouddrive'.
Now the cloouddrive is an important thing to mention here - this is the specific area that is available to access outside of cloudshell - so in this case the specific location is
c:\users\containeradministrator\clouddrive\
Any files written here can be found in the storage account you created when you first set up cloudshell - to illustrate that here is an example of mine
The easiest way to find it is to looks for storage accounts starting cs that are in a resource group called cloud-shell-storage-regionnamehere. Once in there just look for a share which contains your login username and you'll find it.
In the example above that directory is the same exact location as i see as the directory on the c:\drive mentioned above in cloud shell and i can happily copy files to/from this location.
So to run my report i can just paste the whole command in let it run then pick up the csv file from the storage account afterwards.
(just as an FYI to move between the special azure location and the c drive just run either cd c: or cd azure:)
So now i have my csv which looks a little like this:
I've hidden the first couple of columns but you can see in the (now the first column) the licence type - if it displays Windows_Server its BYOL - if it's blank then its PAYG. I'm sure this is dead easy to do in powershell to just do string substitution but I didnt have the energy to figure that out by the time I'd go this far - and quick fix to change that would be appreciated :-)
So there you have it a useful use case for cloudshell - get a list of all of your windows licence usage in Azure from the browser directly......
Comments
Post a Comment