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

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 the psexec -i execution of remote gui process on that session of that user on the remote machine.
So far i managed to use
psexec \\remoteMachine -u user -p pswrd query session
to get list of sessions on the remote machine:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console 0 Conn wdcon
rdp-tcp#919 user 1 Active rdpwd
rdp-tcp#916 user 3 Active rdpwd
so i guess i could somehow isolate the needed id and use it - but haven't managed to do that yet
Any ideas?
Maybe other - simpler ways?
Thanks for the help.

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.

It's possible to do that without PowerShell.
There is qwinsta command line tool that ships with Windows that you can use.
Example:
c:\>qwinsta
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
console 1 Conn
>rdp-tcp#0 YourUser 2 Active rdpwd
rdp-tcp 65536 Listen
Usage:
c:\>qwinsta /?
Display information about Remote Desktop Sessions.
QUERY SESSION [sessionname | username | sessionid]
[/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER] [/VM]
sessionname Identifies the session named sessionname.
username Identifies the session with user username.
sessionid Identifies the session with ID sessionid.
/SERVER:servername The server to be queried (default is current).
/MODE Display current line settings.
/FLOW Display current flow control settings.
/CONNECT Display current connect settings.
/COUNTER Display current Remote Desktop Services counters information.
/VM Display information about sessions within virtual machines.

With the PSTerinalServices powershell module you can get the user sessions and IDs.
The module can be found here: http://code.msdn.microsoft.com/PSTerminalServices
PS > Get-TSSession -UserName user1 -ComputerName pc1 | select UserName,SessionId
UserName SessionId
-------- ---------
User 1

Related

remove a job form cluster

I am on Linux VM. I know there is the command condor_submit hello.submit that will submit a job to cluster. However if I want to remove the jobs submitted. Is there a way? I created a user first using the command: sudo adduser user2 then logged in to user2 with command su user2 to then submit a job using the code above. How can I "unsubmit"/remove it?
the condor_rm command is what you are looking for. It takes either the job id that condor_submit printed out, or -a for all jobs, or if you are root, the name of the user whose jobs you want to remove.

How to automate the Lastpass CLI login process

I was wondering if anyone successfully automated the LastPass CLI login process. I cannot seem to get around the password prompt, which I need to automate to make it useful.
I've tried commands like
echo <password> | lpass login <username>
or
yes <password> | lpass login <username>
I keep getting output saying the password is not correct. Has anyone automated this?
Thanks!
You would probably need to disable the pinetry password prompt so that lpass reads your password from standard input.
As the man pages say:
If pinentry program is unavailable, or if the LPASS_DISABLE_PINENTRY environment variable is set to 1, passwords will be read from standard input and a prompt will be displayed on standard error.
Meaning: It should work if you run it like this:
echo <password> | LPASS_DISABLE_PINENTRY=1 lpass login <username>
Or by just setting the environment variable for your shell (export LPASS_DISABLE_PINENTRY=1 in your ~/.profile or similar)

Cygwin: ssh-host-config fails with 'local group does not exist'

I am trying to install Hbase on a VM under Windows 7.
I followed the instructions and everything is OK until i do the ssh-host-config.
I see the following:
>Should privilege separation be used? Yes
>new local account ‘sshd’? Yes
>Do you want to install sshd as a service? Yes
>Enter the value of CYGWIN for the daemon?[] just <enter>
>Do you want to use a different name? No
>create new privileged user account ‘cyg_server’ account? Yes
>Please enter the password:
>Reenter:
>User has been created
>System error 1376 has occurred
>The specified local group does not exist.
>Adding user cyg_server to local group root failed
>Please add cyg_server to local group before
You can check what that lame error means, with:
$ NET HELPMSG 1376
The specified local group does not exist.
The problem seem to be that the script is not giving the new user account "Administrator" group membership. You can check this with: net user cyg_server from a normal windows command shell (CMD). Make sure the line reads:
Local Group Memberships *Administrators *Users
If it doesn't, you need to open the Windows control panel and navigate to User Accounts.
There you will find a new account called "Privileged server", which is the cyg_server account. You need to change the account type of that from Standard to Administrator.
Now restart sshd and check that it's running with:
$ cygrunsrv.exe --query sshd
Service : sshd
Display name : CYGWIN sshd
Current State : Running
Controls Accepted : Stop
Command : /usr/sbin/sshd -D
However, this is probably not the end of the story as these accounts cannot be used to login with, if they do not have a password assigned. You need to create (and add to /etc/passwd) a new account with remote login rights. And don't forget to check account password expiration...

cPanel Unable to get user id for user

I have three server, we will call them Ebonroc, Anzu and Onyx.
All Three are running the same version of Redhat and the same version of cPanel/WHM.
Onyx is a remote server and the other two are local.
When I try and copy and account using the WHM "Copy an Account From Another Server" tool from Onyx to Ebonroc it fails with only this error. (Unable to get user id for user $Username at /usr/local/cpanel/scripts/pkgacct line 155. Command failed with exit status 255).
If I copy the same account from Onyx to Anzu it will work, and it will also work if i then copy the same account from Anzu to Ebonroc after it has been transferred to Anzu but it will never work from Onyx to Ebonroc.
Both Ebonroc and Anzu are behind the same firewall and i do not see anything being blocked during either servers attempts.
Does anyone have an idea why I can’t transfer between Onyx and Ebonroc?
You should double check that the account name (cPanel username) is exist in the server Onyx.
try grep user_name /etc/trueuserdomains
And see that that account exists. If you sure that the account user_name is exists, then try taking the backup of the account manually
/scripts/pkgacct user_name
If you are still getting error as mentioned above, then check /etc/passwd file
grep user_name /etc/passwd
You should see a line starting like as below
user_name:x:uuu:ggg::/home/user_name:/bin/bash
Instead of /bin/bash you may see /usr/local/cpanel/bin/noshell if you haven't enabled the shell access for the account
uuu >> userID
ggg >> groupID
If you are not getting any o/p for the "grep user_name /etc/passwd", Then the account isn't created correctly.
If the account doesn't created correctly, cPanel can't detect the corresponding details.
Let us know with your results.

jsch + running multiple command in shell after SU and then close channal

My requirement is as follow:
I have to login to Unix box using my credentials and once login, I have to do su to different user. I need to provide password and reason for scsu command.
once su successful, I have to run set of commands one after another as that user.
On completion of executions, close channel and session both.
I tried to use Shell channel but I am able to fire scsu only ... need help to complete remaining requirement of executions of command and closing channel