{"id":4311,"date":"2009-03-26T18:17:00","date_gmt":"2009-03-26T18:17:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/03\/26\/tied-variables-in-powershell\/"},"modified":"2019-02-18T13:12:45","modified_gmt":"2019-02-18T20:12:45","slug":"tied-variables-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/tied-variables-in-powershell\/","title":{"rendered":"Tied Variables in PowerShell"},"content":{"rendered":"<p>With Add-Type and $executioncontext you can add special varibles that have tied values.<\/p>\n<p>I made $random, and $now <\/p>\n<p>add-type @&#8221;<br \/>using System;<br \/>using System.Management.Automation;<br \/>public class RandomVariable : PSVariable<br \/>{<br \/>Random r;<br \/>public RandomVariable ()<br \/>&nbsp; : base(&#8220;Random&#8221;, 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope)<br \/>&nbsp; {r = new Random();}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; public override object Value<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp; r.Next();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>}<br \/>&#8220;@<br \/>$executioncontext.SessionState.PSVariable.Set((new-object RandomVariable))<\/p>\n<p>add-type @&#8221;<br \/>using System;<br \/>using System.Management.Automation;<br \/>public class NowVariable : PSVariable<br \/>{<br \/>public NowVariable ()<br \/>&nbsp; : base(&#8220;Now&#8221;, 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope)<br \/>&nbsp; {}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public override object Value<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp; DateTime.Now;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>}<br \/>&#8220;@<br \/>$executioncontext.SessionState.PSVariable.Set((new-object NowVariable))<\/p>\n<p>Results is,<br \/>PS C:\\tmp&gt; $Random<br \/>229309908<br \/>PS C:\\tmp&gt; $Random<br \/>1759972224<br \/>PS C:\\tmp&gt; $Now<br \/>Thursday, 26 March 2009 3:20:29 PM<\/p>\n<p>I got this from Lee, where you can bind the behaviour to ScriptBlocks instead of C#, making it even easier to use. See attached <\/p>\n<p>&nbsp;It has in it,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(getter != null) { return getter.Invoke(); }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else { return null; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n<p>Which is a simple way to cross over to PowerShell from C#<br \/>And you can use it like so,<br \/>PS C:\\temp&gt; New-ScriptVariable.ps1 GLOBAL:today { (Get-Date).DayOfWeek }<br \/>PS C:\\temp&gt; $today<br \/>Wednesday<\/p>\n<p>Cheers,<br \/>Ibrahim Abdul Rahim [MSFT]<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/09\/51\/22\/01\/New-ScriptVariable.ps1.txt\">New-ScriptVariable.ps1.txt<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With Add-Type and $executioncontext you can add special varibles that have tied values. I made $random, and $now add-type @&#8221;using System;using System.Management.Automation;public class RandomVariable : PSVariable{Random r;public RandomVariable ()&nbsp; : base(&#8220;Random&#8221;, 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope)&nbsp; {r = new Random();} &nbsp;&nbsp;&nbsp;&nbsp; public override object Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp; r.Next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }}&#8220;@$executioncontext.SessionState.PSVariable.Set((new-object RandomVariable)) add-type @&#8221;using System;using [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[96,137,54],"class_list":["post-4311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-add-type","tag-ctp3","tag-scriptblock"],"acf":[],"blog_post_summary":"<p>With Add-Type and $executioncontext you can add special varibles that have tied values. I made $random, and $now add-type @&#8221;using System;using System.Management.Automation;public class RandomVariable : PSVariable{Random r;public RandomVariable ()&nbsp; : base(&#8220;Random&#8221;, 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope)&nbsp; {r = new Random();} &nbsp;&nbsp;&nbsp;&nbsp; public override object Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp; r.Next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }}&#8220;@$executioncontext.SessionState.PSVariable.Set((new-object RandomVariable)) add-type @&#8221;using System;using [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4311"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}