{"id":6611,"date":"2015-04-17T00:01:00","date_gmt":"2015-04-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/04\/17\/combine-powershell-remoting-and-cim\/"},"modified":"2019-02-18T10:29:52","modified_gmt":"2019-02-18T17:29:52","slug":"combine-powershell-remoting-and-cim","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/combine-powershell-remoting-and-cim\/","title":{"rendered":"Combine PowerShell Remoting and CIM"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about combining Windows PowerShell remoting and CIM.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is remoting. It is not a new feature. In fact, it was introduced in Windows PowerShell&nbsp;2.0&mdash;a time that seems like eons ago. Really. That was back in Windows Server&nbsp;2008&nbsp;R2, so that was in 2009. Anyway, it is turned on by default in Windows Server 2012 and later.\nWindows PowerShell remoting lets me connect to remote servers or workstations, over a single port, and in a secure manner. In addition, it will take advantage of domain rights and integration. I can work on one remote server, or a whole bunch all at the same time.\nI like being able to connect to a specific server. When I do, it is like working in a Windows PowerShell console on my laptop. To do this, all I need to do is to use the <b>Enter-PSSession<\/b> cmdlet and specify the remote server. When I am there, I can use the <b>Get-CimInstance<\/b> cmdlet to retrieve WMI information, or I could use <b>Get-WmiObject<\/b> cmdlet. Because I know that I have the CIM cmdlets on the remove server, I will use <b>Get-CimInstance<\/b>. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Enter-PSSession -ComputerName dc1<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:UsersAdministratorDocuments&gt; Get-CimInstance win32_bios<\/p>\n<p style=\"margin-left:30px\">SMBIOSBIOSVersion : Hyper-V UEFI Release v1.0<\/p>\n<p style=\"margin-left:30px\">Manufacturer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Microsoft Corporation<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Hyper-V UEFI Release v1.0<\/p>\n<p style=\"margin-left:30px\">SerialNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3601-6926-9922-0181-5225-8175-58<\/p>\n<p style=\"margin-left:30px\">Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : VRTUAL &#8211; 1<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:UsersAdministratorDocuments&gt; exit<\/p>\n<p style=\"margin-left:30px\">PS C:&gt;\nNow why would I really do something like this? I mean, if I have Windows PowerShell remoting turned on, and I have the CIM cmdlets, why wouldn&rsquo;t I run my CIM commands remotely? For one thing, I may want to perform more than one command at a time. In addition, I may want to have more direct access to resources on the remote computer. For example, I can create folders and files on the remote server as if I was on my own laptop.\nIn the following example, I enter a remote Windows PowerShell session on a server named DC1. I change my working directory to the root of drive C, and then I create a folder off the root named FSO. I then use the <b>Get-CimInstance<\/b> cmdlet to retrieve WMI information from the <b>Win32_BIOS<\/b> WMI class, and I redirect the output to a text file in the FSO folder. I then use <b>Get-Content<\/b> to retrieve that content. Here are the commands and the output:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Enter-PSSession -ComputerName dc1<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:UsersAdministratorDocuments&gt; sl c:<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:&gt; md fso<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Directory: C:<\/p>\n<p style=\"margin-left:30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"margin-left:30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4\/15\/2015&nbsp; 11:52 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fso<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:&gt; Get-CimInstance win32_bios &gt;&gt; c:fsomybiosINFO.txt<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:&gt; get-content C:fsomybiosINFO.txt<\/p>\n<p style=\"margin-left:30px\">SMBIOSBIOSVersion : Hyper-V UEFI Release v1.0<\/p>\n<p style=\"margin-left:30px\">Manufacturer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Microsoft Corporation<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Hyper-V UEFI Release v1.0<\/p>\n<p style=\"margin-left:30px\">SerialNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3601-6926-9922-0181-5225-8175-58<\/p>\n<p style=\"margin-left:30px\">Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : VRTUAL &#8211; 1<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:&gt; exit<\/p>\n<p style=\"margin-left:30px\">PS C:&gt;\nNow, certainly, I could have done all of this without entering a direct remote Windows PowerShell session on DC1, but it would have been more complicated. One other thing that is cool to do is create multiple Windows PowerShell remote sessions, save the sessions, and then enter them as required.\nFirst I use the <b>New-PSSession<\/b> cmdlet to create two new PSSessions, and I store the resultant sessions in variables:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $dc1 = New-PSSession -ComputerName dc1<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $sgw = New-PSSession -ComputerName sgw\nNext, when I use the <b>Enter-PSSession<\/b> cmdlet, I specify the session that is contained in the variable:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Enter-PSSession -Session $dc1<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:UsersAdministratorDocuments&gt; gcim Win32_LocalTime<\/p>\n<p style=\"margin-left:30px\">Day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 15<\/p>\n<p style=\"margin-left:30px\">DayOfWeek&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3<\/p>\n<p style=\"margin-left:30px\">Hour&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 23<\/p>\n<p style=\"margin-left:30px\">Milliseconds&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Minute&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 58<\/p>\n<p style=\"margin-left:30px\">Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 4<\/p>\n<p style=\"margin-left:30px\">Quarter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2<\/p>\n<p style=\"margin-left:30px\">Second&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10<\/p>\n<p style=\"margin-left:30px\">WeekInMonth&nbsp;&nbsp;&nbsp; : 3<\/p>\n<p style=\"margin-left:30px\">Year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2015<\/p>\n<p style=\"margin-left:30px\">PSComputerName :<\/p>\n<p style=\"margin-left:30px\">[dc1]: PS C:UsersAdministratorDocuments&gt; exit\nWhat is really cool is that&nbsp; I can now use the <b>Up<\/b> arrow to recall the commands that I ran on the other server. The commands and output are shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Enter-PSSession -Session $sgw<\/p>\n<p style=\"margin-left:30px\">[sgw]: PS C:Usersadministrator.NWTRADERSDocuments&gt; gcim Win32_LocalTime<\/p>\n<p style=\"margin-left:30px\">Day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 15<\/p>\n<p style=\"margin-left:30px\">DayOfWeek&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3<\/p>\n<p style=\"margin-left:30px\">Hour&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 23<\/p>\n<p style=\"margin-left:30px\">Milliseconds&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Minute&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 58<\/p>\n<p style=\"margin-left:30px\">Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 4<\/p>\n<p style=\"margin-left:30px\">Quarter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2<\/p>\n<p style=\"margin-left:30px\">Second&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 30<\/p>\n<p style=\"margin-left:30px\">WeekInMonth&nbsp;&nbsp;&nbsp; : 3<\/p>\n<p style=\"margin-left:30px\">Year&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: 2015<\/p>\n<p style=\"margin-left:30px\">PSComputerName :<\/p>\n<p style=\"margin-left:30px\">[sgw]: PS C:Usersadministrator.NWTRADERSDocuments&gt; exit\nNow you know how to use Windows PowerShell remoting and CIM. Join me tomorrow when I will talk about more cool stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about combining Windows PowerShell remoting and CIM. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is remoting. It is not a new feature. In fact, it was introduced in Windows PowerShell&nbsp;2.0&mdash;a time that seems like eons ago. Really. That was back [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[385,57,3,4,45],"class_list":["post-6611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-cim","tag-remoting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about combining Windows PowerShell remoting and CIM. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is remoting. It is not a new feature. In fact, it was introduced in Windows PowerShell&nbsp;2.0&mdash;a time that seems like eons ago. Really. That was back [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=6611"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=6611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}