powershell指令碼

KeepSmiling_me發表於2024-04-10

# 全域性終止設定方便捕獲異常
$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"
}

相關文章