카테고리 없음

[Powershell] Invoke-WebRequest SSL/TLS 오류 무시

로파이 2023. 7. 22. 23:15
Add-Type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Invoke-WebRequest : 기본 연결이 닫혔습니다. SSL/TLS 보안 채널에 대한 트러스트 관계를 설정할 수 없습니다.

- SSL 인증서 문제로 나타나는 ps 오류를 무시한다.

# Replace these variables with your OpenSearch endpoint and index name
$OpenSearchEndpoint = "https://localhost:9200"
$IndexName = "dev-log"
$Username = "admin"
$Password = ConvertTo-SecureString -String "admin" -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
Add-Type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

# Read index settings and mappings from a local JSON file
$CurrentDirectory = Get-Location
$IndexSettingsFileName = "dev-log_mappings.json"
$IndexSettingsFilePath = Join-Path $CurrentDirectory $IndexSettingsFileName
$IndexSettings = Get-Content -Path $IndexSettingsFilePath | ConvertFrom-Json

Write-Host $IndexSettingsFilePath
Write-Host ($IndexSettings | ConvertTo-Json)

# Create the index with settings and mappings
$CreateIndexUrl = "$OpenSearchEndpoint/$IndexName"
$CreateIndexHeaders = @{
    "Content-Type" = "application/json"
}

# Create Index
Invoke-WebRequest -UseBasicParsing -Uri $CreateIndexUrl -Method Put -Headers $CreateIndexHeaders -Credential $Cred -Body $IndexSettings