有專案要執行排程工作, 其實可以用quntize 實現, 但管理上會出現分散而難以管理的問題, 在有automation software 前, 唯有利用 task scheduler 作過渡.
# Name : HttpPost.ps1
# Author : Ling
# Description : Connect MAP Backend service to execute task.
#
# Syntax (in cmd) : Powershell.exe -ExecutionPolicy ByPass HttpPost.ps1 <<Web Service Endpoint URL>> <<JSON parameter>>
[string] $url=$args[0]
[string] $requestBody=$args[1]
[string] $logFile="C:\log\HttpPost.log"
[string] $stdOutputMessage=""
function Write-Log {
Param([string]$eventType, [string]$message)
$currentDateTime=Get-Date
$logContent="["+$currentDateTime+"] ["+$eventType+"] "+$message
echo($logContent)
add-content $logFile -Value $logContent
#Write-EventLog -LogName "PS HttpPost" -Source "Application" -EventID 3001 -EntryType $eventType -Message $message -Category 1 -RawData 10,20
}
try {
$stdOutputMessage="HttpPost start. URL:"+$url+"; Parameter:"+$requestBody
Write-Log -eventType "Information" -message $stdOutputMessage
$response=Invoke-RestMethod -Uri $url -ContentType "application/json" -Method POST -Body $requestBody
if($response.success) {
echo($response.message)
Write-Log "Information" $response.message
} else {
throw "Response fail in request. "+$response.message
}
} catch {
$stdOutputMessage=$_.Exception.Message
Write-Log "Error" $stdOutputMessage
} finally {
Write-Log "Information" "HttpPost executed.";
}
Leave a Reply