PowerShell和Bash的定義與使用

powershell是運(yùn)行在windows平臺(tái)的腳本,而bash是運(yùn)行在linux平臺(tái)的腳本

現(xiàn)在bash能做的事情,PowerShell也能做,PowerShell的強(qiáng)大之處是它可以管理windows服務(wù)器(特別是域domain),現(xiàn)在的開(kāi)源PowerShell 也可以管理Linux和Mac(通過(guò)PSRP)。

下載最新的PS程序

安裝后它會(huì)有powershell和它的開(kāi)發(fā)IDE工具,ISE,非常不錯(cuò)!

一、進(jìn)行powershell的程序

PowerShell和Bash的定義與使用

二、創(chuàng)建腳本,簡(jiǎn)單的Helloworld.ps1

任務(wù)的自動(dòng)化是以程序文件或者可執(zhí)行腳本文件為基礎(chǔ)的,PowerShell也支持將命令列表做成腳本文件來(lái)執(zhí)行。以下是Helloworld.ps1腳本文件的內(nèi)容:

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

Helloworld.ps1腳本文件的執(zhí)行情況結(jié)果如下:

PS E:>.Helloworld.ps1? –注意在執(zhí)行它時(shí)要加.,表示當(dāng)前上當(dāng)下的文章,類(lèi)似于centos里的文件執(zhí)行方法
Hello world!
Directory: E:
Mode??????????????? LastWriteTime???? Length?? Name?????????????????????????????????????????????????????????????????????
—-??????????????? ————-???? —— —-?????????????????????????????????????????????????????????????????????

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

下面是在eShopOnContainers上的一個(gè)例子,分別用ps和bash實(shí)現(xiàn)了程序的部署

#!/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:prodfor project in "${projectList[@]}"doecho -e "e[33mWorking on $(pwd)/$project"echo -e "e[33mtRemoving old publish output"pushd $(pwd)/$project      rm -rf obj/Docker/publish      echo -e "e[33mtRestoring project"dotnet restore      echo -e "e[33mtBuilding 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 Yellowif ([string]::IsNullOrEmpty($rootPath)) {      $rootPath = "$scriptPath.."}  Write-Host "Root path used is $rootPath" -ForegroundColor Yellow    $projectPaths =       @{Path="$rootPathsrcWebWebMVC";Prj="WebMVC.csproj"},      @{Path="$rootPathsrcWebWebSPA";Prj="WebSPA.csproj"},      @{Path="$rootPathsrcServicesIdentityIdentity.API";Prj="Identity.API.csproj"},      @{Path="$rootPathsrcServicesCatalogCatalog.API";Prj="Catalog.API.csproj"},      @{Path="$rootPathsrcServicesOrderingOrdering.API";Prj="Ordering.API.csproj"},      @{Path="$rootPathsrcServicesBasketBasket.API";Prj="Basket.API.csproj"}      @{Path="$rootPathsrcWebWebStatus";Prj="WebStatus.csproj"}    $projectPaths | foreach {      $projectPath = $_.Path      $projectFile = $_.Prj      $outPath = $_.Path + "objDockerpublish"$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

自己感覺(jué),這兩個(gè)東西在以后的程序部署上都會(huì)發(fā)揮各自強(qiáng)大的力量!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊6 分享