{"id":3921,"date":"2009-06-04T18:32:00","date_gmt":"2009-06-04T18:32:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/06\/04\/managing-remote-sessions\/"},"modified":"2019-02-18T13:12:35","modified_gmt":"2019-02-18T20:12:35","slug":"managing-remote-sessions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/managing-remote-sessions\/","title":{"rendered":"Managing Remote Sessions"},"content":{"rendered":"<h1 align=\"center\"><font color=\"#365f91\" size=\"5\" face=\"Cambria\"><\/font>&nbsp;<\/h2>\n<p class=\"MsoNormal\"><span lang=\"EN\"><font size=\"3\"><font face=\"Calibri\">PowerShell V2 introduces a new capability which allows you to remotely manage machines in your organization. You may have already tried this new feature. In this blog, I will show how an administrator can manage different remote sessions (created by different users from different clients)<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><b><span lang=\"EN\">Scenario:<\/span><\/b><span lang=\"EN\"> Using PowerShell remoting, normal users can perform non-admin tasks on a machine remotely. There might be situations where an Administrator of the machine may want to terminate specific sessions created by specific users.<\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">This Administrator task can be achieved by just restarting WinRM service. However this is not a good solution as it would close all the remote sessions including the Administrator\u2019s sessions (if any). To remove specific sessions, WSMan\/WinRM provides cmdlets Get-WSManInstance &amp; Remove-WSManInstance. I will show you how this works.<\/font><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"># Create a remote session as a normal user<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; $env:computername<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">KRISCV-LH<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; $s = nsn kriscv-jhoom -cred <b>kriscv-jhoom\\testuser<\/b> -Authentication negotiate<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; $s<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"><span>&nbsp;<\/span>Id Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>ComputerName<span>&nbsp;&nbsp;&nbsp; <\/span>State<span>&nbsp;&nbsp;&nbsp; <\/span>ConfigurationName<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Availability<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"><span>&nbsp;<\/span>&#8212; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8212;<span>&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8211;<span>&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8212;<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"><span>&nbsp; <\/span>6 Session6<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>kriscv-jhoom<span>&nbsp;&nbsp;&nbsp; <\/span>Opened<span>&nbsp;&nbsp; <\/span>Microsoft.PowerShell<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Available<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\" face=\"Calibri\">PS C:\\&gt;<\/font><\/span><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">From machine KRISCV-LH, I connected to KRISCV-JHOOM as a <i>testuser<\/i>. This <i>testuser<\/i> is not an admin on Kriscv-Jhoom. Let\u2019s say this <i>testuser<\/i> is consuming lot of CPU on Kriscv-Jhoom and not letting others to do their work. In this scenario, the Administrator of Kriscv-Jhoom can delete the remote sessions created by <i>testuser<\/i> using Get-WSManInstance and Remove-WSManInstance cmdlets.<span>&nbsp; <\/span>These cmdlets are remote enabled meaning that these cmdlets can be run either locally on Kriscv-Jhoom or from a remote machine (You should provide Administrator credentials). Let\u2019s see how this works:<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; $env:computername<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">KRISCV-Win7<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; <b>Get-WSManInstance -ConnectionURI http:\/\/kriscv-jhoom.wingroup.windeploy.ntdev.microsoft.com:<\/b><\/font><\/font><\/span><\/p>\n<p class=\"Code\"><b><span><font size=\"3\"><font face=\"Calibri\">5985\/wsman shell -enumerate -cred wingroup\\kriscv<\/font><\/font><\/span><\/b><\/p>\n<p class=\"Code\"><span><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">rsp<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: http:\/\/schemas.microsoft.com\/wbem\/wsman\/1\/windows\/shell<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">lang<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: en-US<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">ShellId<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: <b>884D2DB4-C454-4F1C-9AF6-A7DA3D5D8BD7<\/b><\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">ResourceUri<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: http:\/\/schemas.microsoft.com\/powershell\/Microsoft.PowerShell<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><b><span><font size=\"3\"><font face=\"Calibri\">Owner<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: kriscv-jhoom\\testuser<\/font><\/font><\/span><\/b><\/p>\n<p class=\"Code\"><b><span><font size=\"3\"><font face=\"Calibri\">ClientIP<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2001:4898:2b:2:4878:5933:c82c:2cbd<\/font><\/font><\/span><\/b><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">IdleTimeOut<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: PT180.000S<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">InputStreams<span>&nbsp;&nbsp;&nbsp; <\/span>: stdin pr<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">OutputStreams<span>&nbsp;&nbsp; <\/span>: stdout<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">ShellRunTime<span>&nbsp;&nbsp;&nbsp; <\/span>: P0DT0H17M33S<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\" face=\"Calibri\">ShellInactivity : P0DT0H0M33S<\/font><\/span><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">Notice how I am using Get-WSManInstance. In the ConnectURI parameter, I am using the port number 5985 as the WinRM\/WSMan service on Kriscv-Jhoom is listening on this port. That brings us to the point of Port change. By default starting from Win7 RC, WinRM listens on port 5985 <b>not<\/b> port 80(for http traffic). The \/WSMan in the query portion implies to retrieve remote sessions serviced by WSMan (WinRM) service.<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">Notice the output, for each remotely created session you are getting information like user who created this session, from which client machine is this remote session established, the ID, the runtime describing how many days,hours,minutes and seconds the session is active etc.<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">To remove the session, use Remove-WSManInstance cmdlet supplying the ID like this:<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; remove-WSManInstance -ConnectionURI http:\/\/kriscv-jhoom.wingroup.windeploy.ntdev.microsoft.c<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\" face=\"Calibri\">om:5985\/wsman shell @{ShellID=&#8221;884D2DB4-C454-4F1C-9AF6-A7DA3D5D8BD7&#8243;} -cred wingroup\\kriscv<\/font><\/span><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">This will remove the session created by <i>testuser<\/i>!! <\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">Now let\u2019s see how this is reflected in the $s (session) variable created by testuser on Kriscv-LH<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt;<span>&nbsp; <\/span>$env:computername<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">KRISCV-LH<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\">PS C:\\&gt; $s<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"><span>&nbsp;<\/span>Id Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>ComputerName<span>&nbsp;&nbsp;&nbsp; <\/span>State<span>&nbsp;&nbsp;&nbsp; <\/span>ConfigurationName<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Availability<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"><span>&nbsp;<\/span>&#8212; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8212;<span>&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8211;<span>&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8212;<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><span><font size=\"3\"><font face=\"Calibri\"><span>&nbsp; <\/span>6 Session6<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>kriscv-jhoom<span>&nbsp;&nbsp;&nbsp; <\/span>Broken<span>&nbsp;&nbsp; <\/span>Microsoft.PowerShell<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>None<\/font><\/font><\/span><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">The session is Broken!! So <i>testuser<\/i> has to create a new session to continue his work. An Administrator can choose to totally block <i>testuser<\/i> from a creating any session remotely using \u201cSet-PSSessionConfiguration\u201d cmdlet. The \u2013ShowSecurityDescriptorUI parameter will show a nice UI to make these decisions easily.<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">Thanks<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">Krishna<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">Windows PowerShell Development<\/font><\/p>\n<p class=\"Code\"><font size=\"3\" face=\"Calibri\">This posting is provided \u201cAS IS\u201d with no warranties.<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; PowerShell V2 introduces a new capability which allows you to remotely manage machines in your organization. You may have already tried this new feature. In this blog, I will show how an administrator can manage different remote sessions (created by different users from different clients) Scenario: Using PowerShell remoting, normal users can perform non-admin [&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":[8],"class_list":["post-3921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-remoting"],"acf":[],"blog_post_summary":"<p>&nbsp; PowerShell V2 introduces a new capability which allows you to remotely manage machines in your organization. You may have already tried this new feature. In this blog, I will show how an administrator can manage different remote sessions (created by different users from different clients) Scenario: Using PowerShell remoting, normal users can perform non-admin [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3921","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=3921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3921\/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=3921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}