r/PowerShell • • Jun 16 '17

Why PowerShell is so complicated?

Ok, this is how do you attache a disk to VM on Azure (according to Azure documentation).

$diskConfig = New-AzureRmDiskConfig -Location EastUS -CreateOption Empty -DiskSizeGB 128 $dataDisk = New-AzureRmDisk -ResourceGroupName myResourceGroup -DiskName myDataDisk -Disk $diskConfig $vm = Get-AzureRmVM -ResourceGroupName myResourceGroup -Name myVM $vm = Add-AzureRmVMDataDisk -VM $vm -Name myDataDisk -CreateOption Attach -ManagedDiskId $dataDisk.Id -Lun 1 Update-AzureRmVM -ResourceGroupName myResourceGroup -VM $vm

Why is this should be five lines? Can't it be simple? For example:

New-AzureRMVMDisk -VM nameofthevm -diskname nameofthedisk -type typeofthedisk etc.

I mean why PowerShell complicates things? After all it is command line tool that should make our life little bit easier.

1 Upvotes

10 comments sorted by

18

u/GoodShitLollypop Jun 16 '17

That's not Powershell complicating anything. That's the Azure module for Powershell, and it compartmentalizes components of the command on purpose. Most people would find five 20-character lines easier to parse than one 100-character line.

2

u/hayfever76 Jun 18 '17

Well, that and PowerShell is object based so you have to create and manage all the objects that do things which is where the extra lines come from. If you take a look at the current Azure CLI, it's written in Python and takes like 5 lines to do what PowerShell needs 20 to do.

7

u/BitteringAgent Jun 16 '17

I haven't dealt with PowerShell and Azure VMs....maybe you should write yourself a module to make it easier for you in the future?

7

u/markekraus Community Blogger Jun 16 '17

Why is this should be five lines?

You think that is complicated.... you should watch all the underlying API calls to make that happen. You are already getting a very abstract view of a very concrete process.

It's "complicated" because it is flexible. It has more use-cases in mind than your particular use-case and supports more features than you likely care to use.

Can't it be simple? For example:

Absolutely! If you are doing this 100's or 1000's of times with several static options and a few variable ones, you can create your own wrapper functions to make an even more abstract view.

5

u/neztach Jun 16 '17

maybe it's not so much that powershell may or may not be complicated, but maybe your perspective on it isn't where it should be when you deal with it. /u/GoodShitLollypop is exactly right and it's compartmentalized. Think of it like this ...lets say you have a program that can do MANY things. How else do you convey what you intend to do without hitting it in stages.

So in your example,

"Get-AzureRmVm" is calling a specific action.
Adding the "-CreateOption" tells that module which branch of its capabilities you want to use.
"Attach" tells that branch which subbranch of "-CreateOption" you want to use, and so on, and so on.

Just think of it as a program capable of a lot and you have to hit it in pieces so you can both be clear about what you intend to do as well as troubleshoot the command if it doesn't give the results you want. You choose a module and/or action, then choose the appropriate branches and sub-branches of that action so you can be specific to the action you want to take. I can even do you one better for an example.

Try this one out, in this example I have several pieces of information on the Description line that need to be parsed (also keep in mind this is a single line command, but I'm breaking it up here so you can get a feel for what I mean about compartmentalizing the command)...

Get-ADUser 
  -Filter{Enabled -eq $true} 
  -Prop
    Enabled,
    Description,
    Title,
    Company,
    Department,
    Office,
    EmailAddress,
    OfficePhone,
    LastLogon,
    Manager | 
  Select 
    Name,
    @{n='License';e={$_.Description.Split("-")[-1].Trim()}},
    @{n='Property';e={$_.Description.Split(':')[0]}},
    Title,
    Department,
    Office,
    Company,
    EmailAddress,
    OfficePhone,
    @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}},
    @{n='Manager';e={(Get-ADUser $_.Manager).Name}} | 
  Where {
    ($_.Name -NotLike "SYSTEM*") -OR 
    ($_.Description -NotLike "SYSUSER*") -OR 
    ($_.Description -NotLike "UTIL*")} | 
  Where {
    ($_.Property -eq "Property1") -OR 
    ($_.Property -eq "Property2") -OR 
    ($_.Property -eq "Property3") -OR 
    ($_.Property -eq "Property4")} | 
  Sort 
    Department,
    Name | 
  Out-GridView

4

u/[deleted] Jun 16 '17

PowerShell is pretty awesome, but declarative ARM templates are a much better way to roll Azure resources once you get the hang of them, IMHO.

3

u/midnightFreddie Jun 16 '17

Generally speaking, GUIs are easier for one-off tasks. That's why they're so damn popular.

If you need to add a new blank disk to each of 150 existing VMs and for each new VM created, scripting blows away any GUI.

But even "one-off" situations aren't often one-off; maybe you need to add a disk a couple of times a month or week, and you can parameterize the script...well you basically make your own function that takes the arguments you wish for in OP, and boom you got your easy mx item.

Powershell cmdlets do focused, basic things each, and you string them together. Lots of scripting languages are like that. Your need for adding a disk is different from mine, and they can't anticipate all needs, so you have building blocks.

3

u/fourierswager Jun 16 '17

Well, a lot of folks would say that is simpler and more flexible than one function with 20 different parameters.

The philosophy behind this basically says that creating small functions that do one thing makes it easier to diagnose issues/bugs when they are all combined into a larger script.

Then you might say:

"Well, the one thing I want to do is create a new VM."

Okay, but is that literally the ONLY scenario in which defining a Disk Configuration would be useful? What if you want to add a new disk to an existing VM? This is why defining the Disk Configuration is its own cmdlet. Extend this philosophy to all scenarios in Azure, and you end up with 100s of cmdlets that do very specific things (which, again, is a good thing according to this philosophy, and I'd tend to agree).

If you want to make Azure VM deployment simpler/maintainable, then I agree with /u/belstarr - use templates and write a function that digests those templates. That being said, there are products out there that do this already, like Terraform. (There's a learning curve, but once you learn it, you can create deployments that are Cloud Provider agnostic).

2

u/bblades262 Jun 16 '17

This is the best i got for you: Azure is growing and changing every day, and the support tools for it are growing and changing too. As time goes on, suggestions like yours will be implemented. In the mean time, make your own function, and a module to keep it in. Then share it with the community!

2

u/jheinikel Jun 16 '17

Welcome to Powershell with Azure. You want to have more fun and find unnecessary usage of lines, start messing with the NIC configurations for your VM. (Change networks, IPs, etc) In reality though, I would rather have multiple lines doing individual pieces of the config and them all coming together later. You could make that a one-liner if you wanted to. Just wrap each of the commands into parenthesis and feed the final parameters that way. Definitely not what I would call pretty or reusable code though.