就 Powershell 脚本而言,该脚本将有助于 IIS 步骤和备份。
Import-Module WebAdministration
function Get-SiteName
{
[cmdletBinding()]
param(
[Parameter(Mandatory)]
$site)
if(Test-Path IIS:\Sites\$site)
{
Write-Host "The provided website name is $site and it is a valid website`r`n" -ForegroundColor Cyan
}
else
{
Write-Host "There is not a website present in the name provided`r`n" -ForegroundColor Red
Exit
}
}
function Stop-AppPool
{
# Get the app pool of the website
$appPool = Get-Item IIS:\Sites\$site | Select-Object applicationPool
# Get the app pool name
$appPoolName = $appPool.applicationPool
# Stop the app pool. If already stopped then a message will be shown
if ((Get-WebAppPoolState $appPoolName).Value -ne "Stopped")
{
Stop-WebAppPool $appPoolName
Write-Host "App pool $appPoolName is $((Get-WebAppPoolState $appPoolName).Value)"
}
else
{
Write-Host "App pool $appPoolName is already stopped`r`nNo need to stop it again`r`n" -ForegroundColor DarkGreen
}
}
function Test-IsStoppedOrNot
{
# Check the status of the app pool before taking backup
sleep -Seconds 30
if ((Get-WebAppPoolState $appPoolName).Value -eq "Stopped")
{
Write-Host "App pool $appPoolName is in stopped state`r`nBackup and Deploy can start`r`n" -ForegroundColor Cyan
}
else
{
Write-Host "App pool is not yet stopped`r`nTry after sometime`r`n" -ForegroundColor Red
Exit
}
}
function Make-BackupFolder
{
# Get the website physical folder of the website
$SiteFolder = Get-Item IIS:\Sites\$site
$SiteFolderName = $SiteFolder.physicalPath
Write-Host "The physical folder of the website is $SiteFolderName" -ForegroundColor DarkCyan
# Forming backup folder name
$backupFolder = $site + "_" + (Get-Date).ToString('MM.dd.yyyy.hh.mm.ss')
# Creating backup folder path
$DriveName = Split-Path -Path $SiteFolderName -Qualifier
$backupFolderPath = Join-Path "$DriveName" -ChildPath "Backup" | Join-Path -ChildPath "$backupFolder"
# Creating backup folder
mkdir $backupFolderPath
}
function Test-IsBackupFolderCreatedOrNot
{
# Confirm if the backup folder is created
if (Test-Path $backupFolderPath)
{
Write-Host "Backup folder $backupFolderPath is created`r`n" -ForegroundColor DarkGreen
Get-ChildItem $SiteFolderName | Copy-Item -Destination $backupFolderPath
Write-Host "Backup is complete`r`n" -ForegroundColor Blue
Write-Host "Perform the deployment`r`n" -ForegroundColor Cyan
}
else
{
Write-Host "Backup folder is not created`r`nTake a look please" -ForegroundColor Red
Exit
}
}
# Start the app pool. If already started then a message will be shown
function Start-AppPool
{
if ((Get-WebAppPoolState $appPoolName).Value -ne "Started")
{
Start-WebAppPool $appPoolName
Write-Host "App pool $appPoolName is $((Get-WebAppPoolState $appPoolName).Value)"
}
else
{
Write-Host "App pool $appPoolName is already started`r`nNo need to start it again`r`n" -ForegroundColor DarkGreen
Write-Host "THE DEPLOYMENT IS COMPLETE`r`n" -ForegroundColor DarkBlue
}
}
function Test-IsStartedOrNot
{
# Check the status of the app pool once again
sleep -Seconds 30
if ((Get-WebAppPoolState $appPoolName).Value -eq "Started")
{
Write-Host "App pool $appPoolName is not in started state yet`r`nTAKE A LOOK" -ForegroundColor Cyan
}
else
{
Write-Host "App pool is in started state`r`n" -ForegroundColor Red
Write-Host "THE DEPLOYMENT IS COMPLETE`r`n" -ForegroundColor DarkBlue
}
}
function Confirm-Deployment
{
[cmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateSet('true','false','yes','no','1','0')]
$CanProceed)
if (($CanProceed -eq 'true') -or ($CanProceed -eq 'yes') -or ($CanProceed -eq '0'))
{
Start-AppPool
}
}
Get-SiteName
Stop-AppPool
Test-IsStoppedOrNot
Make-BackupFolder
Confirm-Deployment
Test-IsStartedOrNot
该脚本执行以下操作:
- 从用户那里获取站点名称
- 如果站点名称不是有效的网站名称,脚本将终止
- 根据提供的站点名称,将确定站点的应用程序池和物理文件夹。
- 应用程序池已停止
- 等待 30 秒并检查应用程序池是否已停止
- 将使用物理文件夹路径导出备份文件夹路径。例如,如果网站路径是 D:\Sites\MySite,那么备份文件夹将位于 D:\Backup。备份文件夹名称为 SiteName+_+Date。日期的格式为month.day.year.hour.min.seconds。
- 执行备份
- 备份后,您需要按照自己的方式进行部署。脚本不进行部署。
- 该脚本会提示您确认您已执行部署。
- 如果您通过输入 true 或 yes 或 0 确认部署已完成,则脚本将启动应用程序池。
- 等待 30 秒,然后检查应用程序池是否已启动。如果不是,它会让你知道同样的事情。
请再次注意,脚本不负责您的部署。您需要进行部署。在您执行部署之前,您可以保持 POWERSHELL 会话打开。
此外,该脚本不会删除网站文件夹的内容。请根据需要包括该步骤。
希望这可以帮助。