Introduction to PowerShell and Bash, powershellbash
PowerShell is a script running on windows, While Bash is a script running on linux.
What bash can do now can also be done by PowerShell. What makes PowerShell powerful is that it can manage windows servers (especially domain ), currently, open-source PowerShell can also manage Linux and Mac (via PSRP ).
1. powershell Program
2. Create a script, simple Helloworld. ps1
Task automation is based on program files or executable script files. PowerShell also supports making command lists into script files for execution. The content of the Helloworld. ps1 script file is as follows:
$a = "Hello World!"$aecho $a > a.txtdir a.txt
The execution result of the Helloworld. ps1 script file is as follows:
Ps e: \>. \ Helloworld. ps1 -- note that when executing it, add. \ to indicate the current article, which is similar to the file execution method in centos.
Hello world!
Directory: E:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/30/2017 4:56 PM 16 a.txt
The following is an example on eShopOnContainers. The program is deployed using ps and bash respectively.
#!/bin/bashdeclare -a projectList=( '../src/Services/Catalog/Catalog.API' '../src/Services/Basket/Basket.API' '../src/Services/Ordering/Ordering.API' '../src/Services/Identity/Identity.API' '../src/Web/WebMVC' '../src/Web/WebSPA' '../src/Web/WebStatus')# Build SPA app# pushd $(pwd)../src/Web/WebSPA# npm run build:prodfor project in "${projectList[@]}"do echo -e "\e[33mWorking on $(pwd)/$project" echo -e "\e[33m\tRemoving old publish output" pushd $(pwd)/$project rm -rf obj/Docker/publish echo -e "\e[33m\tRestoring project" dotnet restore echo -e "\e[33m\tBuilding and publishing projects" dotnet publish -o obj/Docker/publish popddone# remove old docker images:images=$(docker images --filter=reference="eshop/*" -q)if [ -n "$images" ]; then docker rm $(docker ps -a -q) -f echo "Deleting eShop images in local Docker repo" echo $images docker rmi $(docker images --filter=reference="eshop/*" -q) -ffi# No need to build the images, docker build or docker compose will# do that using the images and containers defined in the docker-compose.yml file.
The powershell code is as follows:
Param([string] $rootPath)$scriptPath = Split-Path $script:MyInvocation.MyCommand.PathWrite-Host "Current script directory is $scriptPath" -ForegroundColor Yellowif ([string]::IsNullOrEmpty($rootPath)) { $rootPath = "$scriptPath\.."}Write-Host "Root path used is $rootPath" -ForegroundColor Yellow$projectPaths = @{Path="$rootPath\src\Web\WebMVC";Prj="WebMVC.csproj"}, @{Path="$rootPath\src\Web\WebSPA";Prj="WebSPA.csproj"}, @{Path="$rootPath\src\Services\Identity\Identity.API";Prj="Identity.API.csproj"}, @{Path="$rootPath\src\Services\Catalog\Catalog.API";Prj="Catalog.API.csproj"}, @{Path="$rootPath\src\Services\Ordering\Ordering.API";Prj="Ordering.API.csproj"}, @{Path="$rootPath\src\Services\Basket\Basket.API";Prj="Basket.API.csproj"} @{Path="$rootPath\src\Web\WebStatus";Prj="WebStatus.csproj"}$projectPaths | foreach { $projectPath = $_.Path $projectFile = $_.Prj $outPath = $_.Path + "\obj\Docker\publish" $projectPathAndFile = "$projectPath\$projectFile" Write-Host "Deleting old publish files in $outPath" -ForegroundColor Yellow remove-item -path $outPath -Force -Recurse -ErrorAction SilentlyContinue Write-Host "Publishing $projectPathAndFile to $outPath" -ForegroundColor Yellow dotnet restore $projectPathAndFile dotnet build $projectPathAndFile dotnet publish $projectPathAndFile -o $outPath}######################################################################################### Delete old eShop Docker images########################################################################################$imagesToDelete = docker images --filter=reference="eshop/*" -qIf (-Not $imagesToDelete) {Write-Host "Not deleting eShop images as there are no eShop images in the current local Docker repo."} Else { # Delete all containers Write-Host "Deleting all containers in local Docker Host" docker rm $(docker ps -a -q) -f # Delete all eshop images Write-Host "Deleting eShop images in local Docker repo" Write-Host $imagesToDelete docker rmi $(docker images --filter=reference="eshop/*" -q) -f}# WE DON'T NEED DOCKER BUILD AS WE CAN RUN "DOCKER-COMPOSE BUILD" OR "DOCKER-COMPOSE UP" AND IT WILL BUILD ALL THE IMAGES IN THE .YML FOR US
I feel that these two things will exert their respective strength in future program deployment!