Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 months ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I installed Chocolatey as per the instructions on the website (https://chocolatey.org/install).
The 'choco' command works fine when I run it normally on cmd but returns the following error when run as administrator:
C:\WINDOWS\system32>choco install -y wget 7zip.commandline
'choco' is not recognized as an internal or external command,
operable program or batch file.
The install choco install -y wget 7zip.commandline fails if not run as administrator.
How do I fix 'not recognized' error in admin cmd?
First, ensure that you are using an administrative shell of command prompt(https://www.howtogeek.com/194041/how-to-open-the-command-prompt-as-administrator-in-windows-8.1/).
Copy the below text into the command prompt.
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Then press enter key from the keyboard. After few seconds you get complete info about current installation.
If you don't see any errors. Type choco or choco -? now.
Reference
The choco environment variable has been altered. I ran into a similar problem when I tried setting env variable for Java SDK, but I was quick to recognize it when I ran the commands presented to me in the documentation which completely overwrites my env Path variable.
If you are sure you've choco installed. Check your System variables and look for Path variable if it has some values like C:\ProgramData\chocolatey\choco.exe;C:\ProgramData\chocolatey\bin if not add it.
And you can save you time by installing it. Installing via cmd line
Powershell:
Copy and Paste the code below to your PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
OR Window CMD.exe:
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
After installation: run choco you see the version of chocolatey installed. Mine
Chocolatey v0.10.14
Please run 'choco -?' or 'choco <command> -?' for help menu.
hopefully this help read more
First Open the Command prompt as administrator and then run -
#powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
And then restart the Command prompt, now choco is successfully installed
Open command prompt in administrator mode and run this command:
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command " [System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Just correcting what #Krishnarjun Banoth has given in the answer:
powershell -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
You don't have to add the PATH variable because choco will automatically add itself to PATH.
Changing the Path entry from 'C:\ProgramData\chocolatey\bin' to 'C:\ProgramData\chocolatey' and rebooting Windows did the trick for me.
Run Powershell as administrator
Run Get-ExecutionPolicy. If it returns Restricted, then run Set-ExecutionPolicy AllSigned or Set-ExecutionPolicy Bypass -Scope Process.
Now run the following command:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
I was able to get the installation to work using C:\ProgramData\chocolatey\bin\choco.exe install -y wget 7zip.commandline. I'm still wondering why the choco command isn't working when cmd is run as administrator. I would appreciate any help. Cheers!
Hi for installing choco on your windows system, follow these steps:
First open cmd as admistrator.
copy this command in the cmd (as administrator):
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
-NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
&& SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
This will download the required dependency for choco and install it in your windows system.
For cross check run the choco command, for ex:
choco install -y nodejs.install python2 jdk8
To install chocolatey yarn:
First of all
Download the installer
This will give you a .msi file that when run will walk you through installing Yarn on Windows.
If you use the installer you will first need to install Node.js.
https://classic.yarnpkg.com/lang/en/docs/install/#windows-stable
And, then go for choco install
Powershell:
Copy and Paste the code below to your PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
OR Window CMD.exe:
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
After installation: run choco you see the version of chocolatey installed.
choco -v
After that just do one more thing
in PowerShell or cmd just copy-paste the following command to install chocolatey yarn in your system
choco install yarn
Here your yarn has been installed, check your yarn version using the following command.
yarn -v
That's it!! :)
This will definitely work :)
Related
Recently I've been investing some time to dive from Docker on Linux with bash (usually using "eux" options) into Windows container with Powershell to gain the same experience in Windows.
However, I have a lot of trouble getting there. Let's begin with the troubling Dockerfile (which will likely be the base for most Windows containers using PowerShell):
FROM mcr.microsoft.com/windows/servercore:20H2-amd64
SHELL ["powershell.exe", "-NoLogo", "-NoProfile"]
# Set ExecutionPolicy = Bypass permanently
RUN Invoke-Command -ErrorAction Stop -ScriptBlock { `
Get-ExecutionPolicy -List ; `
Set-ExecutionPolicy -ExecutionPolicy Bypass ; `
Get-ExecutionPolicy -List `
}
# Install chocolatey
RUN Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# Enable global confirmation and install general build tools
RUN Invoke-Command -ErrorAction Stop -ScriptBlock { `
$ErrorActionPreference='Stop' ; `
choco feature enable -n=allowGlobalConfirmation ; `
choco install --no-progress --no-color -y cmake --installargs 'ADD_CMAKE_TO_PATH=System' ; `
choco install --no-progress --no-color -y mingw ; `
choco install --no-progress --no-color -y make `
}
What I personally miss the most is the possibility to chain multiple commands, so I can avoid the creation of superfluous image layers and container restarts.
However, if one of the commands fails, the command won't stop even that I told Invoke-Command to stop on errors and within the script block I set the ErrorActionPreference variable.
Hope the main problem here is not mainly linked to the missing knowledge in PowerShell, but how PowerShell works in Windows containers.
Thanks!
In Powershell-
How do I run a powershell script(.ps1) inside a bash script(.sh)
I can run just the powershell script-
& .\scriptfile.ps1
and just the bash script.
But when I try to run the powershell inside the bash script, I get
file.ps1 : command not found
Both scripts are in the same path.
Are you trying
.\scriptfile.ps1
?
That should be
./scriptfile.ps1
But also, when invoking powershell from a bash script, you'll need to either run the pwsh command like
pwsh ./scriptfile.ps1
or the first line of your Powershell script file should be a shebang (interpreter directive) like:
#!/usr/bin/env pwsh
See How can I use a shebang in a PowerShell script?
I'm using git bash on Windows 10 Pro and there it's powershell.
The executable is in /c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe and it's contained in my $PATH.
So I can say
powershell ./scriptfile.ps1
# or inline:
powershell 'my script'
In the Ubuntu-on-Windows bash I have to say
powershell.exe ./scriptfile.ps1
try to change permission and make it excutable with chmod +x file.ps1
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of products
sudo apt-get update
# Enable the "universe" repositories
sudo add-apt-repository universe
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh
I am trying to update the path inside my container. I have been everywhere and checked out several threads on this and nothing works. So, that's the trick?
# escape=`
ARG SDK_VERSION=4.8
FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
ENV NODE_VERSION=8.11.2
HEALTHCHECK NONE
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:/Foo/bin"
USER ContainerUser
SHELL ["cmd", "/S", "/C"]
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
This is the error:
PS C:\users\cbongiorno\source> docker build -t mercury:latest -m 4GB -f Dockerfile i18n-tools
Sending build context to Docker daemon 2.057MB
Step 1/10 : ARG SDK_VERSION=4.8
Step 2/10 : FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
---> 4a9d58026a2d
Step 7/10 : RUN setx /M PATH "%PATH%;C:/Foo/bin"
---> Running in 1caf9e758af2
SUCCESS: Specified value was saved.
C:/Foo/bin : The term 'C:/Foo/bin' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:96
+ ... ogressPreference = 'SilentlyContinue'; setx /M PATH %PATH%;C:/Foo/bin
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:/Foo/bin:String) [], ParentCo
ntainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; setx /M PATH "%PATH%;C:/Foo/bin"' returned a non-zero code: 1
There are at least two ways to update the PATH:
Using setx as the earlier answer suggests and as is displayed in the golang's nanoserver-1809/Dockerfile. Notice the use of the USER instruction. The ContainerAdministrator must be used in order to set the system PATH:
# PATH isn't actually set in the Docker image, so we have to set it from within the container
USER ContainerAdministrator
RUN setx /m PATH "%GOPATH%\bin;C:\go\bin;%PATH%"
USER ContainerUser
# doing this first to share cache across versions more aggressively
Using Powershell as displayed in the golang's windowsservercore-ltsc2016/Dockerfile
RUN $newPath = ('{0}\bin;C:\go\bin;{1}' -f $env:GOPATH, $env:PATH); \
Write-Host ('Updating PATH: {0}' -f $newPath); \
[Environment]::SetEnvironmentVariable('PATH', $newPath, [EnvironmentVariableTarget]::Machine);
# doing this first to share cache across versions more aggressively
Both ways do work with recent Docker Desktop.
The ENV instruction is not going to work on Windows.
This is what I got working, but it doesn't seem ideal:
RUN setx /M PATH $($Env:PATH + ';C:\Foo\bin')
I am running on Windows 2019 server.
I am getting an error whenever I invoke powershell from a dockerfile on docker build
Error is..
---> Running in 6efa29aa8a4a
The command 'powershell -Command DIR' returned a non-zero code: 3221226505
Dockerfile..
# escape=` (backtick)
FROM mcr.microsoft.com/windows/servercore:ltsc2019
RUN DIR
RUN ["powershell", "-Command", "DIR"]
COPY ./ app/
WORKDIR app
CMD [ "someapp", "somearg" ]
I have tried replacing cmd with powershell via
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN DIR
and the results are the same.
Thanks
Try running a dockerfile more like
FROM mcr.microsoft.com/windows/servercore/iis
RUN powershell -NoProfile -Command Remove-Item -Recurse
C:\inetpub\wwwroot*
WORKDIR /inetpub/wwwroot
COPY . /inetpub/wwwroot
maybe you gotta move your COPY command after the WORKDIR ?
Try different things out
Check to make sure you have update KB4532691 installed on your build machine (and host). The newest image of ltsc2019 (20/2020) has issues without it.
See https://hub.docker.com/_/microsoft-windows-servercore and https://support.microsoft.com/en-us/help/4542617/you-might-encounter-issues-when-using-windows-server-containers-with-t for more info
I was trying to run some commands inside a powershell script. The scrpt execution doesn't show any error. Script content is like:
Export-PfxCertificate –Cert $selectedcert_CA –FilePath "C:\\Windows\\System32\\CA_cert.pfx" -Password $SecurePassword -Verbose -force
But when I execute the powershell script from ruby as:
powershell.exe -file file.ps1 arg1 arg2
It displays file is created on path but I don't see any file created. If I run the same command separately (not in script just as a powershell command), it creates the file perfectly.
Is there any rights issue or I need to run the script with some permissions.
This snippet will run the powershell console with administrative rights
PS> Start-Process powershell -Verb runAs