Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to find out, if a remote Session has allready been imported

Posted on 2016-08-24
10
Medium Priority
?
37 Views
Last Modified: 2016-09-21
When we import-pssession, Poweshell creates a temporary module for implicit remoting. When we remove-pssession, all the temporary modules that belong to it (Powershell creates a new module every time we Import the session!) are removed.

My goal is to know, if a certain PSSession has been already imported or not. The idea was to find out, if there are modules bound to this session. Unfortunately I could not find any properties in modules that would reference the session neither the session properties referencing the modules. But Powershell knows it somehow!

Does anyone have an idea?

Thanks
Alexander Lerner
0
Comment
Question by:AlLerner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 16

Expert Comment

by:FOX
ID: 41769024
run the command Get-PSSession to see which sessions are connected to the local computer
run the command Get-PSSessionConfiguration to see the registered session configurations on the local computer
0
 

Author Comment

by:AlLerner
ID: 41769032
Unfortunately it does not solve the Problem. Please read the question again. Thank you.
0
 
LVL 16

Expert Comment

by:FOX
ID: 41769044
The command Get-Module will show you all modules you have on your session
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:AlLerner
ID: 41769053
Thank you. On which session? I am still in my local session, but I have imported a remote-session and now I look for indices, that a certain remote session has already been imported.
0
 
LVL 16

Expert Comment

by:FOX
ID: 41769080
using Get-PSSession will show you all sessions on your local machine.  Each session has an ID/guid number. from there you can use a
Connect-PSSession IDNumber

in your powershell run a get-help Get-PSSession -detailed  (for syntax)  
Also run get-help PSSession -detailed                               (for more commands)
0
 

Author Comment

by:AlLerner
ID: 41769095
Dear Foxluv... Please, read my question. I don't want to know the ID of the session. I don't want to connect to session because it has never been disconnected. I want to know, if the session has been imported!!!!!!
0
 
LVL 41

Assisted Solution

by:footech
footech earned 2000 total points (awarded by participants)
ID: 41769351
If you look at a module's Description property you can see that it includes the name of the server that it's connected to for implicit remoting.   I suppose you could do a check on a PsSession's ComputerName property to see if any modules have a Description property that includes the computer name.

If you have PS 3.0+, you might consider using Import-Module <modulename> -PsSession $s
In this case, even if the session $s is removed, if you try to run on of the commands from the imported module, it will automatically try to reestablish a session for the implicit remoting.
0
 

Author Comment

by:AlLerner
ID: 41773849
Well, it is not exactly what I would like to use, because Microsoft can change the description of such module any time it likes, somewhere between the release 5 and 6. Mor than that, the description is in the language of OS, so it can be tricky to look, where exactly in is the URI (not actually the server name) of the session to look for. But there is likely nothin else one could actually use.
0
 
LVL 41

Accepted Solution

by:
footech earned 2000 total points (awarded by participants)
ID: 41774352
The temp modules are saved to the filesystem.  If you actually look in the module file, you can find the InstanceID of the PsSession.
Get-PSSession | Where { Select-String -path (Get-Module | Select -expand path) -pattern $_.instanceid.tostring() -simplematch }

Open in new window


I don't know if this will work for all use cases.
It might be better if you state what problem you're trying to solve, or even why you're trying to solve it.  Perhaps a better approach could be suggested.
0
 
LVL 41

Expert Comment

by:footech
ID: 41808376
ID: 41769351 provides a method that will work for at least some circumstances.
ID: 41774352 provides a method that is more reliable.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question