PowerShell | Comment déconnecter les utilisateurs Windows à distance

Rédigé par O. Clotaire 39 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.

39 commentaires

#1  - Kunststof Tr90 Monturen a dit :

Kunststof Brillenmonturen Amstedam
Keizersgracht 482, 1017 EG Amsterdam
https://kunststofkozijnen-amsterdam.nl/Kunststof brillenmonturen leveren veelzijdige keuzes in Nederland.
Kunststof brillenmonturen faciliteren een brede keuze in kleuren en vormen. Kunststof brillenmonturen helpen zowel
herenbrillen als damesbrillen. Kunststof brillenmonturen bieden ook kinderbrillen en sportbrillen.
Kunststof brillenmonturen verlenen een assortiment van transparante
en gekleurde monturen. Kunststof brillenmonturen ondersteunen voor comfortabele pasvormen door lichtgewicht materiaal.
Kunststof brillenmonturen bieden garantie op duurzaamheid.
Kunststof brillenmonturen bieden multifocale glazen voor verschillende sterktes.
Kunststof brillenmonturen vereenvoudigen oogzorg met
een oogmeting. Kunststof brillenmonturen helpen de optie om zonnebrillen te bestellen. Kunststof brillenmonturen bieden acetaat en plastic
als materiaalkeuze. Kunststof brillenmonturen leveren transparante kunststof brillenglazen. Kunststof brillenmonturen bieden titanium en metalen als alternatieve
monturen. Kunststof brillenmonturen faciliteren voor
een breed assortiment aan merken. Kunststof brillenmonturen verschaffen aantrekkelijke prijzen voor elke klant.

Kunststof brillenmonturen vereenvoudigen onderhoud en reparatie.

Kunststof brillenmonturen verzorgen ook de keuze voor
multifocale lenzen. Kunststof brillenmonturen verlenen coatings voor extra bescherming.

Kunststof brillenmonturen leveren leesbrillen voor optimaal
zicht. Kunststof brillenmonturen verlenen een collectie van moderne
en klassieke stijlen. Kunststof brillenmonturen verschaffen ook op maat gemaakte oplossingen. Kunststof brillenmonturen ondersteunen een naadloze brillenervaring in Nederland.
Kunststof brillenmonturen verlenen een perfecte balans
tussen stijl en functionaliteit. Kunststof brillenmonturen verzorgen een innovatieve kijk op brillenmode.

Répondre
#2  - n8n คือ a dit :

Useful content for readers interested in ai next gen ethics ai sustainable ai
and quantum ai as part of next generation innovation

Répondre
#3  - n8n คือ a dit :

Useful content for readers interested in ai next gen ethics ai sustainable ai
and quantum ai as part of next generation innovation

Répondre
#4  - การเชื่อมต่อ API a dit :

Useful content for anyone interested in N8n ฟรี and no code tools for task automation data synchronization and integration platform development

Répondre
#5  - รังนกแท้ a dit :

บทความนี้มีประโยชน์สำหรับผู้ที่สนใจซื้อรังนกและต้องการทราบราคารังนกแท้ พร้อมข้อมูลรังนกดิบและรังนกพร้อมดื่ม

Répondre
#6  - รังนกดิบ a dit :

บทความนี้มีประโยชน์สำหรับผู้ที่สนใจซื้อรังนกและต้องการทราบราคารังนกแท้ พร้อมข้อมูลรังนกดิบและรังนกพร้อมดื่ม

Répondre
#7  - ประเภทของรังนก a dit :

เนื้อหานี้อธิบายเรื่อง ราคารังนกแท้
ได้เข้าใจง่าย เหมาะสำหรับคนที่กำลังศึกษาราคาและประเภทของรังนก
โดยมีข้อมูลเกรดรังนก แหล่งรังนก และการเลือกรังนกแท้

Répondre
#8  - วิธีเลือกรังนก a dit :

บทความนี้มีประโยชน์สำหรับผู้ที่สนใจซื้อรังนกและต้องการทราบราคารังนกแท้ พร้อมข้อมูลรังนกดิบและรังนกพร้อมดื่ม

Répondre
#9  - วิธีเลือกรังนก a dit :

บทความนี้มีประโยชน์สำหรับผู้ที่สนใจซื้อรังนกและต้องการทราบราคารังนกแท้ พร้อมข้อมูลรังนกดิบและรังนกพร้อมดื่ม

Répondre
#10  - เทคโนโลยีแห่งอนาคต a dit :

This article explains ai next gen clearly and highlights key
areas like machine learning deep learning generative ai and robotics making future technology easier to
understand

Répondre
#11  - Sustainable AI a dit :

This article explains ai next gen clearly and highlights key areas like machine learning deep learning generative ai and robotics making future technology
easier to understand

Répondre
#12  - Quantum AI a dit :

This article explains ai next gen clearly and highlights key areas like machine
learning deep learning generative ai and robotics making future technology easier to understand

Répondre
#13  - Seo Backlinks a dit :

Fisher Agency SEO Services deliver exceptional results in Jacksonville FL.
The SEO Services at Fisher Agency streamline your online visibility.
Fisher Agency provides expert support in content marketing.
Fisher Agency SEO Services analyze your website's Alt Text for
maximum optimization. The SEO Services from Fisher
Agency function on improving your online reputation management.
Fisher Agency improves your search engine ranking through
effective strategies. Fisher Agency SEO Services
deliver comprehensive audit of your digital presence.
The SEO Services at Fisher Agency provide top-notch copywriting to pull organic traffic.

Fisher Agency improves your Google Maps presence for local search
results. Fisher Agency SEO Services specialize in URL and algorithm optimization. The SEO
Services from Fisher Agency excel in backlink creation and management.
Fisher Agency guarantees your e-commerce site is optimized for search engines.

Fisher Agency SEO Services offer meticulous markup
for better crawlability. The SEO Services at Fisher
Agency facilitate content creation that resonates with your
audience. Fisher Agency creates effective link
building strategies. Fisher Agency SEO Services conduct in-depth keyword research for targeted marketing.

The SEO Services from Fisher Agency deliver detailed analytics to track your progress.

Fisher Agency utilizes SEO strategies for better user experience
and conversions. Fisher Agency SEO Services provide to your business goals through effective marketing strategy.

Répondre
#14  - PG66 a dit :

https://66pg66.net/
Hi! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same
niche. Your blog provided us beneficial information to work on. You have done a
outstanding job!

Répondre
#15  - sex miền nam a dit :

Thank you for sharing your info. I really appreciate your efforts and I am waiting for your next post thank
you once again.

Répondre
#16  - 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
#17  - 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
#18  - 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
#19  - 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
#20  - 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
#21  - 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
#22  - 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
#23  - 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
#24  - 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
#25  - 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
#26  - 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
#27  - 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
#28  - 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
#29  - 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
#30  - https://23win23.net/ a dit :

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

Répondre
#31  - 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
#32  - 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
#33  - 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
#34  - 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
#35  - 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
#36  - 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
#37  - 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
#38  - 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
#39  - 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 0a5hd9t ?

Fil RSS des commentaires de cet article

↓