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

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"

Related

PsExec.exe throws error when running Powershell script in SYSTEM context - Error creating key file on {machineName}: Access is denied

I am trying to run a powershell script in the System context using PsExec, but get the error
C:\Users\{myUserName}>PsExec.exe -i -s "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
PsExec v2.34 - Execute processes remotely
Copyright (C) 2001-2021 Mark Russinovich
Sysinternals - www.sysinternals.com
Error creating key file on {MachineName}:
Access is denied.
This is simply to open up a PowerShell terminal with the system account. My end game is to use PsExec to create a PBK file like this
psexec.exe -i -s powershell.exe .\convertXML.ps1 -xmlFilePath .\vpntemplate.xml -ProfileName "TestVPN"
I am using the resources referenced here Configure VPN device Tunnels in WIndows 10
#zett42 when I opened a command window as an administrator and tried your command, it worked as I expected.
psexec.exe -i -s powershell.exe -noprofile
Alternatively, I think it was psexec's EULA that was causing problems because I ran this and it worked flawlessly as well
psexec.exe -i -s powershell.exe -nobanner -accepteula

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?

How to chain\pipe commands onto remote PS-Session and automate manual entries

I need to chain or pipe commands onto remote PS Session on servers. How can I pass arguments or commands to power shell remote sessions and automate the process of feeding command line arguments ?
If I log into a server locally and open a command prompt and type the commands\parameters it works fine. This is for windows 2012 server.
Open a cmd prompt.
1.) cd "D:\Oracle\Servers\AppSrv01\bin" //Type this in cmd prompt
2.) oraadmin.bat -user myuser -pwd mypwd //Type this in cmd prompt
oraadmin>oraadminctrl.invoke('clearlogs') //Type this in cmd prompt
oraadmin>oraadminctrl.invoke('stopserver') //Type this in cmd prompt
oraadmin> exit //This will exit the oraadmin in cmd prompt
3.)exit //Type this in cmd prompt, exits the cmd prompt
This works fine. Now I am trying to automate this in powershell by opening a session ($sess) object as below.
Tried this. But failed 'oraadminctrl.invoke' is not recognized as an internal or external command
$c = Invoke-Command -Session $sess {cmd.exe /c “cd D:\Oracle\Servers\AppSrv01\bin && oraadmin -user admin -password mypassword && oraadminctrl.invoke('clearlogs') && oraadminctrl.invoke('stopserver')" }
I just want to automate the process of feeding this username and password and running these commands one after other on the same command prompt process on the remote machine. I am also open to start-process or other forms of invoke-command. Writing a new batch file to be put on the server is not acceptable. Any idea how to go about this ?

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.

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"