?
Solved

PowerShell syntax

Posted on 2016-08-03
6
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 30

Assisted Solution

by:Britt Thompson
Britt Thompson earned 800 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 85

Accepted Solution

by:
oBdA earned 1200 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 40

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 30

Expert Comment

by:Britt Thompson
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 40

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
1
 
LVL 40

Author Closing Comment

by:footech
ID: 41744510
Thank you both.
0

Featured Post

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 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