powershell脚本

 

复制代码
# 全局终止设置方便捕获异常
$ErrorActionPreference = 'Stop'
# 隐藏进度条
$ProgressPreference = 'SilentlyContinue'

#下载jdk11并解压
Invoke-WebRequest -UseBasicParsing -OutFile "${TOOLS_DIR}/OpenJDK11U-jdk_x64_windows_hotspot_11.0.20.1_1.zip" "${DOWNLOAD_URL_GET_JDK}"
Expand-Archive -Path "${TOOLS_DIR}/OpenJDK11U-jdk_x64_windows_hotspot_11.0.20.1_1.zip" -DestinationPath "${TOOLS_DIR}" -Force >> $null 2> $null
#判断文件夹是否存在
if (-not (Test-Path "${TOOLS_DIR}/jdk-11.0.20.1+1" -PathType container)) { logger error 'install jdk fail' exit 123 }
#判断文件是否存在
 if (-not (Test-Path "$WORK_DIR/ciagent-${AGENT_ID}/springboot.jar" -PathType leaf)) {
        logger error 'install agent.jar fail'
        exit 123
    }
# 删除压缩包
Remove-Item "${TOOLS_DIR}/OpenJDK11U-jdk_x64_windows_hotspot_11.0.20.1_1.zip" >> $null 2> $null
# 停止历史服务
logger debug "stop and delete old ci agent service if exists"
try {
  #停止服务 Stop-Service "spring-${AGENT_ID}"
  #删除服务 sc.exe delete "spring-${AGENT_ID}"

   #删除文件

    Remove-Item "$WORK_DIR/spring-${AGENT_ID}" -Recurse -Force >> $null 2> $null 
}
catch {}
#创建文件夹
if (-not (Test-Path "${TOOLS_DIR}" -PathType container)) {
        New-Item -ItemType Directory -Path "${AGENT_TOOLS_DIR}" >> $null 2> $null
    }
#判断服务是否存在
$serviceStatus = Get-Service -Name ciagent-${AGENT_ID} -ErrorAction SilentlyContinue

if ($serviceStatus.Status -eq "Running") {
    logger info "agent is installed!"
} else {
    logger error "agent is not installed! check the log in ${WORK_DIR}/ciagent-${AGENT_ID}/logs"
}
复制代码
posted @   KeepSmiling_me  阅读(20)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示