Combine PowerShell Remoting and CIM

Doctor Scripto

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 2.0—a time that seems like eons ago. Really. That was back in Windows Server 2008 R2, so that was in 2009. Anyway, it is turned on by default in Windows Server 2012 and later. Windows 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. I 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 Enter-PSSession cmdlet and specify the remote server. When I am there, I can use the Get-CimInstance cmdlet to retrieve WMI information, or I could use Get-WmiObject cmdlet. Because I know that I have the CIM cmdlets on the remove server, I will use Get-CimInstance. Here is an example:

PS C:> Enter-PSSession -ComputerName dc1

[dc1]: PS C:UsersAdministratorDocuments> Get-CimInstance win32_bios

SMBIOSBIOSVersion : Hyper-V UEFI Release v1.0

Manufacturer      : Microsoft Corporation

Name              : Hyper-V UEFI Release v1.0

SerialNumber      : 3601-6926-9922-0181-5225-8175-58

Version           : VRTUAL – 1

[dc1]: PS C:UsersAdministratorDocuments> exit

PS C:> Now 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’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. In 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 Get-CimInstance cmdlet to retrieve WMI information from the Win32_BIOS WMI class, and I redirect the output to a text file in the FSO folder. I then use Get-Content to retrieve that content. Here are the commands and the output:

PS C:> Enter-PSSession -ComputerName dc1

[dc1]: PS C:UsersAdministratorDocuments> sl c:

[dc1]: PS C:> md fso

    Directory: C:

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

d—-         4/15/2015  11:52 PM            fso

[dc1]: PS C:> Get-CimInstance win32_bios >> c:fsomybiosINFO.txt

[dc1]: PS C:> get-content C:fsomybiosINFO.txt

SMBIOSBIOSVersion : Hyper-V UEFI Release v1.0

Manufacturer      : Microsoft Corporation

Name              : Hyper-V UEFI Release v1.0

SerialNumber      : 3601-6926-9922-0181-5225-8175-58

Version           : VRTUAL – 1

[dc1]: PS C:> exit

PS C:> Now, 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. First I use the New-PSSession cmdlet to create two new PSSessions, and I store the resultant sessions in variables:

PS C:> $dc1 = New-PSSession -ComputerName dc1

PS C:> $sgw = New-PSSession -ComputerName sgw Next, when I use the Enter-PSSession cmdlet, I specify the session that is contained in the variable:

PS C:> Enter-PSSession -Session $dc1

[dc1]: PS C:UsersAdministratorDocuments> gcim Win32_LocalTime

Day            : 15

DayOfWeek      : 3

Hour           : 23

Milliseconds   :

Minute         : 58

Month          : 4

Quarter        : 2

Second         : 10

WeekInMonth    : 3

Year           : 2015

PSComputerName :

[dc1]: PS C:UsersAdministratorDocuments> exit What is really cool is that  I can now use the Up arrow to recall the commands that I ran on the other server. The commands and output are shown here:

PS C:> Enter-PSSession -Session $sgw

[sgw]: PS C:Usersadministrator.NWTRADERSDocuments> gcim Win32_LocalTime

Day            : 15

DayOfWeek      : 3

Hour           : 23

Milliseconds   :

Minute         : 58

Month          : 4

Quarter        : 2

Second         : 30

WeekInMonth    : 3

Year           : 2015

PSComputerName :

[sgw]: PS C:Usersadministrator.NWTRADERSDocuments> exit Now you know how to use Windows PowerShell remoting and CIM. Join me tomorrow when I will talk about more cool stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon