Run PowerShell scripts on remote PC - powershell

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"

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 copy script and run on remote computer

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?

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.

Need to unblock remote ps script run using psexec

As part of our automatic build and deploy using TFS, I need to execute a powershell script on a target server. The following is the (simplified for clarity) command run on the build server by the TFS Build Agent PreBuild step, in the (pre-build.ps1) script...
C:\Builds\<snip>\psexec.exe -accepteula -s -i \\WSRMO632WEB powershell.exe \\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1 -name AppPool-DEV -usr User -pw pass
If I run the powershell part of the command on the WSRMO632WEB box in a command window, I get the warning...
Security warning
Run only scripts that you trust. While scripts from the internet can be useful,
this script can potentially harm your computer. If you trust this script,
use the Unblock-File cmdlet to allow the script to run without this warning message.
Do you want to run \\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1?
[D] Do not run [R] Run once [S] Suspend [?] Help (default is "D"):
If I choose R, the script runs and performs correctly.
My problem is that I cannot get the syntax correct to incorporate the Unblock-File cmdlet.
I'm currently thinking that I'm going to have to use multiple psexec commands, one to copy the file from the build server, one to unblock it and a third to finally run it.
Surely it should be easier than that, but I can't find a suitable example and can't get the syntax right.
Any suggestions, please?
You can use Powershell's -command to first do an Unblock-File, then run it as a script.
C:\Builds\<snip>\psexec.exe -accepteula -s -i \\WSRMO632WEB powershell.exe "-command \"$file='\\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1'; $file; Unblock-File $file; & $file\"" -name AppPool-DEV -usr User -pw pass
Quotes are necessary so that the full command string will be passed to Powershell. Add backslashes to escape themselves as necessary.
UPDATE: You can also try feeding the required command into standard input.
echo r | C:\Builds\<snip>\psexec.exe -accepteula -s -i \\WSRMO632WEB powershell.exe \\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1 -name AppPool-DEV -usr User -pw pass
This way Powershell will run, get the "R" for "Run once" and run the script, without any changes to the script or calling command.

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"