{"id":514,"date":"2008-07-29T02:00:50","date_gmt":"2008-07-29T10:00:50","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/29\/live-variables\/"},"modified":"2008-07-29T02:00:50","modified_gmt":"2008-07-29T10:00:50","slug":"live-variables","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/29\/live-variables\/","title":{"rendered":"&quot;Live&quot; Variables"},"content":{"rendered":"<p>In a recent PowerShell <a href=\"http:\/\/www.scriptinganswers.com\" target=\"_blank\">ScriptingAnswers.com<\/a> post, a member was asking about variables and whether they were &#8220;live&#8221; or linked to an actual object.The answer is &#8220;Yes and No, depending&#8230;&#8221;&nbsp; For example, consider this expression:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $s=get-service wsearch<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The $s variable holds the wsearch service object.&nbsp; I can check it&#8217;s status:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $s.status\nRunning\nPS C:\\&gt;<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I could even stop it:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $s.stop()\n<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>However, $s will still show a status of Running while the actual service shows that it was stopped.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $s.status\nRunning\nPS C:\\&gt; get-service wsearch\n\nStatus   Name               DisplayName\n------   ----               -----------\nStopped  WSearch            Windows Search\n<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The $s variable is quasi-live but is really just a container for the service object as it existed when the object was created. If I had stopped the service after creating $s, $s would still show it running.&nbsp; What the forum member was after was a live or linked variable to the service object. Well we can&#8217;t use a variable, but because PowerShell is object based I can use a different type of object.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $s={get-service wsearch}\n<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>$s is now a script block. To see the current object all I need to do is invoke it:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; &amp;$s\n\nStatus   Name               DisplayName\n------   ----               -----------\nRunning  WSearch            Windows Search\n<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I can use the variable in the pipeline:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; &amp;$s | stop-service -whatif\nWhat if: Performing operation \"Stop-Service\" on Target \"Windows Search (WSearch)\".<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Or, which is what the user was after, call methods that will affect the actual service object:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; (&amp;$s).stop()\nPS C:\\&gt; (&amp;$s).status\nStopped\nPS C:\\&gt; (&amp;$s).start()\nPS C:\\&gt; (&amp;$s).status\nStartPending\nPS C:\\&gt; (&amp;$s).status\nRunning\nPS C:\\&gt;<\/pre>\n<p><\/font><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Invoking the $s variable gives me the actual service object in its current and live state.&nbsp; I like efficiency and I can&#8217;t imagine it getting more efficient than that. <\/p>\n<p>So even though a normal variable is merely a point in time snapshot, there are ways to create more <em>lively<\/em> PowerShell variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent PowerShell ScriptingAnswers.com post, a member was asking about variables and whether they were &#8220;live&#8221; or linked to an actual object.The answer is &#8220;Yes and No, depending&#8230;&#8221;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[246,245,28,57,46,244],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-console","tag-get-service","tag-powershell","tag-scripting","tag-scriptinganswers","tag-variable"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=514"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":12765,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions\/12765"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}