Why do I need to combine with a variable first?

In an attempt to clear my coding style, I played with splatting (knowing how much backtick I don't like), so we get something like this

$splat = @{
    "ResourceGroupName" = $ResourceGroupName
    "Location" = $location
    "WhatIf" = $true 
    "ErrorVariable" = "err"
    "ErrorAction" = "SilentlyContinue"
}
New-ResourceGroup @splat 

What always seemed unnatural to me, I don't like the options in front of the cmdlet. It annoys me!

So I'm curious why this can't / doesn't work. (and can this be done)

New-ResourceGroup @{
    "ResourceGroupName" = $ResourceGroupName
    "Location" = $location
    "WhatIf" = $true 
    "ErrorVariable" = "err"
    "ErrorAction" = "SilentlyContinue"
}

Which seems a lot more natural to me. Is there any way to do this? or am I forever doomed to put a cart before a horse ...

edited to add

If there is no answer to this question, I posted a Uservoice proposal - feel free to go and vote for it,

+4
source share
2 answers

, AFAIK. , @splat - ( ). $, , splatting @ () . , splatting , .

PS C:\Users\frode> @splat
At line:1 char:1
+ @splat
+ ~~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@splat' can be used only a
s an argument to a command. To reference variables in an expression use '$splat'

- (, dir @(@{ Filter = "notepad.exe" })) - , - -.

, , requst PowerShell @UserVoice. hashtable upfront - .

+1

TesselatingHacker , . - /.

@{
    Filter = "*.txt"
    Path = "d:\temp"
    File = $true
} | ForEach-Object{Get-ChildItem @_}

. , ForEach-Object % .

0

Source: https://habr.com/ru/post/1627209/


All Articles