The Essentials: After Windows Server 2019 Evaluation Installed - PART II

Last time I wrote Windows Server 2019 installation summary (and VS 2019 Build Tool) , and then described the basic post-installation configuration work. But this is far from my goal-a lightweight (22GB lighter) MSVC build environment-is still far from it.

So continue to add the problems encountered in the configuration process. as follows.

Summary after installation

Install Chocolatey

Install Chocolatey:

powershell Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) 

chocolatey is a command line package manager. Like apt.

You can find specific packages on the official choco software repository page: https://chocolatey.org/packages .

Install nano editor

Using the simple editor in the command line, nano can meet most of the needs of lightweight editing.

Install it:

choco install -y nano

Then you can directly nano 1.txt in the command line.

Revise environment variables

In the command line interface of Server Core, you are at the cmd prompt by default, and you can enter the command "powershell" to enter the PowerShell prompt mode.

In the cmd prompt, set and set PATH still valid and can be used to display all or specified environment variables. set PATH="%PATH%;xxx" can set environment variables.

Officials have explained this: About environment variables-PowerShell-Microsoft Docs

Permanently revise environment variables

In order to permanently modify the environment variables, you need to use the Set-ItemProperty interface provided in PowerShell.

If you are really interested in permanently modifying environment variables at the cmd prompt, check the reference of the SETX command:

setx /?

Sometimes, you will find that the universal restart is very useful: shutdown -r -f .

Fortunately, server core restarts are often very fast, at least fast enough.

Auxiliary function

In the powershell environment, you can add a set of functions to simplify the revision of environment variables. First enter the PowerShell prompt, and then paste the following script:

function get_path () {
  $val = Get-ItemProperty -Path HKCU:\Environment -Name Path
  $val.path.Split(';')
  # Write-Host $val.Replace(";","`n") -ForegroundColor Green
}
function set_path ($new_path) {
  Set-ItemProperty -Path HKCU:\Environment -Name path -Value $new_path
  Write-Host "The operation completed successfully.`n" -ForegroundColor Green
}
function add_path ($path) {
  $val = Get-ItemProperty -Path HKCU:\Environment -Name path
  $new_path = $val.path + ";" + $path.Trim()
  Set-ItemProperty -Path HKCU:\Environment -Name path -Value $new_path
  Write-Host "The operation completed successfully.`n" -ForegroundColor Green
}
function del_path ($path) {
  $path = ';' + ($path.Trim() -replace "\\","\\")
  $val = Get-ItemProperty -Path HKCU:\Environment -Name Path
  $old_path = $val.path
  $flag = 0
  
  # if the argument $path ends with a slash
  if ($path.endswith("\\")) {
      # if the path in the registry ends with a slash
      if ($old_path -match $path) {
          $new_path = $old_path -replace $path,""
          Set-ItemProperty -Path HKCU:\Environment -Name Path -Value $new_path
          $flag = 1
      }
      # if the path in the registry does not end with a slash
      else {
          $path = $path.Substring(0,$path.LastIndexOf('\') - 1)
          if ($old_path -match $path) {
              $new_path = $old_path -replace $path,""
              Set-ItemProperty -Path HKCU:\Environment -Name Path -Value $new_path
              $flag = 1
          }
      }
  }
  # if the argument $path does not end with a slash
  else {
      # if the path in the registry ends with a slash
      $path += "\\"
      if ($old_path -match $path) {
          $new_path = $old_path -replace $path,""
          Set-ItemProperty -Path HKCU:\Environment -Name Path -Value $new_path
          $flag = 1
      }
      else {
          # if the path in the registry does not end with a slash
          $path = $path.Substring(0,$path.LastIndexOf('\') - 1)
          if ($old_path -match $path) {
              $new_path = $old_path -replace $path,""
              Set-ItemProperty -Path HKCU:\Environment -Name Path -Value $new_path
              $flag = 1
          }
      }
  }

  if ($flag) {
      Write-Host "The operation completed successfully.`n" -ForegroundColor Green
  }
  else {
      Write-Host "The operation failed.`n" -ForegroundColor Red
  }
}

You can get four auxiliary functions.

  • Get_path can display the current value of PATH;
  • set_path "new_path_value" and add_path "append_text" can set the PATH value;
  • del_path "a_path_part" can delete a PATH segment. The value of the PATH variable is multiple segments separated by semicolons.
Make the above auxiliary function permanent

In the cmd prompt environment, edit the file %USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 through nano, and append the above code to the file. Each time you start PowerShell, these auxiliary functions will automatically take effect.

Install vscode

Generally speaking, nano can satisfy a lot of our fantasies. But we can also have a more magical option, install Visual Studio Code in Server Core, isn't it fragrant!

Go to the official website to download the installation package, and then execute the installation package on the server.

E.g:

curl -UseBasicParsing -Uri "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-user" -OutFile "vscode-user-setup-x64.exe"
.\vscode-user-setup-x64.exe

Note that in the powershell environment, curl is actually an alias for Invoke-WebRequest. If you want to check the available command line parameters, use "Invoke-WebRequest -?".

If you want to use GNU curl or wget, you can install them via chocolatey.

If you want to install Visual Studio Code for all users, pay attention to selecting System Installer when downloading the installation package.

Reference (for records only):

$Env:Path += "$Env:UserProfile\AppData\Local\Programs\Microsoft VS Code"

"c:\Program Files (x86)\Microsoft Visual Studio\Installer"

GUI?

In the server core environment, it is indeed possible to install Visual Studio Code. Its operation is slightly different, but you should not notice it.

You may encounter the problem of no file dialog like me. In other words, the File Open menu command in the vscode window is unresponsive. code 1.txt us, just start the vscode editor with 06125e372583a5 in the cmd prompt.

For example here:

image-20210819195640123

Kangkang, it does work. For people who have been in the bash environment for a long time, this is really unreasonable, but it is very reasonable, isn't it?

Install git

First you need to download the installation package of git for win: here .

You can run the installation package directly in Server Core, which is a bit of a violation (for those who are used to SSH) but it works:

image-20210819155813960

posh-git

posh-git is a set of integrated scripts to help you better use the git command line in the powershell environment, such as automatic completion.

The posh-git module can be installed through PowerShell, which is achieved through nuget behind the scenes:

Install-Module posh-git -Scope AllUsers -Force

If you just want to install to the current user, change AllUsers to CurrentUser .

The above method is from Git-Git in PowerShell

Related to Visual Studio 2019 Build Tool

First of all, after installing the VSBT tool in Server Core, the environment variables will not be updated. You need to execute LaunchDevCmd.bat of VS to enter the working environment of VSBT.

In the working environment, what cmake, cl, dotnet, nmake, msbuild, etc. will be effective.

The problem is that LaunchDevCmd.bat is very hidden. It is in C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools , which is definitely a long file name for a super crash in the command line environment.

So you can consider appending the path to the PATH environment variable, for example: SETX PATH "%PATH%;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools"

Then you can directly type LaunchDevCmd.bat to enter the build environment of VSBT.

vcpkg

In order to install some open source packages, vcpkg also needs to be ready. Its installation is very simple (refer to its official website ):

cd %USERPROFILE%\
mkdir work
cd work
git clone https://github.com/Microsoft/vcpkg.git
.\vcpkg\bootstrap-vcpkg.sh

After it is built, you can run it:

%USERPROFILE%\work\vcpkg\vcpkg list
%USERPROFILE%\work\vcpkg\vcpkg install yaml-cpp

In our source code development, the cmake build process needs to inject the vcpkg control file:

# configure
cmake -DENABLE_AUTOMATE_TESTS=OFF -S . -B build/ -DCMAKE_TOOLCHAIN_FILE=%USERPROFILE%/work/vcpkg/scripts/buildsystems/vcpkg.cmake

# build
cmake --build build/
# install
cmake --build build/ --target install

As above

🔚

Write so much for the time being.


hedzr
95 声望19 粉丝