PowerShell | Comment déconnecter les utilisateurs Windows à distance

Rédigé par O. Clotaire 24 commentaires
Classé dans : Windows, PowerShell Mots clés : PowerShell, Windows, Windows 10, RDP
Parfois la session reste bloqué parce que nous ne nous sommes pas déconnecté correctement en fermant la session. D'autres fois, c'est un utilisateur qui reste connecté à son ordinateur trop longtemps sans raison. De plus en plus, vous allez devoir intervenir sur les ordinateurs des utilisateurs qui n'ont pas suivi la consigne de déconnexion et vous devrez fermer les sessions distante afin de libérer de la place.

Heureusement, nous pouvons prendre la main et forcer la fermeture des sessions à partir d’une autre machine.

Pour cela, nous utiliserons la commande quser :

PS> quser      
USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
User1                  6  Disc    1+15:55  1/9/2023 8:52 AM

Pour interroger des ordinateurs distants, nous pouvons utiliser le commutateur /server ou invoquer le poste distant :

Invoke-Command -ComputerName 'OrdinateurDistant' -ScriptBlock { quser }USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIMEUser1                                     6  Disc      1+15:55  1/9/2023 8:52 AMUser2                 rdp-tcp#40         10  Active          3  1/12/2023 3:54 PM


Nous forcerions la fermeture de la session distante avec la commande logoff comme cela :

Invoke-Command -ComputerName 'OrdinateurDistant' -ScriptBlock { logoff 10 }


Et en local :

Invoke-Command -ScriptBlock { logoff 10 }


Si nous exécutons à nouveau quser, nous constatons que la session User2 a été déconnecté.
Sur le blog de ipswitch, il est proposé de rassembler les deux commandes afin de pouvoir indiquer le nom d'un utilisateur plutôt que l'ID de la session, comme ceci : 

$scriptBlock = {
     $ErrorActionPreference = 'Stop'
 
     try {
         ## Find all sessions matching the specified username
         $sessions = quser | Where-Object {$_ -match 'abertram'}
         ## Parse the session IDs from the output
         $sessionIds = ($sessions -split ' +')[2]
         Write-Host "Found $(@($sessionIds).Count) user login(s) on computer."
         ## Loop through each session ID and pass each to the logoff command
         $sessionIds | ForEach-Object {
             Write-Host "Logging off session id [$($_)]..."
             logoff $_
         }
     } catch {
         if ($_.Exception.Message -match 'No user exists') {
             Write-Host "The user is not logged in."
         } else {
             throw $_.Exception.Message
         }
     }
 }
 
 ## Run the scriptblock's code on the remote computer
 PS> Invoke-Command -ComputerName REMOTECOMPUTER -ScriptBlock $scriptBlock
 
 Found 1 user login(s) on computer.
 Logging off session id [rdp-tcp#40] ...

Nous pouvons constater que la commande Invoke permet la création du scriptblock pour détecter si l'utilisateur indiqué est connecté et le déconnecte.
Ouaw !

A PROPOS

  • Grace à mon expérience dans les Systèmes d'Information, j'ai le privilège de partager mon expérience en les centralisant sous la forme d’une base de connaissance.
  • Parfois certaines de nos questions ne trouvent pas toujours de réponse. Le domaine de l’informatique évolue rapidement, il est alors important de pouvoir profiter des solutions déjà expérimentés.
  • Ce site sera de plus en plus accessible.
  • Je vous remercie d’avance pour vos éventuelles remarques constructives permettant son amélioration.

24 commentaires

#1  - https://66win6.it.com/ a dit :

Howdy! This post couldn't be written much better!
Going through this article reminds me of my
previous roommate! He always kept talking about this.

I am going to forward this information to him.
Fairly certain he will have a great read. I appreciate
you for sharing!

Répondre
#2  - win88 loker a dit :

Hi there, yeah this paragraph is really nice and I have learned
lot of things from it on the topic of blogging. thanks.

Répondre
#3  - 68win.quest a dit :

I am actually thankful to the owner of this website who has shared this
impressive article at at this time.

Répondre
#4  - https://hello88oo.com/ a dit :

Excellent beat ! I would like to apprentice whilst you amend
your site, how could i subscribe for a weblog web site?
The account helped me a appropriate deal. I had been a little bit acquainted of this your broadcast offered vivid transparent concept

Répondre
#5  - 888B a dit :

I wanted to thank you for this good read!! I absolutely enjoyed every little bit of it.
I have you saved as a favorite to look at new stuff you post…

Répondre
#6  - 888B a dit :

I wanted to thank you for this good read!! I absolutely enjoyed every little bit of it.
I have you saved as a favorite to look at new stuff you post…

Répondre
#7  - 79kingmobi a dit :

Hiya! Quick question that's totally off topic. Do you know how to make your site mobile friendly?
My weblog looks weird when viewing from my iphone4.
I'm trying to find a template or plugin that might be able to resolve this issue.
If you have any suggestions, please share. Many thanks!

Répondre
#8  - tk88 casa a dit :

magnificent issues altogether, you just gained a new reader.

What may you suggest in regards to your submit that you simply made
a few days ago? Any positive?

Répondre
#9  - https://999bet.art/ a dit :

Wow, wonderful blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your
web site is wonderful, as well as the content!

Répondre
#10  - 555win a dit :

It's amazing to pay a visit this web page and reading the
views of all friends regarding this article, while I am also zealous of
getting knowledge.

Répondre
#11  - ev88.rent a dit :

Amazing blog! Do you have any helpful hints for aspiring writers?
I'm planning to start my own blog soon but I'm a little lost on everything.
Would you recommend starting with a free platform
like Wordpress or go for a paid option? There are so many choices
out there that I'm completely overwhelmed ..

Any suggestions? Thanks!

Répondre
#12  - xoso66.st a dit :

I visit day-to-day some sites and sites to read articles,
but this blog offers quality based writing.

Répondre
#13  - topgamebai.it.com a dit :

I have fun with, cause I discovered exactly what I used to be
looking for. You have ended my four day lengthy
hunt! God Bless you man. Have a great day.
Bye

Répondre
#14  - 23win a dit :

Hello fantastic blog! Does running a blog similar to this require a large amount of work?
I've very little expertise in computer programming but I was hoping
to start my own blog soon. Anyway, if you have any ideas or tips for new blog owners please share.
I know this is off subject however I just had to ask. Thank you!

Répondre
#15  - https://23win23.net/ a dit :

Thanks for sharing your thoughts about https://23win23.net/. Regards

Répondre
#16  - Tk88nomco a dit :

Hi there, I believe your website could be having internet browser compatibility problems.
Whenever I take a look at your website in Safari, it looks
fine however, when opening in IE, it's got some overlapping issues.
I merely wanted to give you a quick heads up! Aside from that, fantastic site!

Répondre
#17  - https://sumiekanekomusic.com/ a dit :

Hi, Neat post. There's a problem together with your website in web explorer, may
check this? IE nonetheless is the market leader and a good portion of folks
will pass over your wonderful writing because of this problem.

Répondre
#18  - https://tk88.name/ a dit :

I'm not sure the place you're getting your info, however good
topic. I needs to spend some time learning much more
or figuring out more. Thank you for fantastic info I used to be searching for this info for my mission.

Répondre
#19  - ev88e.net a dit :

My programmer is trying to persuade me to move to .net from PHP.
I have always disliked the idea because of the expenses.

But he's tryiong none the less. I've been using
Movable-type on a number of websites for about a year and
am anxious about switching to another platform. I have
heard excellent things about blogengine.net. Is there a way I can transfer all my
wordpress content into it? Any kind of help would be really appreciated!

Répondre
#20  - sex chịch nhau không che a dit :

I all the time used to read article in news papers but now as I am a user
of net so from now I am using net for articles or reviews, thanks to web.

Répondre
#21  - TK88 a dit :

I do not know if it's just me or if perhaps everyone else experiencing issues with your website.
It appears like some of the text within your posts are running off the screen. Can somebody
else please provide feedback and let me know if this is happening
to them too? This might be a problem with my web browser because I've had this happen previously.
Many thanks

Répondre
#22  - yo88 a dit :

Hey there would you mind sharing which blog platform you're using?
I'm looking to start my own blog soon but I'm having a
hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I'm looking for something unique.
P.S Apologies for being off-topic but I had to ask!

Répondre
#23  - 777king.com a dit :

Hello to every body, it's my first pay a quick visit of this web site;
this web site carries remarkable and truly fine material
in favor of readers.

Répondre
#24  - como restaurar copia de seguridad de whatsapp a dit :

My brother recommended I might like this blog.
He was once entirely right. This submit actually made my
day. You can not consider simply how much time I had spent for this info!
Thanks!

Répondre

Écrire un commentaire

 Se rappeler de moi sur ce site
Quelle est le deuxième caractère du mot ma3kepd ?

Fil RSS des commentaires de cet article

↓