简体   繁体   中英

How do I write a powershell script to setup a developer environment on Windows?

Stack Overflow,

I am working on a PowerShell script to setup a developer environment for my team. I want to have it so that a user can click a shortcut and have the necessary developer tools downloaded and installed for them. These installations should be global and on the PATH. I already have the shortcut that calls the script working. The script itself is what I am having issues with.

I am using chocolatey to try and install node on the developers machine's.

Here is the code I have written so far

devEnv.ps1

echo "Installing Chocolatey package manager, check version with choco"
$env: ChocolateyInstall="$Home/chocolatey"
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
echo "Chocolatey installed. Installing NodeJS and NPM via Chocolatey."
Start-Process powershell.exe -argument "-NoExit -Command ./nodejs.ps1"
echo "Installation complete."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

I am able to install chocolatey successfully, however I am not able to install node. I created another script that I call on line 5 of devEnv.ps1

I was hoping that the new PowerShell instance would have access to the choco command. It does not. Below I have included the second script.

nodejs.ps1

Invoke-Expression -Command "choco install nodejs.install"

Error Returned

choco : The term 'choco' 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:1
+ choco install nodejs.install
+ ~~~~~
+ CategoryInfo          : ObjectNotFound: (choco:String) [], 
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

So I walked through your script and ran into some room for improvement:

$Env:ChocolateyInstall = "$HOME\chocolatey"

$chocoInstall = New-TemporaryFile
'Installing chocolatey package manager.'
'Check version with `choco.exe --version`.'
[Net.WebClient]::new().DownloadString(
    'https://chocolatey.org/install.ps1'
) | Out-File -FilePath $chocoInstall.FullName -Encoding UTF8
. $chocoInstall.FullName

'Chocolatey installed. Installing NodeJS and NPM via Chocolatey.'
& "$Env:ProgramData\chocolatey\choco.exe" install nodejs --yes --force

'Installation complete.'

'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

This will address your issue of choco.exe not being found in the PATH environment variable by full-pathing the executable call. Additionally, it will address cases where npm may already be installed and auto-confirms the package.

You may want to look into Boxstarter for this use case. Boxstarter will handle the reboots needed for certain tools and has additional options for configuring the working environment (configuring Explorer options as an example). It also handles this particular case where choco.exe is not found just after installing it (as the PATH has been updated but the Windows hasn't updated the current session with the change).

You'll find it at https://boxstarter.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM