Running cmd /c from PowerShell with spaces in filepath - powershell

I am trying to run the following command in PowerShell
PS C:\Users\Administrator> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat && nuget restore && msbuild mywebapp.sln /p:DeployOnBuild=true /p:PublishedProfile=ServerFolderProfile"
This produces the error
'C:\Program' is not recognized as an internal or external command.
My paths have spaces and I'm running several commands seperated by && which is messing everything up. I have tried putting quotes all over the place but I can't get it to work.
If i run just the first part of the command
cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat"
it works fine. But I can't get the other commands to work too.

According to the docs for cmd.exe:
If /C or /K is specified, then the remainder of the command line is processed as an immediate command in the new shell. Multiple commands separated by the command separator & or && are accepted if surrounded by quotes
So I just had to change my command to:
PS C:\Users\Administrator> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat" "&&" nuget restore "&&" msbuild mywebapp.sln /p:DeployOnBuild=true /p:PublishedProfile=ServerFolderProfile

Related

how to exec ps1 file from cmd?

I have a ps1 file, Test.ps1, which I need to exec from cmd. For test purposes this file only has 1 line:
write "ps1 test successful"
I was trying to exec this ps1 file from cmd. I googled and it seemed that including the following line might help:
Set-ExecutionPolicy RemoteSigned
write "ps1 test successful"
However I still can't exec this test. I've tried:
powershell Test
powershell Test.ps1
Test
Test.ps1
The cmd path context is set to the dir in which the ps1 script resides. Any idea what I might be doing wrong here?
Does this work?
Powershell -ExecutionPolicy Bypass -File .\Test.ps1
I've done this before with a .bat file, and this was the syntax used. In this instance, you're running from within the same directory as the powershell script (otherwise adjust the filename argument as necessary). And you may need to be running the CMD prompt as admin, if you aren't already.
Use
powershell.exe -ExecutionPolicy Bypass -File "C:\dir name\test.ps1"
Of course, replace C:\dir name\test.ps1 with the path and filename of the script you want to run, enclosed in " (double quotes).
Alternatively, start PowerShell in its own window, then run the script.
On macOS:
Use Homebrew to install Powershell:
brew install --cask powershell
Switch to Powershell:
pwsh
Execute the script:
./Test.ps1
My PowerShell script (Test.ps1):
echo "trying to test something"
I can execute it in cmd with this command:
.\Test.ps1
My output:
trying to test something

Execute PowerShell command from cmd - quotes issue

I have to execute two commands (including a PowerShell one) in new console window. Let me explain:
I am able to execute a PowerShell command from cmd in a new console window (I can specify the command inside quotes):
start /W cmd /K powershell -command "&{Write-Host 'Hello World!';}"
And I can execute two commands (I can write both commands in quotes):
start /W cmd /K "echo Hello && echo World"
But how can I do it together (execute two commands, one of them PowerShell)? I would like to do it without any .bat or .ps script.
I tried something like following (and some variations with escaping character), but I cannot deal with this quotes issue (there are quotes for joining two commands and inside quotes for PowerShell command).
start /W cmd /K "echo Hello && powershell -command "&{Write-Host 'Hello World!';}""
You need to escape & in powershell's ScriptBlock for cmd:
start /W cmd /K "echo Hello && powershell -command "^&{Write-Host 'Hello World!';}""

Running TFS commands with arguments in powershell

Hi I am trying to run the "tf get" command through powershell but i always get a unexpected token error when it reaches the arugments.
I was following the instructions from this post
TFS commands in PowerShell script
the line where the error is happening is
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe" #("get", $args[$i])
where $args[$i] is an argument being entered by the user, but the script stops executing after calling the tf.exe
Could someone help me out here? Thanks.
You can't execute a command in a string without using the call operator e.g. &. In PowerShell a string evaluates to a string e.g.:
C:\> "hello world"
hello world
You have to tell PowerShell that the string contains the name of a command using the call operator.
$tf = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe'
& $tf get $args[$i]
Note: when using & the string must contain just the name of the command. Arguments should be specified separately.

Running CMD command in PowerShell

I am having a bunch of issues with getting a PowerShell command to run. All it is doing is running a command that would be run in a CMD prompt window.
Here is the command:
"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe" PCNAME
I have tried the following with no success (I have tried many iterations of this to try and get one that works. Syntax is probably all screwed up):
$TEXT = $textbox.Text #$textbox is where the user enters the PC name.
$CMDCOMMAND = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"
Start-Process '"$CMDCOMMAND" $TEXT'
#iex -Command ('"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"' $TEXT)
The command will just open SCCM remote connection window to the computer the user specifies in the text box.
Try this:
& "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe" PCNAME
To PowerShell a string "..." is just a string and PowerShell evaluates it by echoing it to the screen. To get PowerShell to execute the command whose name is in a string, you use the call operator &.
To run or convert batch files externally from PowerShell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a PowerShell script, e.g. deletefolders.ps1.
Input the following into the script:
cmd.exe /c "rd /s /q C:\#TEMP\test1"
cmd.exe /c "rd /s /q C:\#TEMP\test2"
cmd.exe /c "rd /s /q C:\#TEMP\test3"
*Each command needs to be put on a new line calling cmd.exe again.
This script can now be signed and run from PowerShell outputting the commands to command prompt / cmd directly.
It is a much safer way than running batch files!
One solution would be to pipe your command from PowerShell to CMD. Running the following command will pipe the notepad.exe command over to CMD, which will then open the Notepad application.
PS C:\> "notepad.exe" | cmd
Once the command has run in CMD, you will be returned to a PowerShell prompt, and can continue running your PowerShell script.
Edits
CMD's Startup Message is Shown
As mklement0 points out, this method shows CMD's startup message. If you were to copy the output using the method above into another terminal, the startup message will be copied along with it.
For those who may need this info:
I figured out that you can pretty much run a command that's in your PATH from a PS script, and it should work.
Sometimes you may have to pre-launch this command with cmd.exe /c
Examples
Calling git from a PS script
I had to repackage a git client wrapped in Chocolatey (for those who may not know, it's a package manager for Windows) which massively uses PS scripts.
I found out that, once git is in the PATH, commands like
$ca_bundle = git config --get http.sslCAInfo
will store the location of git crt file in $ca_bundle variable.
Looking for an App
Another example that is a combination of the present SO post and this SO post is the use of where command
$java_exe = cmd.exe /c where java
will store the location of java.exe file in $java_exe variable.
You must use the Invoke-Command cmdlet to launch this external program. Normally it works without an effort.
If you need more than one command you should use the Invoke-Expression cmdlet with the -scriptblock option.

TeamCity Powershell Runner - Unable to run Source Code

Im trying to run some PS scripts using the Powershell Runner in TC and defining my own script as "Source Code" instead of a script file.
My script is as simple as:
"Hello World!"
Im running on Windows Server 2008 R2 and ive tried with to:
Run it as x86 + x64
Using "Execute .ps1 with '-File' argument" + "Put script into powershell stdin with "-Command -" arguments.
Ive set the security policy to Unrestricted in an attempt to get it to work, but no luck.
If I instead use a Command Line runner and for example writes:
powershell -Command Get-ExecutionPolicy
It works fine.
The errors im getting (depending on which of the 2 execution modes im using) are:
Starting: C:\...\cmd.exe /c C:\...\powershell.exe -NonInteractive -Command
- "<C:\...\powershell3889347351955805274.ps1" && exit /b %ERRORLEVEL%
in directory: C:\...\e18dda4054c166c7
'-' was specified with the -Command parameter; no other arguments to -Command are permitted.
OR
Starting: C:\...\cmd.exe /c C:\...\powershell.exe -NonInteractive -File
"C:\...\powershell8264270201473986040.ps1" && exit /b %ERRORLEVEL%
in directory: C:\...\e18dda4054c166c7
The term 'f' is not recognized as the name of a cmdlet, function, script file,
It looks to me like TC puts something in the actual script itself, but im not sure. Im stuck and I cant figure out what point im missing here :S.
Can anyone help?
I wasn't able to reproduce this, but I noticed something pretty weird with the command that TeamCity was trying to run:
-NonInteractive -Command - "<C:\...\powershell3889347351955805274.ps1"
I did not see it adding the quotes for me, so I thought maybe TeamCity is trying to quote a path with space(s) in it ( would have helped if you hadn't redacted your path)
So I switched my agent to a path with a space in it and I got the same command, and yes, the same error. So TeamCity is quoting the path wrongly. It is including the < in the quotes while it should have been <"c:\path with\space"
I will see if I can file a bug for this ( if there isn't one)
Try moving your agent to a non-space path as a workaround.