Answered by:
simple if and else statement from the result of get statement

Question
-
Hi Team,
I get the result of the server sizes using powershell Commandlet using the below cmdlet
Get-AzureRmVMSize -ResourceGroupName TestRG -VMName $VMName | Select Name Name ---- Standard_NV6 Standard_NV12 Standard_NV24 Standard_A0 Standard_A1 Standard_A2 Standard_A3 Standard_A5
I want to match the above result with one value Ex "Standard_A3" if this value is present in the output of the powershell then do this, else do something else. Below code does not help and it errors out.
Get-AzureRmVMsize -ResourceGroupName $ResourceGroupName -Name $VMName | if ( $_.Name -match "Standard_D1_v2" ) { Code block } Else { Code block }
the above code does not work. Can you guys help me in this
- Edited by Qureshi Aquib Saturday, June 16, 2018 3:40 AM Title change
Saturday, June 16, 2018 3:40 AM
Answers
-
$sizes = Get-AzureRmVMSize ... if ($sizes.Name -contains 'Standard_A3') { # Do something } else { # Do something else }
- Edited by Leif-Arne Helland Saturday, June 16, 2018 10:29 AM
- Marked as answer by Qureshi Aquib Saturday, June 16, 2018 11:33 AM
Saturday, June 16, 2018 10:27 AM
All replies
-
You can't pipe directly to the if statment.
Get-AzureRmVMsize -ResourceGroupName $ResourceGroupName -VMName $VMName | ForEach-Object {if ($_.Name -match "Standard_D1_v2") {Code block} Else {Code block}}
Saturday, June 16, 2018 6:10 AM -
- Edited by jrv Saturday, June 16, 2018 7:52 AM
Saturday, June 16, 2018 7:52 AM -
Thanks for looking into it.
foreach loop will run for first value Standard_NV6 and if it does not match then it will run the else code
Once again it will loop through the second value and if does not match then it will run the else code
I want to run the if and else code only once by checking all the values.
Saturday, June 16, 2018 9:47 AM -
$sizes = Get-AzureRmVMSize ... if ($sizes.Name -contains 'Standard_A3') { # Do something } else { # Do something else }
- Edited by Leif-Arne Helland Saturday, June 16, 2018 10:29 AM
- Marked as answer by Qureshi Aquib Saturday, June 16, 2018 11:33 AM
Saturday, June 16, 2018 10:27 AM -
This is generic training videos, do you have anything related to the post?
To understand the problems with your question and to understand the answer you must do the basic PowerShell training videos or get one of the many books that will teach you PowerShell. The answers you seek are in both.
This forum and all technical forums are not places to ask for others to write a script for you. If you do not wish to learn the technology then you will need to hire a consultant to work with you.
Believe me when I say that you really need to do the "generic" training. The questions you are asking show you have absolutely know experience with scripting, programming or any basic computer training. Basic computer assembly and operation is not technical computer training.
The basic problem you present is that, without basic knowledge, you are not able to formulate a question that makes enough technical sense for us to guess at what you are trying to ask. Your question is incomplete and somewhat illogical.
Sorry but the training or a consultant will be the easiest way for you to go.
\_(ツ)_/
- Edited by jrv Saturday, June 16, 2018 10:29 AM
Saturday, June 16, 2018 10:28 AM -
You can only use '$_' inside a script block, which has the curly braces '{' and '}'.
Saturday, June 16, 2018 3:41 PM