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.
Related
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
I am looking for a Windows 7 equivalent of the "tail" command and thought I had found it with this Powershell equivalent -
C:\>powershell -command "& {Get-Content file.txt | Select-Object -last 100}"
If I use this in the CMD prompt on my own Windows 7 PC, returns the info just fine. I can even input/append it into another file.
However, when I log on remotely to another PC (via openSSH), the command works, but it never drops me back to a command prompt - just hangs after showing me the last 100 lines of the file. Which means this won't work for a batch file I'm trying to edit for about 300 remote Windows 7 PCs.
Any ideas?
After trying MANY different suggestions found all over online, FINALLY found one that worked!
And the answer is within the Batch file itself. My batch file to call this Powershell line was just this:
Powershell.exe -noprofile -executionpolicy Bypass C:\log\Tail.ps1
:end
Again, works great if you're using it on the very PC from which you want it to run/get the information. But not remotely. Finally found you just need to add "< nul" to the end of your call to Powershell in your batch file, just like this
Powershell.exe -noprofile -executionpolicy Bypass C:\log\Tail.ps1 <nul
:end
What the other person wrote is what finally made sense: "My research has shown that PowerShell runs the commands in the script indicated through the -File switch and then waits for additional PowerShell commands from the standard input (my brief experimentation with the -Command switch demonstrated similar behavior). By redirecting the standard input to nul, once PowerShell finishes executing the script and 'reads end-of-file' from the standard input, PowerShell exits."
Found here at this page - Powershell script gets stuck, doesn't exit when called from batch file
so credit actually goes to #Gordon Smith
Since your running the command with -command "...", according to the docs, you need to specify the -noexit flag to stop powershell from exiting after the command is run.
powershell -command "& {Get-Content file.txt | Select-Object -last 100}" -noexit
When you add this to a batch file you'll probably need -noprofile and -noninteractive as well - though for remote commands you might want to spawn a process for better control and error handling. Also, if this doesn't work the problem would probably be with how OpenSSH is handling something (this worked for me on a test-server with remote connect)
I have made a plugin for Microsoft System Center Virtual Machine Manager that executes a powershell script on a host machine through a powershell script called by the c# code of the plugin. (Shellception :P)
Since I allways got an error I decided to test it manually in SCVMM by right clicking on the host and entering powershell.exe or powershellfor executable and export-v -name [name] -path [path] -force - copystate -wait.
Now it tells me that there is no such file.
Strangely it works with cmd(.exe) and echo test.
Shouldn't powershell be installed on Windows Server 2012?
Also, if I remotecontroll the host, it works just fine in the console.
What am I missing?
I figured out that you need to provide the full path when using powershell.exe as executable. The issue is that not all hosts have the system variable PATH that includes the path to the powershell.exe executable.
You can run powershell.exe by providing the full path:
%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe
Or you can run cmd.exe as executable and then to run powershell.exe from this cmd:
executable: cmd.exe
parameters: /c powershell.exe echo 1; return 0;
I have a hard time trying to run PowerShell Scripts from the "Run Command" within Komodo Edit on Windows 7.
The command that I am using is:
powershell -File "%F"
When I run it, it does not return anything to the console, it just keeps running till I terminate it.
I have tested it, with the following simple script:
Write-Host "Hello World"
This is a known issue where powershell.exe waits for a STDIN prompt to return in certain cases, causing it to hang when no input is provided.
Solutions
Use -InputFormat None to indicate STDIN will not be used:
powershell.exe -InputFormat None -File "%F"
Forward null input from the outer command scope so that STDIN returns:
powershell.exe -File "%F" < NUL
Similar Questions
2010-11-09 Silently executing a PowerShell script from WiX Hangs PowerShell
2010-11-21 Running PowerShell from MSdeploy runcommand does not exit
2011-07-26 Exiting VMware vSphere PowerCLI command prompt?
I am using Notepad++ to edit a PowerShell file and want to be able to execute that file from inside Notepad++.
How can I set that up?
It took me a little fiddling, but I finally got this working. (I am using version 1.0 but this should work in other versions as well.)
Notepad++ can be set up to run commands, and assign shortcuts to those commands, as follows:
From the menu, click Run → Run
Add the command
C:\NotepadRun.bat "$(FULL_CURRENT_PATH)"
Save the command, giving it a name and a key shortcut.
Below are the contents of the batch file. I named mine NotepadRun.bat, but you can name it whatever.
#echo off
GOTO %~sx1
:.ps1
cd "%~d1%~p1"
powershell.exe .\%~n1%~sx1
GOTO end
:.rb
ruby "%~f1"
GOTO end
:.php
php "%~f1"
GOTO end
:end
pause
As a note upgrading to Windows7 and Powershell 2 I found some Issues with this and have updated to passing in an ExecutionPolicy to ensure I can run the script I am editing.
:.ps1
cd "%~d1%~p1"
powershell -ExecutionPolicy Unrestricted -File "%~n1%~sx1"
GOTO end
See Using Notepad++ to Compile and Run Java Programs and replace "javac" with "C:Windows\system32\WindowsPowerShell\v1.0\powershell.exe" (or your path to PowerShell). (Caveat: I'm not a Notepad++ user and haven't tried this.)
That said, I'd just use PowerShell ISE (installs with PowerShell) or one of the other dedicated PowerShell IDEs instead.
I would recommend using PowerShell ISE which comes as part of PowerShell and designed specifically for Powershell.
You can run a saved script from "Run" -> "Run" menu in Notepad++ with the following command:
powershell.exe -noexit -command . \"$(FULL_CURRENT_PATH)\"
Based on the answers before:
powershell.exe -ExecutionPolicy Unrestricted -NoLogo -File "$(FULL_CURRENT_PATH)"
You can also add the -NoExit parameter to keep PowerShell from closing automatically:
powershell.exe -ExecutionPolicy Unrestricted -NoExit -NoLogo -File "$(FULL_CURRENT_PATH)"
Note: File has to be saved.