How to run exe as a service with elevated credentials on remote PC with psexec? - psexec

I am using the command below (and tried many variations) to run an exe as a different domain user on a remote pc as a service (phew!).
psexec \\RemotePC -u admin -p mypass "SC create MyService displayname=
"MyService" binpath= "C:\ProgramData\Microsoft\Windows\Start
Menu\Programs\Startup\MyService.exe" start= auto obj=
corp\service_account password= servicepass"
I get:
PsExec could not start sc create MyService displayname=MyService on
RemotePC: The system cannot find the file specified.
I think I'm probably getting some spaces or quotation marks in the wrong place, but this is driving me nuts. Have tried all kinds of variations, including launching cmd.exe and passing the sc command in as an argument. Same issue.
Both Windows 7 PC's in a domain environment. Attempting to send local administrative user credentials with psexec command, and attempting to execute the sc command as a domain-level user.
EDIT:
Tried this:
c:\SysInternals>psexec \\RemotePC -h -u localadmin -p mypass "sc
create MyService displayname= \"MyService Name\" binpath=
\"C:\ProgramData\Microsoft\Windows\Start Menu
\Programs\Startup\MyService.exe\" start= auto obj= corp\service_account
password= domainpass"
and got:
PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark
Russinovich Sysinternals - www.sysinternals.com
Starting sc create MyService displayname= "MyService Name" binpath=
"C:\ProgramData\Microsoft\Windows\Start
Menu\Programs\Startup\MyService.exe" start= auto obj= corp\service_acc
PsExec could not start on RemotePC: The filename,
directory name, or volume label syntax is incorrect.
c:\SysInternals>

Loving the downvote with no comments or reason...
I solved this myself by simply removing all the quotes for anyone else having this issue.
c:\SysInternals>psexec \\RemotePC -h -u localadmin -p localpass sc create MyService displayname= MyService binpath= C:\Temp\Script\MyService.exe start= auto obj= corp\domainuser passw
ord= domainpass

Related

Cannot run pg_basebackup in powershell

We are trying to run a pg_basebackup script on Windows in Powershell. The command seems to work in the windows command line (cmd), but not in powershell on some machines. We are running Powershell as admin. Has anyone run into this?
The following is the command we are running:
pg_basebackup --host "localhost" --port "5432" --username="test" --password="testpassword" --progress --verbose -D "C:\test\master\base_backups\test"
Try to use this
# Put here your pg_basebackup file location
Set-Location "C:\PostgreSQL\9.6\bin"
# Put Here your password
$env:PGPASSWORD = "testpassword"
[string]$job = .\pg_basebackup --% -U test --progress --verbose -D "C:\test\master\base_backups\test"
The result will be in $job variable.

VBS opens a CMD but can't read next commands to go on other directory

I am trying to create vbs file so that it will be executed by the MS Scheduler in order to backup my Postgresql database because i can't seem to find a way in using agent since i installed it but it is not appearing in the UI of PGAdmin III that was installed when i used Odoo. I am using windows 10.
Here is my .vbs file
Set ShellCmd = WScript.CreateObject("WScript.Shell")
ShellCmd.run "cmd cd\ cd 'Program Files' cd Odoo 9.0-20161004 cd PostgreSQL cd bin pg_dump -h localhost -p 5432 -U openpg -f 'C:/BackupFiles/Sample.backup' -d 120120161800"
WScript.Echo "Success!"
When i run this, the command line isn't going anywhere. Even just "cmd cd\" the cmd does nothing. I'ts a quite simple problem but i can't get my automated backup working if i can't run a simple vbs command
Instead of using a vbs script, you should just use a .bat or .cmd batch file:
c:
cd "\Program Files\Odoo 9.0-20161004\PostgreSQL\bin"
pg_dump -h localhost -p 5432 -U openpg -f 'C:/BackupFiles/Sample.backup' -d 120120161800"
Generally, we do not use ShellCmd.run "cmd ...." in vbs to execute multiple commands in one statement.
Your ShellCmd is heavily broken. I suggest instead of using .Run take the .Exec method to catch the output - at least until problems are solved. I don't know if your pg_dump requires to be run inside that same folder, if not this might work:
Dim WshShell, oExec, s
Set WshShell = CreateObject("WScript.Shell")
Program = """C:\Program Files\Odoo 9.0-20161004\PostgreSQL\bin\pg_dump"""
Options = " -h localhost -p 5432 -U openpg -f 'C:/BackupFiles/Sample.backup' -d 120120161800"
Set oExec = WshShell.Exec(Program & Options)
Do While Not oExec.StdOut.AtEndOfStream
s = s & oExec.StdOut.ReadLine() & vbCrLf
Loop
WScript.Echo "oExec.Status=" & oExec.Status
Wscript.Echo s

pgAgent scheduled job failed on Windows

I am trying to set up the step with Batch file path on particular time in pgAgent via pgAdmin. But when I run that it is failing and in Step statistics I got this Output
C:\Windows\system32>C:\postgresql\run.bat
'psql' is not recognized as an internal or external command,
operable program or batch file.
Details:
Postgresql 9.3.5 on local system account (Current User)
pgAdmin 1.18.1
pgAgent via stack builder with Administrator account (Current User)
in run.bat I have only two statement
#echo off
psql -h localhost -p 5433 -U postgres -d test -a -f "test.sql"
I have psql in system path variable and able to access it in cmd. When I run that bat file manually it is executing without fail. But when I given the batch file path (C:\postgresql\run.bat) in pgAgent jobs it is giving that error in statistics.
Is there anything wrong in my configuration? Why it is always going to that C:\Windows\system32>?
Edit:
My run.bat file
#ECHO OFF
SET LBSDatabaseName=Test
SET dbHost=localhost
SET dbPort=5434
SET dbUser=postgres
SET logFile=DbInstall.log
SET sqlFolder="D:\SOURCECODE\archivescripts"
"C:\Program Files (x86)\PostgreSQL\9.3\bin\psql.exe" -h "%dbHost%" -p "%dbPort%" -d "%LBSDatabaseName%" -U "%dbUser%" -L "%logFile%" -q -f "%sqlFolder%\Archive.sql"
My Archive.sql
update "Archive".emp set "FirstName"='Srikanth Dyapa';
For example,
D:\pgAgent_jobs
is the path where psql located.
D:\pgAgent_jobs\scripts\test.sql
is the path in which my test.sql placed.
D:\pgAgent_jobs\scripts\psqlss.bat
is my bat file to execute test.sql
so my bat file will be like below
#echo off
cd /D D:\\pgAgent_jobs
psql -h localhost -p 5432 -U postgres -d db_name -a -f "D:\pgAgent_jobs\scripts\test.sql"
Note : my pg_hba.conf is configured with trust for all hosts that's why am not passing any password in the above psql command

Installation of postgresql with NSIS

I would install postgresql with the NSIS installer, but I don't know how.
Can you help me please ?
I've find this code
outfile "C:\project-open\installer\install_postgres.exe"
Name "Install PostgreSQL"
!include Registry.nsh
!include LogicLib.nsh
!include MultiUser.nsh
!include Sections.nsh
!include MUI2.nsh`
!define TARGET c:\project-open
Function .onInit
StrCpy $INSTDIR "c:\project-open"
FunctionEnd
section
UserMgr::CreateAccountEx "postgres" "*******" "PostgreSQL" "PostgreSQL Database User" "Database user created by ]po[ installer" "UF_PASSWD_NOTREQD|UF_DONT_EXPIRE_PASSWD"
pop $R0
DetailPrint "After creating account: result=$R0"`
UserMgr::AddPrivilege "postgres" "SeBatchLogonRight"
pop $R0
DetailPrint "SeBatchLogonRight: result=$R0"
UserMgr::AddPrivilege "postgres" "SeServiceLogonRight"
pop $R0
DetailPrint "SeServiceLogonRight: result=$R0"
nsExec::ExecToLog '"$INSTDIR\pgsql\bin\initdb.exe" --username=postgres --locale=C --encoding=UTF8 -D "$INSTDIR\pgsql\data"'
pop $R0
DetailPrint "After initializing database: result=$R0"
nsExec::ExecToLog 'sc create postgresql-9.2 binpath= "c:\project-open\pgsql\bin\pg_ctl.exe runservice -N postgresql-9.2 -D c:/project-open/pgsql/data -w" DisplayName= "PostgreSQL 9.2" start= "demand" type= own obj= ".\postgres" password= "******" '
pop $R0
DetailPrint "After registering the service: result=$R0"sectionEnd`
but I have a problem with UserMgr::CreateAccountEx but I've imported all files required.
All you need to do is create a data dir:
initdb -D %PROGRAMDATA%\MyApp\data
then install your PostgreSQL config file and pg_hba.conf or make any required edits to the files generated automatically by initdb at %PROGRAMDATA%\MyApp\data\postgresql.conf and %PROGRAMDATA%\MyApp\data\pg_hba.conf.
Then:
pg_ctl register -D %PROGRAMDATA%\MyApp\data -S auto -N postgres-MyApp -U NETWORKSERVICE
net start postgres-MyApp
Please do not use the default port 5432. Run on a non-default port that won't conflict with any existing or future PostgreSQL install. Also, do not use the "standard" service names like postgresql-9.2.
(NSIS may offer a command to start a service, instead of using net start. If it does, use the appropriate NSIS command).
Note that %PROGRAMDATA% is only defined for Windows Vista and higher (where it points to %SYSTEMDRIVE%\ProgramData by default). You can use %ALLUSERSPROFILE% on Windows XP, but really, who cares about XP now?
Personally, I suggest using MSI installers with WiX.
For those who looking for the very minimal Postgresql 13 installation package, it's following folders only:
bin
lib
share
It's not taking to account any special cases. Each individual setup have to be tested, in my case above working fine.

How to execute Powerpoint on a remote computer using psexec

I have a remote computer I'd like to launch a powerpoint presentation remotely using psexec. I would like the powerpoint presentation to launch on the REMOTE computer.
I'm trying to execute the following command and it says "invalid file name" I have verified the file exists
psexec -u domain\user -p password \remotepc -h C:\Test\Test.pptx
Try:
psexec \\remotepc -u domain\user -p password -h -i C:\Test\Test.pptx
Notice you have 2! slashes before the remotepc name.
And I added the -i option.