Run a powershell script in batch file as administrator - batch-file

I have a powershell script. To run this sript, I have written a batch file.
Here is code of batch file:
:: psscript.bat
set psscript='%CD%\Hotfix-Automation-Installer.ps1'
echo Running PowerShell Script: %psscript%
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&%psscript% %*
When I double-click on batch file, I want my script Hotfix-Automation-Installer.ps1 to be run as an administrator.
How I can run this script as administrator?

Read Matt's article on this link:
How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
He has given complete script to elevate from current session to administrator session. Quiet helpful.

Related

How do I execute cmd commands through a bat file?

I need to create a .bat to put together with my setup system to install a network driver, but I have some difficulties in creating the bat.
This .bat needs:
execute a cmd with administrator privileges
run this command: netcfg.exe -v -l networkbll_lwf.inf -c s -i nt_networkbll
exit
The folder for all files location is: c:\Windows\System\Drivers.
You might have to use another batch file first to launch the second with admin rights.
In the first use
runas /noprofile /user:mymachine\administrator batchfilename.bat
PAUSE
and write the needed command in another bat file

Run batch file as admin, hidden, from inno setup

I have an Inno Setup installation which successfully run a batch file when finished. The batch asks for admin rights, gets them and does things.
I've been asked to completely hide the prompt window, so I've added "Flags: runhidden" in Inno Setup and #echo off and >nul 2>nul in the batch. But the batch still flash a prompt window, because of this:
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & goto
batch & exit /b)
EXIT /b
:batch
---MyCodeHere---
I have not written this code by myself and I'm not expert about batch programming, but I'm pretty sure that these lines open a new prompt with admin permissions, and that's the prompt flashing.
Does anyone know if there's a solution to this? Maybe some flag to add to "powershell start", or another completely different way to do this task.
Thank you all in advance
Don't make the batch file ask for Administrator permissions.
Run the batch file with Administrator permissions straight away:
Run process (net.exe or sc.exe) with Administrator privileges at the end of an installation in Inno Setup
[Run]
Filename: "{app}\my.bat"; Description: "Run my batch file"; \
Flags: postinstall runascurrentuser runhidden

Run a batch script in another directory than the batch script directory

I have a batch script that I run from the Windows Task Scheduler, the advantage is that the scheduler has the option of "Start in:", this allows me to run the batch script in a totally separate directory.
In the cmd prompt the equivalent would be to open a cmd prompt window in Directory1 and type out the full Directory2/batchscript.bat.
However, I was wondering if there was a way inside my batchscript.bat to set the directory to "run" in?
Try something like this:
set "your_dir=path_to_your_directory"
pushd %cd%
cd %your_dir%
run_your_command
popd

SQL Server agent for scheduling SFTP using WinSCP under SSIS

I have a batch script which generates a WinSCP upload script to upload a file to an SFTP location. Now when I run the batch file via command prompt - it runs successfully and loads it. I called the same thru SSIS Execute process task - it runs successfully and loads it. Now when I put the same on SQL Agent - I tried the following two options:
Using Operating System (CmdExec) - cmd.exe /c "\.bat"
Added the SSIS package to SSISDB and added it as a job step.
With both the above options the job showed a successful run. However the file is not uploaded! Any ideas on what is happening?
Here's my batch script:
echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\\<staging path>\
SET scriptPath=\\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
#REM ftpFlags: -explicit
echo deleting uploadScript if it already exists
IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1
echo Generating WINSCP Upload Script
>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%#%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit
echo Launching WINSCP upload
start /wait %winscp% /console /script=%scriptPath%
As you start the WinSCP via the start (why?), the exit code is not propagated to the SSIS. So, you never learn, if the script fails. And it most probably fails.
You also should enable logging, so that you can see what's wrong.
You should use this code to propagate the WinSCP exit code to SSIS and to enable logging:
%winscp% /log=\\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%
(Note that the winscp.com does not have the /console parameter)
Anyway, one clear problem is that you do not specify an expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?
When testing the script, add the /ini=nul parameter to isolate the script from your configuration.
For this and other hints, when debugging WinSCP running under SSIS, see My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
And finally, see WinSCP SFTP Task for SSIS.
Your variable seems set incorrectly. To manage with a space in the path and into the variable you have to put in quotes the whole path or the whole variable.
i.e.
set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
or
set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
Another point, you have to check this file: UploadScript.txt because your script adds new lines rather than remake the file.
change this line to >%scriptPath% echo option batch abort instead of >>%...
Ah, I did not pay attention to the IF EXIST.

Create an .inf script to open a batch file as administrator

I`ve been looking around the web for a tutorial on how to make .inf scripts so I can execute a batch file from it. The problem is that the batch script needs admin to run so i have to get that in some way. Here is my code:
:start
echo off
copy program.jar "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\"
The program needs admin to copy to the Starup folder.
Thnx.
Only for the All User's folder. Only Admins can install for all users.
The per user startup folder is at %appdata%\Microsoft\Windows\Start Menu\Programs\Startup

Resources