I am trying to execute the following post build event code but I am getting an non-useful error :
"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"
I have run the following PS script before I try :
Set-ExecutionPolicy unrestricted
What am I missing?
UPDATE
This is strange, I am not getting an error on VS now. but the script is not working. When I run it with powershell console I get the following error :
Visual Studio writes the post-build event script to a .BAT file and executes that using cmd.exe. So using & "<path-to-powershell>" won't work. Just execute:
Powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
And if you think you're likely to run into execution policy issues on other machines that build the solution, consider going this route:
Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1"
You can reproduce the error in Powershell as follows:
"this is a string" -file "my.ps1"
It is taking the first as a string, the -file as the -f format flag and saying it doesn't have a value expression on the right for the format substitution.
Try like this:
& "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"
(as Keith notes, this will not work as this is run from a bat file than Powershell.)
Or just:
powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
Before calling power-shell script from visual studio, set the ExecutionPolicy to unrestricted from power-shell window like this...
Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: unrestricted;
the call power-shell script in the following manner...
powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)
then in the script, you can always read the parameter like this...
param([string]$SolutionDir,
[string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");
Related
I am provisioning a Windows Server 2012R2 with terraforms on Vsphere. I have the machine up and running. Now I want to run some .cmd files on the machine via remote_exec of terraforms.
The user is not the Administrator, but in the Administrator group. UAC disabled for the purpose of executing scripts via CLI or powershell.
I have written a runAsAdmin.ps1 which would call my install_software.ps1 which will have all the necessary commands to execute the .cmd files.
runAsAdmin.ps1
powershell -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file C:\scripts\install_software.ps1' -verb RunAs}"
install_software.ps1
cd C:\\gitrepo\\lip-core-devops\\terraform\\system-config\\ims\\scripts
echo "Installing IMS Base from powerscript"
Invoke-Item .\Install_IMS_Base.cmd
I can run execute the runAsAsAdmin.ps1 on a powershell directly on the machine without admin elevation and everything works as expected.
But when I am executing the runAsAdmin.ps1 from the remote-exec of terraforms, the execution is successful, but the contents of the install_software file are not executed. I tried creating a directory before executing the cmd file.
Terraform part
provisioner "remote-exec" {
inline = [
"powershell -File C:\\scripts\\runAsAdmin.ps1"
}
the connection is all set and successful. I get success at the end of the script execution.
What I am I missing in my scripts that it is not executed via terraforms but works on the machine.
This could be caused by powershell execution policy?, try running it using the -ExecutionPolicy setting. I have to do this when using VM extensions in Azure.
start powershell -ExecutionPolicy Unrestricted -File runAsAdmin.ps1
i create server with winrm, than i put some command in powershell
like
provisioner "remote-exec" {
inline = [
"powershell.exe sleep 3",
My server is Windows Server. I would like to replicate the Unix tail command in Windows Server.
Unix Server: tail -f test.txt
PowerShell: Get-Content test.txt
How to execute in Windows Server?
Below command is not working:
powershell -File "Get-Content test.txt"
Error message:
Unable to execute program 'powershell -File "\"Get-Content...
Any idea?
Get-Content is not a file; it is a cmdlet. The -file parameter to Powershell.exe instructs Powershell to read the file supplied and execute the commands in it, as a script.
You can pass commands directly to Powershell by using the -command parameter; the parameter can be a quoted string, which is interpreted as a Powershell command. You would therefore want to use
powershell -command "Get-Content test.txt"
in the simplest case.
Note that Powershell.exe must be in your system path; if it is not, you would need to supply the full path to powershell, e.g.,
C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -command "Get-Content text.txt"
This question is very similar - perhaps essentially identical - to Unix tail equivalent command in Windows Powershell; I would recommend reading that question and its answers as well.
Additionally, exploring the help for Get-Content will provide useful information.
Working fine after setting full path of powershell.exe and without any quotes
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content test.txt
Within a powershell window :
Get-Content test.txt
command returns :
hello world.
i'm inside test.txt.
bye.
So i have fairly easy powershell script that contains following:
import-module activedirectory
Get-ADUser -Filter *
remove-module activedirectory
If i run it from powershell it runs OK, but when i try to call it from CMD nothing happens, it just opens powershell and thats it. I am using following command to run it:
powershell.exe -file "D:\test.ps1"
I noticed also following thing, 2 powershell.exe processes run after i execute this. If i exit from CMD from one powershell then i start seeing lists that this PS query should be returning. Is there a way to get this working since i am trying to run ps script as scheduled job. The crucial part here is import module when i run it over cmd which is not happening for some reason.
It's powershell 2.0 running on Windows 2008R2. I tried this script on win 2012r2, works fine from CMD... Looks like ps 2.0 limitation?
Could be a couple of things going on here. Since your windows opens and closes you wont get to see any errors that might be occurring. What is your ExecutionPolicy set to? Get-ExecutionPolicy
When I make scheduled tasks of my scripts I usually set up my action as such
Program/Script = %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments = -ExecutionPolicy Unrestricted -NoProfile -File C:\data\script.ps1
Start In = %SystemRoot%\system32\WindowsPowerShell\v1.0
Also, I don't believe it matters in this case but be sure you have the script "Running with highest privilege" if required.
Is there a way to pass switch operators to the powershell prompt that is created to execute scripts. Essentially I have a state file which executes a powershell script:
function1:
cmd.script:
- source: salt://utils/scripts/function1.ps1
- shell: "powershell"
- env: "-ExecutionPolicy bypass"
But this doesn't work.
Because a dirty solution is to run the script via cmd:
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Bypass -File
Any way to set the executionpolicy flag in the state file itself?
Thanks!
try using a file.managed to deploy the sls that holds the .exe and then use and cmd.run instead of cmd.script for the execution
How can I get my PowerShell script to execute?
When I run my script I get the error:
File cannot be loaded because the execution of scripts is disabled on this system
Can you help me to get my script to run?
set-executionpolicy unrestricted
#Set-ExecutionPolicy RemoteSigned
$target=[IO.File]::ReadAllText(".\usermenuTest1.4d")
$output=[IO.File]::ReadAllText(".\usermenuTest2.4d")
($output -replace $target) | Set-Content "usermenuTest2.4d.new"
Start-Sleep -s 10
This Technet article suggests trying Get-ExecutionPolicy to check your script execution policy, and then set it to AllSigned or Unrestricted. Also, make sure you run the script like this ".\scriptname.ps1"