footech
asked on
PowerShell syntax
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:compu tername)_$ (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!
"C:\temp\logs\${env:computername}_$(get-date -f dd-MM-yyyy)_CHKDSKResults.
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:compu
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!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
$Item = "spoolsv"
$S = New-PSSession -ComputerName $Computer -Credential $WindowsPass
$Data = Invoke-Command -Session $S -ScriptBlock ${function:Processes} -ArgumentList $Item
ASKER
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
Running ${function:Processes} just returns the scriptblock defining the function. Same as if you had run
gi function:\Processes | Select -expand Scriptblock
ASKER
Thank you both.
ASKER
@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.