Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Get Variable from C# Cmdlet

I'm writing a C# Cmdlet that needs to get the value of a global script variable. How do I do it?

I noticed that the Runspace has SessionStateProxy.GetVariable method. Can I access the runspace from a C# Cmdlet?

Thanks!

like image 750
Yuval Peled Avatar asked Nov 11 '09 00:11

Yuval Peled


People also ask

How do I get a variable in PowerShell?

Description. The Get-Variable cmdlet gets the PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name.

What is $ENV in PowerShell?

The $env:PATHEXT variable contains a list of file extensions that Windows considers to be executable files. When a script file with one of the listed extensions is executed from PowerShell, the script runs in the current console or terminal session.

What is $_ in PowerShell script?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

If you're implementing a PSCmdlet use the this variable to access it like so:

this.SessionState.PSVariable.GetValue()
like image 148
Keith Hill Avatar answered Sep 19 '22 11:09

Keith Hill