Last active
June 26, 2025 22:50
-
-
Save jhoneill/3eef2c58606c65fb59a66ad1b242c730 to your computer and use it in GitHub Desktop.
Demonstration of PowerShell Tied / automatic variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# The template for our class looks like this | |
// Constructor for our variable calls PSVariables' constructor with Name, value, options | |
public class XYZVariable : System.Management.Automation.PSVariable { | |
public XYZVariable () : base("XYZ", / Name | |
0, // Value | |
ScopedItemOptions.ReadOnly | // Options | |
ScopedItemOptions.AllScope){} | |
// variable objects have a value property, override it to return our value | |
public override object Value { | |
get { | |
// Whatever code you need to make an XYZ ( Must have a return ) | |
return the_XYZ_thing ; | |
} | |
} | |
} | |
Examples... #> | |
Add-type @" | |
using System.Management.Automation; | |
public class WhoAmiVariable : PSVariable { | |
public WhoAmiVariable () : base("WhoAmI", 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope){} | |
public override object Value {get { | |
return System.Security.Principal.WindowsIdentity.GetCurrent().Name ; | |
}}} | |
public class DefaultGatewayVariable : PSVariable { | |
public DefaultGatewayVariable () : base("Gateway", 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope) {} | |
public override object Value {get{ | |
using (PowerShell ps = PowerShell.Create()) | |
{ | |
ps.AddScript("(Get-NetRoute -DestinationPrefix '0.0.0.0/0').Nexthop"); | |
return ps.Invoke(); | |
} | |
}}} | |
public class NowVariable : PSVariable { | |
public NowVariable () : base("Now", 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope) {} | |
public override object Value {get { | |
return System.DateTime.Now; | |
}}} | |
"@ | |
# Create a new object for our type(s) and add to out session | |
$executioncontext.SessionState.PSVariable.Set((New-Object -TypeName DefaultGatewayVariable)) | |
$executioncontext.SessionState.PSVariable.Set((New-Object -TypeName NowVariable)) | |
$executioncontext.SessionState.PSVariable.Set((New-Object -TypeName WhoAmiVariable)) | |
#### After I showed the above at PSConf_EU @jborean93 showed me a trick for script properties in PowerShell classes. | |
class BatVariable : System.Management.Automation.PSVariable { | |
BatVariable () : base("Bat", 0, "ReadOnly, AllScope"){} | |
[object] get_Value(){ | |
$b = (Get-CimInstance -Namespace root\wmi -ClassName BatteryStatus ).RemainingCapacity / | |
(Get-CimInstance -Namespace root\wmi -ClassName BatteryFullChargedCapacity root\wmi).FullChargedCapacity | |
return $b.tostring("p0") | |
} | |
} | |
$executioncontext.SessionState.PSVariable.Set((New-Object -TypeName BatVariable)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment