Solved

PowerShell syntax

Posted on 2016-08-03
6
43 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Author Closing Comment

by:footech
ID: 41744510
Thank you both.
0

Featured Post

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.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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