PowerShell Script remote ausführen? Hier Schließen einer SMB-Session

Bob.Dig

Captain
Registriert
Dez. 2006
Beiträge
3.914
Ich möchte eine remote SMB-Session schließen, von meinem PC aus. Geht das überhaupt?
Ich bin blutiger Anfänger, eigentlich nicht mal das...

Noch mal konkret, ich bin mit meinem PC auf der Freigabe meines Windows Servers. Nun möchte ich diese Session auf dem Server schließen, mittels eines Scriptes, welches ich auf meinem PC ausführen möchte.
 
Lösung
Ist das hier anfängerfreundlich genug?
https://learn.microsoft.com/en-us/p...shell.core/invoke-command?view=powershell-7.4

Example 2: Run a command on a remote server​

This example runs a Get-Culture command on the Server01 remote computer.

PowerShell:
Invoke-Command -ComputerName Server01 -Credential Domain01\User01 -ScriptBlock {
    Get-Culture
}
The ComputerName parameter specifies the name of the remote computer. The Credential parameter is used to run the command in the security context of Domain01\User01, a user who has permission to run commands. The ScriptBlock parameter specifies the command to be run on the remote computer.

In response, PowerShell requests the password...
Hab es jetzt erst mal mit winrm versucht, aber bis jetzt geht da nichts, irgendwas anfängerfreundliches dazu? Homeserver ist zwar ein WinServer22 aber nur in der Arbeitsgruppe und Vorwissen ist hier wie gesagt nicht vorhanden.
 
Ist das hier anfängerfreundlich genug?
https://learn.microsoft.com/en-us/p...shell.core/invoke-command?view=powershell-7.4

Example 2: Run a command on a remote server​

This example runs a Get-Culture command on the Server01 remote computer.

PowerShell:
Invoke-Command -ComputerName Server01 -Credential Domain01\User01 -ScriptBlock {
    Get-Culture
}
The ComputerName parameter specifies the name of the remote computer. The Credential parameter is used to run the command in the security context of Domain01\User01, a user who has permission to run commands. The ScriptBlock parameter specifies the command to be run on the remote computer.

In response, PowerShell requests the password and an authentication method for the User01 account. It then runs the command on the Server01 computer and returns the result.
 
Zuletzt bearbeitet:
Wenn du es ohne Script machen willst, gehts auch über mmc.exe. Da dann Snap-In hinzufügen und "Freigegebene Ordner" auswählen. Da sind dann auch die Sitzungen mit dabei. Bei "Anderer Computer" gibts du dann den FQDN oder IP deines Servers ein. Damit das funktioniert, muss die Remoteverwaltung auf dem Server aktiv sein (müsste über den Servermanager aktivierbar sein).
 

Anhänge

  • Unbenannt.png
    Unbenannt.png
    239,8 KB · Aufrufe: 34
  • Gefällt mir
Reaktionen: Bob.Dig und aragorn92
Bob.Dig schrieb:
Ich möchte eine remote SMB-Session schließen, von meinem PC aus...
Hallo magst du etwas zum Hintergrund sagen, welcher Prozess ist denn von Wo nach Wo verbunden und warum muss die Session extra geschlossen werden?

Ahoi D.
 
  • Gefällt mir
Reaktionen: Bob.Dig
Hab mich inzwischen zum Admin gemacht auf dem Server und dann scheint es auch zu funktionieren. Ich musste leider den Befehl mehrfach wiederholen, sonst war das Ergebnis nicht zufriedenstellend.
PowerShell:
Invoke-Command -ComputerName xyz -ScriptBlock {Close-SmbSession -ClientComputerName 192.168.1.10 -Force}
Invoke-Command -ComputerName xyz -ScriptBlock {Close-SmbSession -ClientComputerName 192.168.1.10 -Force}
Invoke-Command -ComputerName xyz -ScriptBlock {Close-SmbSession -ClientComputerName 192.168.1.10 -Force}
Invoke-Command -ComputerName xyz -ScriptBlock {Close-SmbSession -ClientComputerName 192.168.1.10 -Force}
Invoke-Command -ComputerName xyz -ScriptBlock {Close-SmbSession -ClientComputerName 192.168.1.10 -Force}
Was die Firewall betrifft, bin ich mir jetzt nicht sicher, ob oder wie ich es erlaubt habe, aktuell läuft's.
 
Zuletzt bearbeitet:
  • Gefällt mir
Reaktionen: tollertyp
Ungetestet, könnte auch gehen:
Code:
1..5 | Invoke-Command -ComputerName xyz -ScriptBlock {Close-SmbSession -ClientComputerName 192.168.1.10 -Force}

Weißt du, ob du es mehrmals aufrufen musst, weil beim Ziel nichts "ankommt", also der Befehl da nicht ausgeführt wird?
 
  • Gefällt mir
Reaktionen: Bob.Dig
Zurück
Oben