?
Solved

PowerShell syntax

Posted on 2016-08-03
6
Medium Priority
?
76 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 41

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 41

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 41

Author Closing Comment

by:footech
ID: 41744510
Thank you both.
0

Featured Post

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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