第一个PowerShell Update脚本update-sign.ps1

java程序安装脚本内容 

#war包下载url
$DownloadUrl="http://192.168.0.117:81/sign_sys/ROOT.war"

#war包存放路径
$DownPath = "C:\sign_down\ROOT.war"

#tomcat服务项目路径
$TomcatDirectory='C:\apache-tomcat-8.5.41\webapps\'

#jdbc配置文件路径
$sign_webapps_conf=$TomcatDirectory + 'ROOT\WEB-INF\classes\'

#jar包路径
$sign_webapp_jar=$TomcatDirectory + 'ROOT\WEB-INF\lib\sign-service-1.0.jar'

#项目路径
$sign_webapps=$TomcatDirectory +'ROOT\'


#war包下载
$client = new-object System.Net.WebClient
$client.DownloadFile($DownloadUrl, $DownPath)

#系统服务状态查询
$ser=Get-Service "Apache Tomcat 8.5 Tomcat-sign"
$Service_Name="Apache Tomcat 8.5 Tomcat-sign"

#stop系统
if($ser.Status -eq "Running"){
	do{
		Write-Host $ser "<==Stopping service..."
		Write-Host $ser.Status "......"
		Write-Host " "
		Stop-Service $Service_Name
	}while($_.Status -eq "Running") #need to wait until it stopped 需要等到它停止

	if($_.Status -eq "Running"){
        Write-Host $ser.Status "<==The Service is Running"
		Write-Host $ser.Status "......"
        Write-Host " "
	}else{
		Write-Host $ser "<==Stop the servcie successful"
		Write-Host $ser.Status "......"
		Write-Host " "
	}
}else{
	Write-Host $ser.Status "<== The service is already stopped now"
	Write-Host $ser.Status "......"
	Write-Host " "
}

#move移动备份项目文件夹
$FolderToCreate = "C:/sign_back/"
if (!(Test-Path $FolderToCreate -PathType Container)) {
	New-Item -ItemType Directory -Force -Path $FolderToCreate
}
$src ='C:\apache-tomcat-8.5.41\webapps\'
$dest = 'C:/sign_back'
$NowDateTime=Get-Date -Format yyyyMMddHHmmss
Get-ChildItem -Path $src -Filter 'ROOT' -Recurse | ForEach-Object {
	$nextName = Join-Path -Path $dest -ChildPath $_.name
	while(Test-Path -Path $nextName) {
		$nextName = Join-Path $dest ($_.BaseName + "-$NowDateTime")
	}
	$_ | Move-Item -Destination $nextName
}
Write-Host $ser "<== Moving successful"
Write-Host " "


#删除5天以前的备份文件夹
$TimeOutDays=5    
$filePath = 'C:/sign_back'
$allFiles= get-childitem -path $filePath     
foreach ($files in $allFiles)     
{      
   $daypan=((get-date)-$files.lastwritetime).days       
   if ($daypan -gt $TimeOutDays)       
   {         
     remove-item $files.fullname -Recurse -force       
    }     
}

#重新创建新的项目文件夹
$FolderToCreate = "C:\apache-tomcat-8.5.41\webapps\ROOT\"
if (!(Test-Path $FolderToCreate -PathType Container)) {
	New-Item -ItemType Directory -Force -Path $FolderToCreate
}
Write-Host " "

#解压war包
cd $sign_webapps
jar xf $DownPath
cd "C:\"
Write-Host $ser "<== The war package was decompressed successfully"
Write-Host " "

cd "C:\Users\Administrator\Desktop\sign-conf\"

#覆盖替换sign-service-1.0.jar包coin币种配置文件
jar uf $sign_webapp_jar "coins.properties"
jar uf $sign_webapp_jar "cpctCoins.properties"

#覆盖替换jdbc配置文件
Copy-Item "application.properties" $sign_webapps_conf
Copy-Item "application-prod.properties" $sign_webapps_conf
cd "C:\"

#重启签名系统服务
if($ser.Status -eq "Running"){
	Write-Host $ser "<== The service the running now,will restart the service"
	Write-Host " "
	Write-Host $ser "<== Starting service..."
	Write-Host " "
	Restart-Service $Service_Name
	Write-Host $ser "<== Start the servcie successful"
	Write-Host  " "
}elseif($ser.Status -eq "Disabled"){
	#Set the status of service, cause the cmd Start-Service can not start the service if the status is disabled
	#设置服务状态,如果状态为禁用,则导致cmd Start-Service无法启动服务
	Set-Service $Service_Name -StartupType automatic Manual
	Write-Host $ser.Status "<== Set the status to manual successful"
	Write-Host  " "
	Write-Host $ser "<== Starting service..."
	Write-Host $ser "......"
	Start-Service $Service_Name
	Write-Host $ser "<== Start the servcie successful"
	Write-Host  " "
}else{
	Write-Host $ser "<== The service the stopped now,will start the service"
	Write-Host  " "
	Write-Host $ser.Status "<== The status of servcie now"
	Write-Host  " "
	Write-Host $ser "<== Starting service..."
	Write-Host $ser "......"
	Start-Service $Service_Name
	Write-Host $ser "<== Start the servcie successful"
	Write-Host  " "
}

脚本执行结果

猜你喜欢

转载自blog.csdn.net/baidu_38432732/article/details/112587950