Run individual command as another windows user within batch file - powershell

I have the following batch file (in Jenkins). I would like to run only "SqlPackage.exe" command as another windows user. I have the domain user id and password for it.
Batch file:
CD Folder_Name
DEL Another_Folder
SqlPackage.exe /Action:Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:Server1 /op:Publish.sql
Copy Publish.sql Somewhere_Folder
I understand I can run the batch file as another user using windows runas feature. However, in this case, I would like to only run an individual command (sqlpackage.exe).
Is it possible?

Related

i have a .ps1 file which fetch folder content from the folder.how to run that .ps1 file from jmeter so i can do performance testing?

I have a .ps1 file which fetch folder content from the folder. How to run that .ps1 file from jmeter so I can do performance testing?
PS C:\Users\######> D:\KANHA_####\niii1.ps1
Add OS Process Sampler to your Test Plan
Configure it as follows:
Command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Argument 1: -File
Argument 2: C:\Users######> D:\KANHA_####\niii1.ps1
That's it, when you run the test JMeter will execute the script and you will be able to see the output using i.e. View Results Tree listener.
More information: How to Run External Commands and Programs Locally and Remotely from JMeter article for more information.
P.S. You might need to amend execution policy settings

The system cannot find the file specified- Task Scheduler

I have batch script file, which should encrypt a file with pgp. I have defined a task in task scheduler to do this, but I am keep receiving the error"The system cannot find the file specified".
Interestingly, when I run the same line of script in my powershell , the encrypted file is successfully generated.
I was wondering if anyone knows what can possibly be wrong here?
I tried to give the full path in my batch script , and also added the pass in start in part, when defining the action.
the batch scrip code is here::
rem #echo off
#set path=c:\test;%path%
#set d=%date:~-4,4%%date:~4,2%%date:~-7,2%
#set d=%d: =_%
#set t=%time:~0,2%%time:~3,2%%time:~6,2% #set t=%t: =0%
Rem Generate PGP encrypted file
#echo Starting PGP... >> c:\apps\ftpLogs\test.log
gpg2 --batch --yes -r testkey --output c:\test\foo\test_20150505.pgp --encrypt c:\test\foo\test_20150505.txt >> c:\apps\ftpLogs\test.log
and the script that I ran in my powershell, which works fine, is this line:
gpg2 --batch --yes -r testkey --output c:\test\foo\test_20150505.pgp --encrypt c:\test\foo\test_20150505.txt >> c:\apps\ftpLogs\test.log
Finally I was able to resolve the issue. The problem was with the user authority. The batch script was suppose to encrypt a file and then ftp the encrypted file to the vendor's ftp server.
Apparently in Windows Server 2012 , the ADMINISTRATORS have the permission to create a file (here the encrypted file) while this user does not have the permission to send it. and SYSTEM user has the permission to send but not to create. (Both of them had this authority back in Win Server 2012).
So what did I do at the end, was to make two different batch script tasks and schedule them with 10 mins time distance. The first one was running the above code with ADMINISTRATOR privileges and the second one was sending it out with SYSTEMS.
If your batch file doesn't work, but the PowerShell script does, just run the PowerShell script from your scheduled task instead.
From the Scheduled Task, instead of entering the path to your batch file specify PowerShell.exe (or more likely C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe). Then in the Arguments box specify your parameters. You probably want to hide the window, so we'll include that in the arguments, and for this you can probably skip loading a profile, so we'll add that too. Then just use the -Command parameter to specify your code that you have in the question.
So your Program to run box should show:
C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe
Then the box that says "Add Arguments (optional)" should have:
-NoProfile -WindowStyle Hidden -command "gpg2 --batch --yes -r testkey --output c:\test\foo\test_$((get-date).ToString(yyyyMMdd)).pgp --encrypt c:\test\foo\test_$((get-date).ToString(yyyyMMdd)).txt >> c:\apps\ftpLogs\test.log"
On a side note, should that be gpg2 or pgp2? I just copied your command, but it seemed odd to me that they would name it like that.

TeamCity running Powershell script, but failing to execute a batch file

I am currently in the process of implementing a deployment method using Teamcity, which runs a Powershell script on my Build Agent, which then configures my Production environment etc.
I have a problem with the Powershell script though, in that it can't seem to run the batch file from it.
The script runs perfectly if I run it manually, it only fails when run via TeamCity.
In the build log I am getting the error:
'myBatchFile.bat' is not recognized as an internal or external command, operable program or batch file.
The batch file and the powershell script are in the same directory and the batch file is called as such:
cmd /c Deploy.bat
I have my TeamCity configuration set up to have the build step for this as:
Script: File
ScriptExecutionMode: Execute script with -File argument
Script Arguments: None
Additional CMD line params: None
I had originally not used the cmd to try to execute the batch file, but executing the batch file like .\Deploy.bat did not seem to work either.
Is there an additional thing I need to set up in order to get the batch file to run? The rest of the script runs fine, just the call to the batch that doesn't.
This is a bit of a wild stab as it's difficult to predict what's happening, but from the description it seems like the path is been altered in the script and it's also dynamic as TeamCity creates temp directories, but if you replace:
cmd /c Deploy.bat
with
cmd /c "$(Split-Path $myinvocation.MyCommand.Path)\Deploy.bat"
then I think this will be able to located the deploy script.
Let me know how it goes.

Copy a non exe File to a Remote Machine

I used PsExec to copy and run an exe file in a remote machine. I also want to copy a xml file to remote machine. I am able to do this way
PsExec.exe -d -c \\someserver c:\somefile.xml
The above command throws error saying system cannot find the file specified but adds the xml file to remote server.
Do u know any better way of copying files to remote server.
Is there any PsTool available for that?
Or an option in PsExec ?
Edit: (Answer)
I found out that using Powershell we can copy file to remote machines and it worked.
As you can read from psexec help
-c: Copy the specified program to the remote system for execution. If you omit this option the application must be in the system path on the
remote system.
So your xml file is copied on remote sys/USER:[domainname]username]tem and executed, this gives you the error.
If your xml is part of an application you have to run in remote computer, one solution is compress the app with all necessary files in a self-extracting EXE that runs main command when extracted.
If you just have to copy a file, why don't you use a simple script that maps remote folder and then copies file? Something like:
NET USE \\computername\sharename password /USER:[domainname\]username
xcopy .....
NET USE \\computername\sharename /DELETE
PsExec is not designed to copy files across machines. It can only copy the program it is going to run remotely.
If you have access to the remote machine, the copy could be done by running copy c:\somefile.xml \\\\remote-machine\Admin$ before running PsExec.
You can use this pattern with psexec to copy any extension ...
psexec -d -i 2 \PC Name -u domain\username -p password cmd /c copy
\server\location\filename c:\xx\xx\xx
PS: Refer to PSEXEC switches if you're unsure of what -d and i does. However "2" is a session id of remote desktop user that may change every time a new remote desktop session is created.
this helped me copy my exe file into c:\windows directory (one to one copy within same domain) :
PsExec.exe -d -c \\remoteserver -u administrator -p password c:\executable.exe

Run a exe file through a power shell script

I want to run a power shell script which can run a exe file and following are my requirements.
I have that exe file in a remote server location(//ES-WEBSRV01/DBMigration) which is shared to my local machine. Also I want to run that ps file through a cmd.exe.
You can simply call it like any other program:
\\ES-WEBSRV01\DBMigration\something.exe
or, if it contains spaces somewhere along the path:
& "\\ES-WEBSRV01\DBMigration\some thing.exe"
I have no clue what you mean by »Also I want to run that ps file through a cmd.exe.«, though. If you mean that you need a batch file and want to run the PowerShell script from there, then:
powershell -file myscript.ps1