Keywords: Build, Windows, PowerShell based Building Scripts

Usages

How to setup building environment of MSCV (VC++)

.ps1:

if (!(Test-Path vswhere.exe)) {
  Invoke-WebRequest -Uri "https://github.com/microsoft/vswhere/releases/download/2.8.4/vswhere.exe" -OutFile vswhere.exe
}

# setup vsdevcmd.bat
# https://github.com/microsoft/vswhere/wiki/Find-VC
$path = .\vswhere.exe -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
if ($path) {
  $batpath = join-path $path 'Common7\Tools\vsdevcmd.bat'
  if (test-path $batpath) {
    cmd /s /c """$batpath"" $args && set" | Where-Object { $_ -match '(\w+)=(.*)' } | ForEach-Object {
      $null = new-item -force -path "Env:\$($Matches[1])" -value $Matches[2]
    }
  }
  # dbghelp.dll, administrator permission needed
  $debuggerpath = join-path $path 'Common7\IDE\Extensions\TestPlatform\Extensions\Cpp\x64\dbghelp.dll'
  if (!(Test-Path 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64')) {
    New-Item 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64' -ItemType Directory -Force
    Copy-Item $debuggerpath 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\dbghelp.dll'
  }
}

.bat:

@echo off

set PROTOBUF_ARCH=x64
set VSDEVCMD=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat
if exist "%VSDEVCMD%" (
    @REM Tell VsDevCmd.bat to set the current directory, in case [USERPROFILE]\source exists. See:
    @REM C:\Program Files (x86)\Microsoft Visual Studio\%COMPILER%\Community\Common7\Tools\vsdevcmd\core\vsdevcmd_end.bat
     set VSCMD_START_DIR=%CD%
     call "%VSDEVCMD%" -arch=%PROTOBUF_ARCH%
      ) else (
     echo ERROR: Cannot find Visual Studio %COMPILER%
     exit /b 2
)
How to replace string in text file

example.ps1:

(Get-Content ../source/build.cs) | ForEach-Object { $_ -replace '//UserList.Add\("NeilWang"\);', 'UserList.Add("NeilWang");' } | Set-Content ../source/build.cs

Result:
//UserList.Add("NeilWang"); would be replaced as UserList.Add("NeilWang");.

Reference:
https://stackoverflow.com/a/60065/1645289

If want to run powershell script from Windows batch. do follows.
example.bat:

powershell -Command "(Get-Content ../source/build.cs) | ForEach-Object { $_ -replace '//UserList.Add\(\"NeilWang\"\);', 'UserList.Add(\"NeilWang\");' } | Set-Content ../source/build.cs"
How to call powershell script file(.ps1) from CMD

Example:

powershell -ExecutionPolicy Bypass -File example.ps1

Reference:
PowerShell says “execution of scripts is disabled on this system.”
https://stackoverflow.com/a/9167524/1645289

How to delete file if exists
IF EXIST [Filename] (
    del [Filename]
) ELSE (
    ...
)

Origin: Delete files in batch with without error message
https://stackoverflow.com/a/16552500/1645289

How to delete all fiels
::removes all files from the directory,
del /q destination\*
::recursively removes all nested directories
for /d %%x in (destination\*) do @rd /s /q "%%x"

How to delete all files and folders in a folder by cmd call
https://stackoverflow.com/a/1502935/1645289

Reference

Examples

Building script of WebRTC:
https://github.com/crow-misia/libwebrtc-bin/blob/main/build.windows.ps1

Books

Book List

The PowerShell Scripting and Toolmaking Book
https://leanpub.com/powershell-scripting-toolmaking


“This defines entrepreneur and entrepreneurship - the entrepreneur always searches for change, responds to it, and exploits it as an opportunity.” ― Peter F. Drucker, Innovation and Entrepreneurship: Practice and Principles