<# ESEGUE UNA CHIAMATA A UNA uri e fa il log dei dati restituiti ... Se tutto Ok scrive un file di check, questo file viene usato per verificare che non sia già stato eseguito in modo corretto nelle precedenti n. ore, così posso schedularlo più volte Esegue prima una chiamata ping al server, poi una chiamata a una URL di test e infine la chiamata effettiva Sarebbe da sistemare e rendere parametrica / procedura ... # *** NOTE TECNICHE PER SCHEDULAZIONE # Per far scrivere il ritorno dalla chiamata di IIS nel file di log ho creato la schedulazione entrando con # l'utente stesso (Steamware) che esegue lo script e ho fatto import della schedulazione ( in ambiente ISE il log lo scrive ) # Ho messo anche permessi all'utente "Logon as batch job" ( vedi sotto ) # Utente messo in PowerUser e permessi scrittura cartella Steamware # Logon as batch job policy is set for the user. This policy is accessible by opening the Control Panel, Administrative Tools, # and then Local Security Policy -> Local Policy -> User Rights Assignment -> then Logon as batch job. # Task Security Context # https://forsenergy.com/en-us/taskscheduler/html/a922c2b5-6a43-4503-ab7f-4f3d77b3cc8a.htm # Mod. 2022-05-16 Minor Fix # Mod. 2022-05-17 Ping e Varie # Mod. 2022-05-18 add Check ObjTest #> Remove-Variable * -ErrorAction SilentlyContinue # pulisco variabili x ISE # avvio stopwatch $stopwatch = [system.diagnostics.stopwatch]::StartNew() $FolderName="c:\Steamware\Logs" $Logfile = Join-Path $FolderName "\GPW_checkTimb.log" $Server = "office.egalware.com" $testUri = "https://office.egalware.com/GPW/Api/api/VC19" $callUri = "https://office.egalware.com/GPW/Api/api/TimbraCheck/DailyCheck" # file scritto solo se tutto OK - check se scritto in precedenza non eseguo $ObjTest = Join-Path $FolderName "\GPW_checkTimbOk.log" $CheckHour = -6 Function LogWrite { Param ([string]$logstring) $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss.fff") Add-content $Logfile -value "$Stamp $logstring" } # test e creazione DIR di Log if (Test-Path $FolderName) { # Write-Host "Folder Exists" } else { #PowerShell Create directory if not exists New-Item $FolderName -ItemType Directory Write-Host "Folder Created successfully" } # Test, se già eseguito in modo corretto in precedenza esco... ( file $ObjTest scritto nelle ore precedenti ) $time = (Get-Date).AddHours( $CheckHour ) if ( Get-ChildItem -Path $ObjTest -Recurse -Force | Where-Object {$_.LastWriteTime -gt $time } ) { LogWrite("CheckProj process already done...") Break } else { LogWrite("Start CheckProj process...") } # # TEST PING CONNECTION # LogWrite("Start Ping --> $Server ...") # Test-Connection -ComputerName $Server -ErrorAction SilentlyContinue | Format-Table -AutoSize | Out-File -encoding utf8 $Logfile -Append # se non lo trova prosegue cmq try { # prima faccio una chiamata di test per "svegliare" IIS... LogWrite("Test Call") Invoke-WebRequest -URI $testUri -UseBasicParsing } catch { LogWrite("TEST Call Error...") LogWrite($Error[0]) } try { # vera chiamata LogWrite("Test Call effettuata, effettuo chiamata target") $Response = Invoke-WebRequest -URI $callUri -UseBasicParsing LogWrite($Response) (Get-Date).toString("yyyy/MM/dd HH:mm:ss.fff") | Out-File $ObjTest #[string]$Return = $Response.Content # messo per prova visto che non creava nulla in LOG - potrebbe non servire } catch { $Response = 'CALL ERROR ...' LogWrite($Error[0]) } finally { LogWrite("*** END Call ***") } # fermo stopwatch e conteggio... $stopwatch.Stop() $durata = $stopwatch.Elapsed.TotalSeconds & "C:\Program Files\Zabbix Agent\zabbix_sender.exe" -z zabproxy.ufficio -s "W2022-IIS04" -k Gpw.Timb -o $durata LogWrite("Durata esecuzione: $durata")