Solved

PowerShell syntax

Posted on 2016-08-03
6
35 Views
Last Modified: 2016-08-05
So I recently read a post that used some syntax I'm unfamiliar with (I've bolded the relevant portion).
"C:\temp\logs\${env:computername}_$(get-date -f dd-MM-yyyy)_CHKDSKResults.txt"

I'm certainly familiar with the use of subexpressions, $(), but the portion in question uses braces instead of parentheses.  I've never seen that syntax before and have not seen any mention of it, yet it works.  If someone has asked me to come up with the equivalent, I would have used the following:
"C:\temp\logs\$($env:computername)_$(get-date -f dd-MM-yyyy)_CHKDSKResults.txt"

So here's the actual question.  Can anyone point me toward documentation or a resource that describes this syntax, and possibly what can be done with it?
And in the sample given, I'm not even sure how ${env:computername} works.  It's like running (gi env:\computername).value

My poor soul craves enlightenment!
0
Comment
Question by:footech
  • 3
  • 2
6 Comments
 
LVL 30

Assisted Solution

by:renazonse
renazonse earned 200 total points
ID: 41741590
It's syntax that allows you to use special characters in variables. It's also another way to call functions with ${function:functionname} via the invoke commands. It's not necessary in the example but it works.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 300 total points
ID: 41742164
That's from an answer of mine, so I guess I'll have to add something here ...
I'm trying to keep a consistent syntax in my scripts, so for environment variables embedded in strings, I'm always using the ${} syntax (whether needed or not for the specific variable) since trying to access $ENV:ProgramFiles(x86), where a subexpression won't get you very far.
It's actually pretty easy to come across that syntax. In a 64bit PS console, enter
$env:ProgramFile

Open in new window

and hit <tab>. This completes, as expected, to $env:ProgramFiles.
Now hit <tab> again, and you'll end up with ${env:ProgramFiles(x86)}

It seems to be called Variable Namespace Notation
Working with PowerShell variables
http://www.dotnetspark.com/kb/3790-working-with-powershell-variables.aspx
0
 
LVL 39

Author Comment

by:footech
ID: 41742762
@renazonse - Your comment wasn't quite clear, but it did lead me to playing around with variable names, and then re-reading the about_Variables topic (built-in PS help) which actually mentions it.  Do you have any more information on its use to call functions "via the invoke commands"?  I'm not sure what you mean by "invoke commands", unless you just mean the cmdlet Invoke-Command, but I'm not seeing the connection.  Example(s) would be really helpful, please!

@oBdA - I was hoping you would spot that.  Thanks for the link!  
I had a question about working with drives after reading reading renazonse's mention about functions, and seeing your example with env:, so I'm glad to see the mention in the reading about using this notation to work with PS drives.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:renazonse
ID: 41743188
function Processes ($Item) { Get-Process | ?{$_.Name -eq $Item} }
$Item = "spoolsv"
$S = New-PSSession -ComputerName $Computer -Credential $WindowsPass
$Data = Invoke-Command -Session $S -ScriptBlock ${function:Processes} -ArgumentList $Item
0
 
LVL 39

Author Comment

by:footech
ID: 41744504
It took some further investigation, but I finally figured out how that works.
Running ${function:Processes} just returns the scriptblock defining the function.  Same as if you had run
gi function:\Processes | Select -expand Scriptblock
0
 
LVL 39

Author Closing Comment

by:footech
ID: 41744510
Thank you both.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now