PsExec copy script and run on remote computer - powershell

I am trying to show an confirmation-box on a remote computer and from what I understand I can't use invoke-command for any interactive scripts.
I found some tips in a forum-post that I need to run PsExec to be able to run interactive scripts.
I managed to get the script running on a remote host by using:
PsExec \\<Host> -s -i powershell -WindowStyle Hidden c:\temp\test.ps1
But that just works if the script is already in "c:\temp\test.ps1" on the remote host. I have tried using:
PsExec \\<host> -i -s powershell.exe -c c:\dev\scripts\test.ps1 test.ps1
and different variations of the "c" and "f" parameters but I only get a flashing black window on the remote host when I run the command from my server. What am I doing wrong?

Related

Is there a way to execute powershell commands remotely on a domain user from the DC?

Let's say for example, I have a domain controller and a client that is joined to the domain.
If I wanted to remotely lock out the client I would supposedly run
Invoke-Command -ComputerName [workstation name] -ScriptBlock {rundll32.exe user32.dll, LockWorkStation}
However this does not work. I'm assuming this is because the Invoke-Command cmdlet runs the code in the scriptblock but returns anything back to the local terminal. What I'm trying to accomplish is to have the code or powershell script run locally on the remote computer.
My question is first of all if this is the correct approach and second why the command I'm running does not work.
Download PsExec from https://learn.microsoft.com/en-us/sysinternals/downloads/psexec and run following command.& "C:\PSTools\PsExec.exe" -s -i \\COMPNAME rundll32.exe user32.dll,LockWorkStation
As per my comment when using PSExec... So, stuff like this ---
PsExec.exe \\ -d -u \Administrator -i cmd.exe /c "C:\windows\system32\rundll32.exe user32.dll, LockWorkStation"
Or using PowerShell with quer.exe ...
(it's a tool in every modern Windows version)
quser | Microsoft Docs
...in a PowerShell remoting script, like described here:
How To Log Off Windows Users Remotely With PowerShell
Again the work is being done by quser.exe, not PowerShell specifically. PowerShell is just being used to run quser.exe remotely. You could do the same, by copying PSExec to the remote host and do a similar operation.

PsExec -i parameter works only with console

I am using PsExec to run a process on a remote computer but I want also to use the -i parameter in order the process interact with the computer desktop.
For example:
PsExec.exe \\computer1 -i -h powershell.exe -noexit -command hostname
When I RDP to the machine, I cannot see the "GUI" if the powershell, but I do see a new PowerShell process run under processes.
But if I am using the console feature in my virtualization environment (as this is a VM), I can see the PowerShell process pops up.
Tried to search online a "Console" like feature when using RDP to a machine but it seems like the /console argument has been deprecated...
What else can I try?
I found the solution thanks to hawaii:
I just need to add the session id after the i parameter.

How to use Psexec execute a script on a remote machine without security warning

I am trying to execute a powershell script remotely using psexec like this:
.\psexec.exe \\remotecomputer -u domain\remoteuser -p password powershell.exe -c:\myscript.ps1
But the script does get executed remotely, but I see a 'security warning' when it get executed. On the remote machine, I already set 'Set-ExecutionPolicy bypass' in powershell. And when I executed the script 'c:\myscript.ps1 on that remote machine, I don't get any security warning. What can I do to get the powershell script executed remotely without security warning.

Powershell: Using psexec with UNC path gives "access is denied"

I am trying to run a powershell command like this:
psexec \\MachineB "\\MachineB\drops\Func2WebSiteOnline.bat"
I get this error:
PsExec could not start \\MachineB\drops\Func2WebSiteOnline.bat on
MachineB: Access is denied.
I have tried the following things:
run powershell in "Administrator" mode
Run the powershell command with "-u Domain\user -p password" params
Neither of those help. However, I can run simple commands against machineB like this:
psexec \\MachineB net stop dcache
and that works just fine.
Any thoughts on how I can run that batch file above against MachineB?
Thanks
I found a somewhat unconventional way to solve it.
psexec \\MachineB -u domain\user -p password cmd /c "cd /d
I:\drops\Func2 && func2web.bat"

Run PowerShell scripts on remote PC

I have installed PS 1.0 on a remote PC(RPC001). I used Windows Sysinternals
tool PSExec.exe to execute the following process on the remote:
PSExec \\RPC001 -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2
I can see the PowerShell.exe process running on the remote PC afterwards, but it is actually doing nothing, just hanging there. I tried to put a simple code of "Write-Output/Host" a string in the script. I run the same script on the remote by RTS, it works there.
Not sure if I miss anything else to run the script by using PSExec, or it is PSExec.exe limitation. I would like to start a PS script on remote to do something there locally (compress some files locally and remove old files) from my box.
I asked a similar question in Stackoverflow: Run remote process by powershell. Don suggested me to use PSExec. It sounds like an alternative way to solve the issue. However, I cannot get it working with PowerShell. Any way to get PS working on remote PC?
By the way, I cannot use PS 2.0 since my network does not allow me to install Windows XP SP3, which is required for PS 2.0.
The accepted answer didn't work for me but the following did:
>PsExec.exe \\<SERVER FQDN> -u <DOMAIN\USER> -p <PASSWORD> /accepteula cmd
/c "powershell -noninteractive -command gci c:\"
Example from here
After further investigating on PSExec tool, I think I got the answer. I need to add -i option to tell PSExec to launch process on remote in interactive mode:
PSExec \\RPC001 -i -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2
Without -i, powershell.exe is running on the remote in waiting mode. Interesting point is that if I run a simple bat (without PS in bat), it works fine. Maybe this is something special for PS case? Welcome comments and explanations.
Can you try the following?
psexec \\server cmd /c "echo . | powershell script.ps1"
Accepted answer doesn't work for me, but this does. Ensure script in the location (c:\temp_ below on each remote server. servers.txt contains a list of IP addresses (one per line).
psexec #servers.txt -u <username> cmd /c "powershell -noninteractive -file C:\temp\script.ps1"