| | |
| | | [int]$ConnectTimeoutSec = 10, |
| | | [int]$CommandTimeoutSec = 15, |
| | | [int]$HttpTimeoutSec = 30, |
| | | [string]$StateFile = (Join-Path $PSScriptRoot 'j22-collector-state.json'), |
| | | [string]$LogFile = (Join-Path $PSScriptRoot 'j22-collector.log'), |
| | | # 下面两个留空即在正文里解析成"脚本同目录";不要写成 param 默认值: |
| | | # Windows PowerShell 的参数默认值不在脚本作用域里求值,$PSScriptRoot 在那里是空的(现场实测踩到)。 |
| | | [string]$StateFile, |
| | | [string]$LogFile, |
| | | [switch]$Once |
| | | ) |
| | | |
| | | $ErrorActionPreference = 'Continue' |
| | | |
| | | # 脚本所在目录:$PSScriptRoot 为主,取不到时退回命令路径、再退回当前目录。 |
| | | $ScriptDirectory = $PSScriptRoot |
| | | if ([string]::IsNullOrEmpty($ScriptDirectory)) { $ScriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path } |
| | | if ([string]::IsNullOrEmpty($ScriptDirectory)) { $ScriptDirectory = (Get-Location).Path } |
| | | if ([string]::IsNullOrEmpty($StateFile)) { $StateFile = Join-Path $ScriptDirectory 'j22-collector-state.json' } |
| | | if ([string]::IsNullOrEmpty($LogFile)) { $LogFile = Join-Path $ScriptDirectory 'j22-collector.log' } |
| | | |
| | | # ---------------------------------------------------------------- 日志 |
| | | function Write-Log([string]$message) { |
| | | $line = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss') + ' ' + $message |