Installshield - Running Powershell script from elevated command prompt - windows

I'm using InstallShield to create an MSI that runs a .cmd file which calls a powershell script. The powershell script changes 2 registry keys. One it is removing a DENY right on a registry key for SYSTEM and the other it's changing a key's value from 0 to 1.
The script runs fine when manually executed or when I simply run the .cmd file that's used within the .MSI.
When I run the .msi, however, the cmd file runs... the powershell script executes and the value gets changed from 0 to 1... however, the DENY is not removed. The msi is running an executable custom action which I named RunCMD it is set to run in "Deferred Execution in System Context" and set to run After PublishFeatures.
I am at a loss as to why running the .cmd in this way (should be elevated) is not executing the portion of the powershell script which alters the permission on the registry key but it does allow for the value change on the other key. The script definitely runs but it partially fails when run within the .msi.
Again, if I manually run the .cmd file myself the script runs without issue so it's not a problem with the script itself.

Related

Inno Setup : execute command/batch file after install process is complete

Friends, I'm writing a script which does a lot of stuff - install my application + other opensource software, set up s/w as service, execute batch files to do some stuff etc.
I set some environment variables at the start. They of course do not take effect till the Inno script finishes execution (i.e the installation finishes).
The environment variables are needed to install a service.
The commands to install, configure and start the service are in a batch file which I need to execute after installation is complete. I need to execute this batch file just once.
How do I execute this batch file after installation is complete, using Inno? It can be immediately after user finishes the install process or run once at system restart. Is it achievable through Inno script?
Or is there a way for my environment variables to take effect so that my batch file can run ?
I know there are installation programs that do part of installation after user has finished install process and system has restarted. Does Inno support that kind of functionality?
I'm not sure if CurStepChanged + ssPostInstall method will work here as it will essentially be the same environment that Inno began with and my variables are not yet in effect.
If this cannot be achieved through Inno script, I will place the batch file at system startup - in that case I will need to check if the service is already installed/running and take appropriate action. But that would be crude.
Please help with any pointers/suggestions.
Thanks.
To schedule a batch file to be run on the next Windows start/logon, add a value to HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce key:
[Registry]
Root: HKCU; Subkey: "Software\Microsoft\Windows\CurrentVersion\RunOnce"; \
ValueType: string; ValueName: "MyProg"; ValueData: """{app}\MyBatch.bat"""
A value in RunOnce key is automatically deleted by Windows, when the program/batch is run.
Read about Run and RunOnce Registry Keys:
By default, the value of a RunOnce key is deleted before the command line is run. You can prefix a RunOnce value name with an exclamation point (!) to defer deletion of the value until after the command runs. Without the exclamation point prefix, if the RunOnce operation fails the associated program will not be asked to run the next time you start the computer.
Execution order:
If more than one program is registered under any particular key, the order in which those programs run is indeterminate.
Actually the programs run in parallel. Some of the entries can easily be an application that runs until the end of the Windows session. So Windows cannot wait for the program to finish before starting another.
If you need to run the batch files in a particular order, create just one entry pointing to a master batch file that runs the other batch files in the order you need.

Batch file on network drive CALL fails to call another batch file - scheduled task

I have a batch file on a network drive (V:), named one.bat. Inside, it has a CALL to another batch file on the same network drive (V:) in a different folder. I have one.bat run by double clicking the file, it works fine. I setup a scheduled task (Windows 2012 R2) and if I have it set to 'Run only when user is logged in', it works fine, but if I switch to 'Run whether user is logged in or not' and run it, it fails to execute the CALL commands inside the one.bat file. I echo the %ERRORLEVEL% out to a text file and the error level after the call is a '1'.
Any ideas on why this would fail when switch the setting on the scheduled task? I have run this script without issue on other computers and Windows 2008, so I'm wondering if this is an environmental issue with the mapped drive or something different with 2012?
one.bat:
SET Drive=V:
SET WorkEnv=MYWORK
SET CommonCode_Path=%Drive%\%WorkEnv%\Common-Code
CALL %CommonCode_Path%\ParmSettingsSetup.bat
This call fails with a ERRORLEVEL of 1.

Task Scheduler fails to run batch file despite reporting task completed

I have a simple batch file which calls a powershell script.
I've looked at the following 3 previous questions on the subject as well:
Run a batch file from Task Scheduler is not working with a java command
Windows Task Scheduler doesn't start batch file task
Task Scheduler not executing batch (bat) file with MSTest commands
It seems like I've tried every single combination of running the task and it still doesn't execute my powershell script.
batch file contents:
powershell.exe "E:\SIS\fileCopy.ps1"
If I run the command in the batch file manually, it runs just fine. Here are things I've tried to do to get it working:
I've change the Security options to run as System with "Run with highest priveleges" checked
I've tried every other combination of "Run only when user is logged on", "Run whether user is logged on or not" and "Run with highest priveleges"
I've tried adding the "Start in (optional" setting to the folder where the files are located.
I'm at my wits end and can't believe Microsoft hasn't figured out a way to make this easier.
You need to have task scheduler execute Powershell.exe and have the arguments be the path to your .ps1 file.
To validate your script is running properly, you should set the Security options to 'Run only when user is logged on'. It will then pop the powershell dialog when it runs. I often also use start-transcript to view the results of scheduled poweshell scripts.
After you validate the script is running correctly, you can set the security options however best fits your situation.
Use the execution policy flag to flag that instance to unrestrisicted because your powershell settings may be blocking script execution.
powershell -executionpolicy unrestricted -Command "E:\SIS\fileCopy.ps1"
I found that Task Scheduler can't run a batch file if it lives in a folder that is being synced by OneDrive. I had to move the batch file to another folder to get Task Scheduler to be able to run it.

Using RUNAS to use the local Administrator account to run a second batch file with elevated privileges

I am trying to develop a simple script that can run a second script as the machine's local administrator. As the first script I am currently trying:
set name=%computername%
runas /noprofile /user:%name%\Administrator "cmd.exe /c %cd%bg2.2.bat"
When I run the script it never seems to run the second script, which is located in the same directory. The second script is only for copying files.
del "C:\Windows\obh_logo.bmp"
copy "%cd%obh_logo.bmp C:\Windows\obh_logo.bmp"
When the first script runs I am prompted for the admin password as expected and once I enter it, a second command prompt seems to flicker on the screen then vanishes. It never appears to run the second script. What seems to be the reason for this not working?
%cd%will probably not point where you think it does, (just tested it, of course the variable gets replaced before the command gets called and does actually point to the first scripts dir, but the problem still persists in the second script)
the runaschanges the working directory for the cmd.exe instance you are starting, probably to C:\Windows\system32.
replace it with %~dp0, which yields the drive and path of the current batch file.
But! Your script will not run the second file elevated, just under the Administrator account, which is not the same. Manual confirmation is always necessary, see here for possible ways to request elevation from within your script.
I now assume the missing rights is the actual problem, add a pause statement in your second script to watch it, or call it with cmd /k so the console doesn't close.

batch file commands in powershell execute in a different command prompt

I'm using this new machine, so as usual I go and set the execution policy so that I can use my profile script, after doing that however powershell now opens all batch files in a new cmd.exe window.
I tried undoing this step but it's still the same so I think it has nothing to do with the script execution policy, also I still have the powershell window in which I originally set the execution policy and this one behaves normally, only new windows have this problem.
I may have installed some software, but nothing is related to windows, and I tried setting the PATH variable to its exact value in the working window but it does not work.
Batch files will open in a new window if the PATHEXT environment variable does not contain '.BAT' as one of the executable extensions.
To check the variable, enter the following at the PowerShell prompt: $env:PATHEXT

Resources