PowerShell和Bash的介紹


PowerShell是運行在windows平台的腳本,而Bash是運行在linux平台的腳本

現在bash能做的事情,PowerShell也能做,PowerShell的強大之處是它可以管理windows服務器(特別是域domain),現在的開源PowerShell 也可以管理Linux和Mac(通過PSRP)。

下載最新的PS程序

https://msdn.microsoft.com/en-us/Mt173057.aspx

安裝后它會有powershell和它的開發IDE工具,ISE,非常不錯!

一、進行powershell的程序

二、創建腳本,簡單的Helloworld.ps1

任務的自動化是以程序文件或者可執行腳本文件為基礎的,PowerShell也支持將命令列表做成腳本文件來執行。以下是Helloworld.ps1腳本文件的內容:

$a = "Hello World!"
$a
echo $a > a.txt
dir a.txt

Helloworld.ps1腳本文件的執行情況結果如下:

PS E:\>.\Helloworld.ps1  --注意在執行它時要加.\,表示當前上當下的文章,類似於centos里的文件執行方法
Hello world!
Directory: E:\
Mode                LastWriteTime     Length   Name                                                                     
----                -------------     ------ ----                                                                     

-a---         5/30/2017   4:56 PM         16 a.txt 

下面是在eShopOnContainers上的一個例子,分別用ps和bash實現了程序的部署

#!/bin/bash
declare -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:prod

for 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
    popd
done

# 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) -f
fi

# 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.

powershell代碼如下

Param([string] $rootPath)
$scriptPath = Split-Path $script:MyInvocation.MyCommand.Path

Write-Host "Current script directory is $scriptPath" -ForegroundColor Yellow

if ([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/*" -q

If (-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

自己感覺,這兩個東西在以后的程序部署上都會發揮各自強大的力量!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM