Call Set-AzureRmAppServicePlan cmdlet with arbitrary parameters - azure

I want to call Set-AzureRmAppServicePlan with arbitrary parameters, the list of parameters is defined in runtime and is not set statically.
In other languages like Perl I would use Hash for this, but I get stuck here in Powershell even though I know that Powershell supports HashTables.
Following example does not work
$params = #{}
$params.add('-WorkerSize', 'Small');
$params.add('-NumberofWorkers', '2');
Set-AzureRmAppServicePlan -ResourceGroupName 'RG1' -Name 'AppServicePlna1' $params
I get Set-AzureRmAppServicePlan : Long running operation failed with status 'BadRequest'. error from Azure.

PowerShell does support what you are trying to do. It is known as splatting.
In your case, you have a minor typo. All you need to do is change the $ to an # in this line:
Set-AzureRmAppServicePlan -ResourceGroupName 'RG1' -Name 'AppServicePlna1' $params
So the working version looks like this:
Set-AzureRmAppServicePlan -ResourceGroupName 'RG1' -Name 'AppServicePlna1' #params

The issue is not with PowerShell per say.
Last time I checked you cannot pass 'Parameter key' for an Azure Module command.

Related

Azure PowerShell :: how to print a list of properties

I found on this post how to print the the Azure Subscription ID:
(Get-AzContext).Subscription.id
But if I look in the official documentation of the command Get-AzContext I don't see anywhere that the .Subscription.id or .id would print that information.
How the guy who replied to that question knew such information?
Where can I find a list of properties for each command?
Commmands like Get-AzContext | fl * or Get-AzContext | gm or get-help Get-AzContext -full don't provide such list.
I want to be able to see all properties provided by commands like Get-AzResource or Get-AzSqlDatabase or any other.
Problably not the cleanest way, but as I use this trick very often and since I shared to some teammates I noticed they are using it now I guess it worths sharing :) .
Use the convertto-json -depth xx (where xx is big enough for your need and depending on the objet's complexity) to get the whole view of an object
Then you can redirect to a file and look for what you need quite easily.
In case you run Get-AzContext | convertto-json -depth 10 you will find back the subscription and the ID.

Trying to Extract Previous Day Azure VM Report but unable to Do via Powershell

I am trying to generate backup report for previous day via Powershell but its not working .Can anyone help me on that .
Below is my Powershell Script
$ErrorActionPreference = "SilentlyContinue"
$report_object =$null
$report_object = #()
$vms = get-azvm | select Name
$acs = Get-AzRecoveryServicesVault
foreach ($ac in $acs){
Set-AzRecoveryServicesVaultContext -Vault $ac
$container_list = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM
foreach($container_list_iterator in $container_list){
$backup_item = Get-AzRecoveryServicesBackupItem -Container $container_list_iterator -WorkloadType AzureVM
Your code is missing two curly braces (}}) at the end of the code. Is it just a typo? Or is that the reason for you to be unable to extract the report?
I have tried to reproduce the issue to see if I get any exceptions while execution but it all went well as shown in below screenshot.
Can you elaborate on what you meant by "unable to extract the report". Did you receive any error while executing the code? Please provide more details in that context.

String Length different when using ISE or Powershell.exe

I have written a small user Interface with PowerShell in order to create checksums comfortable. At the moment I see a difference between using the ISE and the powershell itself.
When running the PowerShell script within the ISE I get perfect results.
This is the code I am using (Just a snippet):
$aaa = (Get-FileHash -Algorithm SHA512 -Path $str_filepath |
Select-Object -Property hash |
Format-Table -HideTableHeaders |
Out-string).TrimEnd().TrimStart()
So I create a checksum (SHA512) from a file. These are the results for one file as example:
Running the script in ISE:
0518E6DF62AB7B8D7A238039262C7A0E9F1F457D514EDE2BB8B3F4719340EF4B61053EC85ED30D07688B447DBC756F3A7455D7E0C84C7BCF62A8884E4715C8A0
Running the script in PowerShell:
0518E6DF62AB7B8D7A238039262C7A0E9F1F457D514EDE2BB8B3F4719340EF4B61053EC85ED30D07688B447DBC756F3A7455D7E0C84C7BCF62A8...
As you can see the string is shortend when using PowerShell. More confusing is that the shortening is not consistent. At home on my Windows 7 machine the string is even shorter then on my Windows 8.1 System at work. I know that there are some differences between ISE and PowerShell when running scripts regarding to styles. But shorter strings... Hmm.
So now the question. Does anyone of you have expierenced that difference between ISE and Powershell regarding to String length limitations? And if so. Does have anyone an answer for me how I can script it that there will be no different string results?
Do not use Format-* cmdlets if you need to process your data further. Those cmdlets are only for displaying data to a user. Instead expand the Hash property of the object Get-FileHash returns, either like this:
$str_checksum_sha512 = (Get-FileHash -Algorithm SHA512 -Path $str_filepath).Hash
or like this:
$str_checksum_sha512 = Get-FileHash -Algorithm SHA512 -Path $str_filepath |
Select-Object -Expand Hash

I am trying to read the output values from ARM template in post deployment script file. Can you give me the syntax to read those values?

I am trying to read the output values from ARM template in post deployment script file. Can you give me the syntax to read those values?
You could retrieve the out value from a linked template by using reference function.
retrieve the property value with syntax like: "[reference('<name-of-deployment>').outputs.<property-name>.value]"
Note: You cannot use the reference function in the outputs section of a nested template. To return the values for a deployed resource in a nested template, convert your nested template to a linked template.
If Powershell command is possible, we could get the following command
$deploy = New-AzureRmResourceGroupDeployment -Name $deployment -ResourceGroupName $resourceGroupName -TemplateFile $deployJsonFilePath -TemplateParameterFile $deployJsonParameterFilePath
$outPuts = $deploy.Outputs

Powershell concatenate an Environment variable with path

So I have this code:
$userprofile=Get-ChildItem Env:USERPROFILE
$localpath="$userprofile\some\path"
I would expect output of the below from $localpath:
c:\users\username\some\path
However what I get is:
System.Collections.DictionaryEntry\some\path
So, of course, something like cd $localpath fails. How would I accomplish what I need?
A convenient way to obtain the string value rather than the dictionary entry (which is technically what Get-ChildItem is accessing) is to just use the variable syntax: $Env:USERPROFILE rather than Get-ChildItem Env:USERPROFILE.
$localpath = "$env:USERPROFILE\some\path"
For more information:
PowerShell Environment Provider
about_Environment_Variables
Also, the Join-Path cmdlet is a good way to combine two parts of a path.
$localpath = Join-Path $env:USERPROFILE 'some\path'

Resources