Philip Wiki

Персональный wiki-сайт

Инструменты пользователя

Инструменты сайта


docs:powershell:modules:polaris

Polaris

Обзор

Кросс-платформенный, минималистичный веб-фреймворк для PowerShell

Ссылки

Установка

PS > Install-Module Polaris

Пример работы

New-PolarisGetRoute -Path "/helloworld" -Scriptblock {
    $Response.Send('Hello World!');
}
 
Start-Polaris

Советы

HTTPS (SSL)

Работа по HTTPS1)

Генерируем сертификат

$GUID = New-GUID | Select-Object -ExpandProperty GUID
$HostName = 'localhost'
$Port = '443'
 
#Create Cert
$Cert = New-SelfSignedCertificate -DnsName $HostName -CertStoreLocation cert:\LocalMachine\MY
#Bind cert to port
#Invoke-Expression -Command "netsh http add sslcert ipport=0.0.0.0:$($Port) certhash=$($Cert.Thumbprint) appid='$($AppID)' certstorename=MY"
Add-NetIPHttpsCertBinding -CertificateHash $Cert.Thumbprint -ApplicationId "{$GUID}" -IpPort "0.0.0.0:$Port" -CertificateStoreName My -NullEncryption:$false
# просмотр существующих http-сертификатов
netsh http show sslcert
# удаление
netsh http delete sslcert ipport=0.0.0.0:$Port

Запускаем Polaris

New-PolarisGetRoute -Path "/HelloWorld" -Scriptblock {
 
    $Respone = "Hello World"
 
    $Response.Send($Respone);
}
 
Start-Polaris -Port $Port -Https

ещё один вариант работы (импорта) с сертификатом

$CertificatePassword = "PasswordToDecryptCertificate" |
ConvertTo-SecureString -AsPlainText -Force
 
$CertificateImport = Import-PfxCertificate -FilePath "$Local\Certificate.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password $CertificatePassword
 
$GUID = New-GUID | Select-Object -ExpandProperty GUID
Add-NetIPHttpsCertBinding -CertificateHash $CertificateImport.Thumbprint -ApplicationId "{$GUID}" -IpPort "0.0.0.0:$Port" -CertificateStoreName My -NullEncryption:$false

Запуск как службы

Polaris Script

Path: C:\Scripts\Polaris.ps1

Import-Module C:\Polaris\Polaris.psm1
New-WebRoute -Path "/helloworld" -Method "GET" -ScriptBlock {
    $response.Send('Hello World');
}
Start-Polaris
 
while ($Polaris.Listener.IsListening) {
   Wait-Event callbackeventbridge.callbackcomplete
}

NSSM Script

function Install-Service {
    Param(
        [string]$nssmPath = '.',
        [string]$Name,
        [string]$Description,
        [string]$Executable,
        [string]$Arguments
    )
 
    $nssm = Join-Path -Path $nssmPath -ChildPath 'nssm.exe'
    & $nssm install $name $executable $arguments
    $null = & $nssm set $name Description $description
    Start-Service $name
}
 
Install-Service -Name Polaris -Description 'PowerShell HTTP API Service' -Executable pwsh.exe -Arguments '-ExecutionPolicy Bypass -Command C:\Scripts\Polaris.ps1'

Test

Invoke-RestMethod -Uri http://localhost:8080/helloworld -Method GET

Чуть подробнее про тоже самое тут: https://4sysops.com/archives/how-to-run-a-powershell-script-as-a-windows-service

Можно ещё попробовать вот так, без внешних утилит:

New-Service -Name "Polaris" -Description 'PowerShell HTTP API Service' -BinaryPathName "powershell.exe -ExecutionPolicy Bypass -Command C:\Scripts\Polaris.ps1"

Разное

Вроде как с модулем PSHTML можно подружиться и будет круто… Много примеров здесь.

docs/powershell/modules/polaris.txt · Последнее изменение: 04.06.2022 11:22 — philip

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki