Windows SDK environment and powershell - windows

The windows v7.1 SDK has a SetEnv.Cmd script in its binary folder to correctly setup its environment. The problem is this script obviously only works with cmd.exe and I can't find an equivalent for powershell anywhere.
So am I forced to use cmd.exe or is there a way to use powershell (apart from manually rewriting the SetEnv.Cmd script - if even that would work?).

Someone wrote a ps1 script that parses out the SDK's setenv so you can avoid the extra processes:
http://www.tavaresstudios.com/Blog/post/The-last-vsvars32ps1-Ill-ever-need.aspx

You can launch an instance of CMD.EXE, run SetEnv.cmd, and then launch PowerShell from within the CMD window. The PowerShell instance that opens up will import the environment variables from the CMD instance that spawned it.
It's ugly, but it works.
I wrote a batch script to do it for me, and just created a shortcut to it on my desktop. Here's an example:
#echo off
call "C:\Path\To\SetEnv.cmd" param1 param2 param3
powershell.exe -noexit

Related

How to start a program from an existing cmd.exe with the current user environment; as if the user would start the program from the start menu

There are a lot of questions already asking similar things but none helped me:
Start new cmd.exe and NOT inherit environment?
Is there a command to refresh environment variables from the command prompt in Windows?
What i'm f.e. after is:
Create a batch- oder powershell script which
installs python to a system where python isn't installed (this works)
after a successful installation, run python with a python script as argument
(It should also work the same way for installing and using other apps like MSVC, git for windows, ...)
After installing python from a console, the path which has been added to the system environment variables isn't reflected in the current cmd instance environment.
But it's there when i start a new cmd or elevated cmd manually from the windows start menu and not from a script.
I tried the following without luck to update the environment of the existing console or create a new instance with the new user environment from script:
start /i/b&exit
start /i/b "%windir%\explorer.exe" "%windir%\system32\cmd.exe"
powershell -Command Start-Process -UseNewEnvironment -Wait -FilePath python -ArgumentList "test.py"
But for UseNewEnvironment it's documented that
the new process starts only containing the default environment
variables defined for the Machine scope. This has the side effect that
the $env:USERNAME is set to SYSTEM. None of the variables from the
User scope are included
and for start /i that
Ignore any changes to the current environment, typically made with SET. Use the original environment passed to cmd.exe
I think both approaches cannot be used for what i'm after.
Is there a simple possibility to automatically run a script in the new environment which the installer created without extensive code or third party binaries?

Start Command in CMD Window

I use below command in CMD window:
start notepad.exe
Is start a short-cut for an executable in Windows like:
C:\Windows\System32\start.exe
or is it a parameter to be passed to cmd.exe?
When I use
wcsript test.vbs
wcsript refers to
C:\Windows\System32\wcsript .exe
So I want to ask does same apply for start command?
Some commands available in CMD are built directly into CMD.EXE. I believe start, like dir, is one of those commands. You won't find a separate program called dir.exe or start.exe.
You do not need to use start, simply the location of your script with the filename+extension works.
The reason you can start notepad is because it is inside the Windows folder where CMD also is.
If it's on your desktop use: C:\Users\%Username%\Desktop\test.vbs
If you have multiple files you can make a script using:
cd C:\Users\%Username%\Desktop\
test1.vbs
test2.vbs

Difference between running a command from cmd.exe or Windows Run

I'm actually trying to run an application as another user and while it works really nicely in a cmd.exe prompt, it doesn't always work if I go with the Windows Run prompt (it actually depends on what application I'm trying to run).
For example, this works fine both from cmd.exe or from W-Run prompt (using either Windows XP or Windows 7):
runas /user:ME regedit.exe
While this only works in a cmd.exe prompt (it does ask for my password in both cases but it does nothing after that if launched from W-Run on either WinXP or W7):
runas /user:ME services.msc
It's actually kind of inconsistent, with cmd it always works but with Windows Run, it's really unreliable and random.
Any ideas where there is such a difference? To get around the problem, I'm actually using batch files to launch applications as another user and then just type the batch file full path in Windows Run prompt. It does ensure reliability but I still would like to know if I'm doing something wrong.
cmd /k "runas /user:ME ""regedit.exe"" && exit"
The "problem" with runas are
It needs all the command as only one argument, so if you are running something with arguments you have to enclose all the command in quotes, and if the command includes its own quotes, they need to be escaped.
It is designed to call .exe files (well, windows valid executable files).
This two options should handle your program start
runas /user:ME "cmd.exe /c \"start services.msc\""
runas /user:ME "mmc.exe %systemroot%\system32\services.msc"
In the first case, it is using the ability of cmd.exe to find the adecuated executable to run the .msc file. In the second case, it directly calls the adecuated executable to handle the .msc file.
For your batch files, instead of cmd /k .... & exit, you can directly use cmd /c ... that will close the console when the command finishes.

How to run a PowerShell script

How do I run a PowerShell script?
I have a script named myscript.ps1
I have all the necessary frameworks installed
I set that execution policy thing
I have followed the instructions on this MSDN help page
and am trying to run it like so:
powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit)
which returns exactly nothing, except that the file name is output.
No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.
The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.
What am I doing wrong?
Prerequisites:
You need to be able to run PowerShell as an administrator
You need to set your PowerShell execution policy to a permissive value or be able to bypass it
Steps:
Launch Windows PowerShell as an Administrator, and wait for the PS> prompt to appear
Navigate within PowerShell to the directory where the script lives:
PS> cd C:\my_path\yada_yada\ (enter)
Execute the script:
PS> .\run_import_script.ps1 (enter)
Or: you can run the PowerShell script from the Command Prompt (cmd.exe) like this:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
according to Invoking a PowerShell script from cmd.exe (or Start | Run) by Kirk Munro.
Or you could even run your PowerShell script asynchronously from your C# application.
If you are on PowerShell 2.0, use PowerShell.exe's -File parameter to invoke a script from another environment, like cmd.exe. For example:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
If you want to run a script without modifying the default script execution policy, you can use the bypass switch when launching Windows PowerShell.
powershell [-noexit] -executionpolicy bypass -File <Filename>
Type:
powershell -executionpolicy bypass -File .\Test.ps1
NOTE: Here Test.ps1 is the PowerShell script.
I've had the same problem, and I tried and tried... Finally I used:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
And put this line in a batch-file, and this works.
If you only have PowerShell 1.0, this seems to do the trick well enough:
powershell -command - < c:\mypath\myscript.ps1
It pipes the script file to the PowerShell command line.
Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.
Open PowerShell in administrator mode
Run: set-executionpolicy unrestricted
Open a regular PowerShell window and run your script.
I found this solution following the link that was given as part of the error message: About Execution Policies
Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to security risks.
Using cmd (BAT) file:
#echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If you need run as administrator:
Make a shortcut pointed to the command prompt (I named it
Administrative Command Prompt)
Open the shortcut's properties and go to the Compatibility tab
Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
An easy way is to use PowerShell ISE, open script, run and invoke your script, function...
In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:
Create a task
Set Program/Script to Powershell.exe
Set Arguments to -File "C:\xxx.ps1"
It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler?.
If your script is named with the .ps1 extension and you're in a PowerShell window, you just run ./myscript.ps1 (assuming the file is in your working directory).
This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.
Give the path of the script, that is, path setting by cmd:
$> . c:\program file\prog.ps1
Run the entry point function of PowerShell:
For example, $> add or entry_func or main
You can run from cmd like this:
type "script_path" | powershell.exe -c -
Use the -File parameter in front of the filename. The quotes make PowerShell think it is a string of commands.
I've just found the method what Microsoft do when we right click on a ps1 script and click on "Run with PowerShell" :
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell
C:\my_path\yada_yada\run_import_script.ps1
That does not do so well with arguments. The real answer to your question is that you are missing the & to say "execute this"
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'

Why is it that Cygwin can run .bat scripts?

When I execute a .bat script from bash in Cygwin, by what mechanism is it running? I understand that if I run a .EXE it will launch, regardless of whether the .EXE is from Cygwin or from a more traditional environment. I understand that when I execute an executable script with #! at the beginning that Cygwin supplies the magic for it to run.
But why does a .bat script work? Is there some component inside of Cygwin that is aware of what a Windows .bat script is and what to do with it? Or is it that it is somehow impossible under Windows to execute a call to launch a .EXE file that won't automatically also work for a .bat script instead?
Running
./test.bat params
from bash seems to be equivalent to
cmd /c test.bat params
I believe that bash in cygwin sees the bat extension as being flagged executable (a cygwin hit-tip to windows convention). As such it loads and executes the file with it's associated interpreter (cmd.exe, per os configuration), much as it creates a new instance of bash to run your #! scripts (per posix standard).
And if you want to fork an *.cmd file execution like a ShellScript process and append his log to an file:
cmd /c test.bat > nohup.out &
Enjoy!

Resources