fc9c490d36
Aggiunta file
221 lines
7.4 KiB
PowerShell
221 lines
7.4 KiB
PowerShell
<#
|
|
.DESCRIPTION
|
|
Stop di una appPoll, cancellazione dir temp ASP.NET, start pool o backup della dir della webapp
|
|
ATTENZIONE. Se abilito il backup della app non avvia il pool dopo averlo stoppato
|
|
|
|
.PARAMETER <paramName>
|
|
.EXAMPLE IN BAT FILE
|
|
|
|
PowerShell.exe .\AppPoolRestart.ps1 ^
|
|
-appPoolName 'Pool GMW Test' ^
|
|
-AppPath 'GMW_Test' ^
|
|
-FramPath '\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\' ^
|
|
-Install $False ^
|
|
-FileLog 'AppPoolRestart_GMW_Test' ^
|
|
-wwwRoot 'C:\inetpub\wwwroot\*.*' ^
|
|
-BackupPath 'D:\Progetti\GMW\Backup_WWWROOT\
|
|
#>
|
|
|
|
PARAM (
|
|
[string]$appPoolName = "Pool GMW Test" # App Pool da riavviare
|
|
,[string]$AppPath = "GMW_Test" # Path temporaneo da cancellare
|
|
,[string]$FramPath = "\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\" # dir a cui aggiungo AppPath e poi viene cancellata
|
|
|
|
,[bool]$Install = $True # se $True fa backup dell'app e lascia il pool spento altrimenti riavvia il pool
|
|
,[string]$wwwRoot = "C:\inetpub\wwwroot\*.*" # dir copiata x backup
|
|
,[string]$BackupPath = "D:\Progetti\GMW\Backup_WWWROOT\" # dir a cui viene aggiunta la data e usata x copia backup
|
|
|
|
,[string]$FileLog = "App_PoolRestart"
|
|
)
|
|
|
|
# Remove-Variable * -ErrorAction SilentlyContinue # pulisco variabili x ISE
|
|
CLS
|
|
|
|
import-module WebAdministration
|
|
|
|
# $appPoolName = 'Pool GMW'
|
|
# $AppPath = 'GMW' # dir ASP.NET temporanea da cancellare
|
|
# $FramPath = "\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\$AppPath"
|
|
|
|
$FramPathDel = $FramPath + $AppPath # es. "\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\GMW_Test"
|
|
$TempAspPath = $env:SystemRoot + $FramPathDel # path completo dir ASP.NET temporanea da cancellare
|
|
$Date = (Get-Date -format "yyyy-MM-dd HH:mm")
|
|
$BackupPath = $BackupPath + (Get-Date -format "yyyy-MM-dd_HH_mm") + '\' # aggiungo data al backup path
|
|
|
|
# PARAMETRI PER BACKUP INSTALL APP
|
|
# $Install = $False # se $True fa backup dell'app e lascia il pool spento altrimenti riavvia il pool
|
|
# $wwwRoot = "C:\inetpub\wwwroot\GMW_Test\*.*"
|
|
# $BackupPath = "D:\Progetti\GMW\Backup_WWWROOT\GMW_Test_$Date\"
|
|
# $FileLog = "App_PoolRestart"
|
|
|
|
# ----------------------------
|
|
# parametri per log errori
|
|
# ----------------------------
|
|
# path dello script ps1 mi serve a causa di problemi con lo schedulatore
|
|
if ( $MyInvocation.MyCommand.Path -ne $null ) {
|
|
$scriptPath = split-path -parent $MyInvocation.MyCommand.Path
|
|
}
|
|
else {
|
|
CD
|
|
$scriptPath = "."
|
|
}
|
|
|
|
$ErrlogFile = "$scriptPath\$FileLog-ErrorLog.txt" # "$scriptPath\ErrLog_Log.txt" # se non lo voglio mettere = $null quindi posso usare una variabile
|
|
$LogFile = "$scriptPath\$FileLog-Log.txt"
|
|
|
|
$ErrorActionPreference = "Stop" # comportamento x errori minori
|
|
|
|
# attivo il Transcript NO per powershell 1.0
|
|
# Start-Transcript -path $LogFile | Out-Null
|
|
|
|
">>> Start : $Date" >> $LogFile
|
|
|
|
TRY
|
|
{
|
|
|
|
$PoolStateIni = (Get-WebAppPoolState $appPoolName).Value # salvo stato pool iniziale
|
|
|
|
# STOP APP POOL
|
|
if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
|
|
{
|
|
" Stop Pool : $appPoolName" >> $LogFile
|
|
Stop-WebAppPool -Name $appPoolName
|
|
}
|
|
|
|
# attendo fino a che è Stopped
|
|
$try = 5
|
|
$step = 0
|
|
While ( $try -gt $step )
|
|
{
|
|
$step++
|
|
|
|
if( (Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
|
|
{
|
|
$appPoolName + " " + (Get-WebAppPoolState $appPoolName).Value
|
|
Start-Sleep -s 5
|
|
}
|
|
else
|
|
{
|
|
(Get-WebAppPoolState $appPoolName).Value
|
|
$step = $try # esco
|
|
}
|
|
|
|
} #end while
|
|
|
|
# =======================================
|
|
# DELETE TEMP E BACKUP o START APP POOL
|
|
# =======================================
|
|
if((Get-WebAppPoolState $appPoolName).Value -eq 'Stopped')
|
|
{
|
|
|
|
# cancello directory Temp ASP.NET
|
|
" Delete Dir -> $TempAspPath" >> $LogFile
|
|
Remove-Item $TempAspPath -Force -Recurse -ErrorAction SilentlyContinue # se anche non c'è la dir non do errore
|
|
|
|
if ( $Install -eq $True )
|
|
{
|
|
# COPIA DELLA WebbApp - uso XCOPY così copio anche i permessi
|
|
XCOPY $wwwRoot $BackupPath /s /e /h /x /v
|
|
Compact /C /S "$BackupPath*.*" # comprimo cartella
|
|
|
|
# EXPLORER.exe $BackupPath
|
|
}
|
|
else # solo restart
|
|
{
|
|
# riavvio Pool solo se era start all'inizio
|
|
if ($PoolStateIni -eq 'Started') {
|
|
" Start Pool : $appPoolName" >> $LogFile;
|
|
Start-WebAppPool -Name $appPoolName
|
|
}
|
|
}
|
|
}
|
|
|
|
} # end TRY
|
|
|
|
# ==== GESTIONE ERRORI =====
|
|
CATCH [Exception]
|
|
{
|
|
# imposto invio mail di errore
|
|
$pMailError = $True
|
|
|
|
# salvo errore
|
|
"CATCH Error at $Date" > $ErrlogFile
|
|
|
|
$error[0]
|
|
Write-Output $Error[0] >> $ErrlogFile
|
|
|
|
$err = $_.Exception
|
|
while ( $err.InnerException )
|
|
{
|
|
$err = $err.InnerException
|
|
Write-Output $err.Message # anche a video
|
|
Write-Output $err.Message >> $ErrlogFile
|
|
}
|
|
}
|
|
# ==== STEP FINALE - sempre eseguito =====
|
|
FINALLY
|
|
{
|
|
# SE APP POOL STOPPED LO AVVIO
|
|
if((Get-WebAppPoolState $appPoolName).Value -eq 'Stopped' -and $PoolStateIni -eq 'Started' -and $Install -ne $True )
|
|
{
|
|
" Start Pool FINALLY : $appPoolName" >> $LogFile
|
|
Start-WebAppPool -Name $appPoolName
|
|
}
|
|
|
|
# "End execute at : $Date" > $ErrlogFile
|
|
# ================================
|
|
# INVIO MAIL ( error And success )
|
|
# ================================
|
|
<# if ( $SendMail -and $pMailError ) # ( send con errore )
|
|
{ EmailNotification $aListMailError }
|
|
|
|
ElseIf ( $SendMail -and $SendMailSuccess ) # ( send if success )
|
|
{ EmailNotification $aListMailSuccess }
|
|
#>
|
|
# Stop capturing output
|
|
# Stop-Transcript
|
|
}
|
|
|
|
<# Comprime ma non tiene le permission
|
|
|
|
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
|
|
$Date = (Get-Date -format "yyyy-MM-dd_HHmm")
|
|
$sourcePath = 'C:\inetpub\wwwroot\WebGIM';
|
|
$destPath="C:\Steamware\Test2_$Date.zip";
|
|
|
|
$includeBaseDirectory = $true # nello zip anche cartella base
|
|
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal;
|
|
|
|
# zip intera cartella!!!
|
|
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath,$destPath, $compressionLevel, $includeBaseDirectory);
|
|
#>
|
|
|
|
<#
|
|
|
|
# errore dice che il file esiste già, forse zip solo della cartella usare CreateEntry?
|
|
|
|
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
|
|
$currentDate = Get-Date;
|
|
$currentDate | Get-Member -Membertype Method Add;
|
|
$daysBefore = -1;
|
|
$archiveTillDate = $currentDate.AddDays($daysBefore);
|
|
|
|
$compressionLevel= [System.IO.Compression.CompressionLevel]::Optimal
|
|
|
|
foreach( $item in ( Get-ChildItem $sourcePath | Where-Object { $_.CreationTime -le $archiveTillDate }) )
|
|
{
|
|
[System.IO.Compression.ZipFile]::CreateFromDirectory( $sourcePath, $destPath, $compressionLevel, $true);
|
|
|
|
}
|
|
|
|
#altro
|
|
Get-ChildItem "C:\inetpub\wwwroot\WebGIM\*.*" -Recurse | foreach {
|
|
$File = $_.fullName
|
|
[System.IO.Compression.ZipFile]::CreateFromDirectory($File, "C:\Temp\Folder.zip")
|
|
}
|
|
|
|
#>
|
|
|