How to determine the session id on remote machine for usage with psexec -i using script/powershell/…?

后端 未结 3 832
独厮守ぢ
独厮守ぢ 2020-12-30 14:11

I am in need of a script or powershell command that will be able to determine the session id of a specific logged in user on remote machine, to be later used as parameter to

3条回答
  •  鱼传尺愫
    2020-12-30 14:46

    As long as you're using PSExec, I would just stick with it. You can get the ID field pretty easily given a username e.g.:

    $username = 'joe'
    $results = psexec \\remoteMachine -u adminuser -p password query session
    $id = $results | Select-String "$username\s+(\w+)" |
                     Foreach {$_.Matches[0].Groups[1].Value}
    
    psexec \\remoteMachine -u $username -i $id -d notepad.exe
    

    Note that you want to use -d with PSExec otherwise it will wait until the launched program exits.

提交回复
热议问题