106 lines
3.2 KiB
PowerShell
106 lines
3.2 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Posts notices to Polaris via the Leap (PAPI) API. PowerShell port of ../post_notices.py.
|
|
|
|
.EXAMPLE
|
|
.\post_notices.ps1 -NoticeType 2 -OrgIds 3,4,5,6,7,8
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[int]$NoticeType,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$OrgIds,
|
|
|
|
[int]$DeliveryOption = 1,
|
|
|
|
# Defaults to a .env file next to the repo root (one level up from this script),
|
|
# matching where the Python version expects it.
|
|
[string]$EnvFile
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# $PSScriptRoot isn't always populated (e.g. PowerShell 2.0, or the script being
|
|
# dot-sourced/pasted rather than run via -File), so fall back to $MyInvocation.
|
|
$ScriptRoot = $PSScriptRoot
|
|
if (-not $ScriptRoot) {
|
|
$ScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
}
|
|
|
|
if (-not $EnvFile) {
|
|
$EnvFile = Join-Path $ScriptRoot "..\.env"
|
|
}
|
|
|
|
. (Join-Path $ScriptRoot "PolarisClient.ps1")
|
|
|
|
function Import-DotEnv {
|
|
param([string]$Path)
|
|
if (-not (Test-Path -LiteralPath $Path)) { return }
|
|
|
|
foreach ($line in Get-Content -LiteralPath $Path) {
|
|
$trimmed = $line.Trim()
|
|
if (-not $trimmed -or $trimmed.StartsWith('#')) { continue }
|
|
|
|
$idx = $trimmed.IndexOf('=')
|
|
if ($idx -lt 1) { continue }
|
|
|
|
$key = $trimmed.Substring(0, $idx).Trim()
|
|
$value = $trimmed.Substring($idx + 1).Trim()
|
|
if ($value.Length -ge 2 -and (
|
|
($value[0] -eq '"' -and $value[-1] -eq '"') -or
|
|
($value[0] -eq "'" -and $value[-1] -eq "'")
|
|
)) {
|
|
$value = $value.Substring(1, $value.Length - 2)
|
|
}
|
|
|
|
# Don't clobber a value already set in the environment (e.g. via Task Scheduler).
|
|
if (-not (Get-Item -Path "Env:$key" -ErrorAction SilentlyContinue).Value) {
|
|
Set-Item -Path "Env:$key" -Value $value
|
|
}
|
|
}
|
|
}
|
|
|
|
Import-DotEnv -Path $EnvFile
|
|
|
|
$requiredEnv = @(
|
|
"POLARIS_SERVER",
|
|
"POLARIS_SITE_DOMAIN",
|
|
"POLARIS_ORGANIZATION_ID",
|
|
"POLARIS_WORKSTATION_ID",
|
|
"POLARIS_USERNAME",
|
|
"POLARIS_PASSWORD"
|
|
)
|
|
$missing = $requiredEnv | Where-Object { [string]::IsNullOrWhiteSpace((Get-Item -Path "Env:$_" -ErrorAction SilentlyContinue).Value) }
|
|
if ($missing) {
|
|
Write-Error "Missing required .env values: $($missing -join ', ')"
|
|
Write-Error "Copy .env.example to .env and fill it in (or set these as environment variables)."
|
|
exit 1
|
|
}
|
|
|
|
$orgIdList = $OrgIds -split "," | Where-Object { $_.Trim() } | ForEach-Object { [int]$_.Trim() }
|
|
|
|
$language = if ($env:POLARIS_LANGUAGE) { $env:POLARIS_LANGUAGE } else { "eng" }
|
|
$productId = if ($env:POLARIS_PRODUCT_ID) { $env:POLARIS_PRODUCT_ID } else { "7" }
|
|
|
|
$client = [PolarisClient]::new(
|
|
$env:POLARIS_SERVER,
|
|
$env:POLARIS_SITE_DOMAIN,
|
|
$env:POLARIS_ORGANIZATION_ID,
|
|
$env:POLARIS_WORKSTATION_ID,
|
|
$language,
|
|
$productId
|
|
)
|
|
|
|
try {
|
|
$client.Authenticate($env:POLARIS_USERNAME, $env:POLARIS_PASSWORD)
|
|
$client.PostNotices($NoticeType, $orgIdList, $DeliveryOption) | Out-Null
|
|
} catch {
|
|
Write-Error "Error: $($_.Exception.Message)"
|
|
exit 1
|
|
}
|
|
|
|
Write-Output "Notices posted successfully: noticeType=$NoticeType, orgIds=$($orgIdList -join ','), deliveryOption=$DeliveryOption"
|