zoukankan      html  css  js  c++  java
  • 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	Removing old publish output"
        pushd $(pwd)/$project
        rm -rf obj/Docker/publish
        echo -e "e[33m	Restoring project"
        dotnet restore
        echo -e "e[33m	Building 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="$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

    自己感觉,这两个东西在以后的程序部署上都会发挥各自强大的力量!

  • 相关阅读:
    crontab定时任务写法记录
    Django与Vue语法冲突问题完美解决方法
    Django下MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL解惑
    解决python2.x用urllib2证书验证错误, _create_unverified_context
    django 异步任务实现及Celery beat实现定时/轮询任务
    用Python写WebService接口并且调用
    django(权限、认证)系统——第三方组件实现Object级别权限控制
    django(权限、认证)系统—— 基于Authentication backends定制
    django(权限、认证)系统—— Permissions和Group
    [jmeter]linux下自动测试环境+持续集成ant+jmeter+Apache(httpd)环境搭建与使用
  • 原文地址:https://www.cnblogs.com/lori/p/6955488.html
Copyright © 2011-2022 走看看