Solved

PowerShell syntax

Posted on 2016-08-03
6
38 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

9 Experts available now in Live!

Get 1:1 Help Now