question

bandolton avatar image
0 Votes"
bandolton asked bandolton commented

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

windows-server-powershellazure-disk-storage
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

shivapatpi-MSFT avatar image
1 Vote"
shivapatpi-MSFT answered bandolton commented

Hello @bandolton ,
Thanks for reaching out to Microsoft Q&A.
Let me explain you those 2 lines where $VM was defined twice:

$vm = Get-AzVM -name $machinename -resourcegroupname $resourcegroup

Above command is fetching the details of the existing Virtual Machine , once that command runs $vm variable will have all the details about the VM
More precisely , if you do $vm.StorageProfile.DataDisks , after that command - if there are no data disks - it will show empty.

Now coming to next command:
$vm = add-azvmdatadisk -vm $vm -name $name -createoption attach -ManagedDiskId $datadisk01.id -lun 1

It is trying to add the data disk to the VM whose reference details are there in $VM after adding the data disk the same variable is being overridden.
In this case, after running the above command if you see the output of $vm.StorageProfile.DataDisks - you will see the output as something like below:

Name : disk1
DiskSizeGB :
Lun : 1
Caching : None
CreateOption : attach
SourceImage :
VirtualHardDisk :


Basically $VM is considered as an object pointing to some class which has multiple attributes .
When the second command gets executed : $vm = add-azvmdatadisk -vm $vm -name $name -createoption attach -ManagedDiskId $datadisk01.id -lun 1
Only a part of the attribute i.e. disk related is being modified and others are unchanged.
So what it means is , when we use the same variable $vm - only additional required attributes are being updated others are not changed. (like all other VM properties).
Now when we do update-azvm - It will update the VM with the latest changed parameters

Obviously we can also use some other variable with name $vm1 , it should be okay. By reusing the same variable sometimes we will be saving memory (Ofcourse in this case as it is a simple script it does not make any difference)





Hope that explanation helps you out in understanding better. Our best wishes are with you for your AZ104 exam.


Kindly let us know if you have additional questions !

Also make sure to "upvote and Accept the answer" if that helps out in answering your doubts

Regards,
Shiva.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thank you so much for taking the time out to explain that to me

couldn't have asked for a more clear detailed explanation

0 Votes 0 ·