Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepowershell
linenumberstrue
#Url 및 api 주소
$base_url = ""

# admin 계정 정보
$user =""
$password = ""
$groupname = "test"
$total_list = 10000
$LogArray = @()
# SSL 인증서 오류 일 경우, SSL 인증서 문제 없을 경우 해당 부분 필요 X
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
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls13, [Net.SecurityProtocolType]::Ssl3

# 인증 인코딩
function Get-BasicAuthCreds {
    param([string]$Username, [string]$Password)
    $AuthString = "{0}:{1}" -f $Username,$Password
    $AuthBytes  = [System.Text.Encoding]::ASCII.GetBytes($AuthString)
    return [Convert]::ToBase64String($AuthBytes)
}
$BasicCreds = Get-BasicAuthCreds -Username $user -Password $password;
$basicAuthValue = "Basic $BasicCreds"
$Headers = @{
 Authorization = $basicAuthValue
}

$LogArray = @()
$number = 0
while($number -le (${total_list}+1000)){
    $get_user_list = "/rest/api/2/user/search?username=.&includeInactive=true&startAt=${number}&maxResults=1000"
    $get_filter = Invoke-WebRequest -Uri "${base_url}${get_user_list}" -Method Get -ContentType 'application/json' -Headers $Headers
    if($get_filter -ne $null){
        $filter_name = ($get_filter.Content | ConvertFrom-Json).name
        $filter_name | ForEach-Object{
            $group_api = "/rest/api/2/group/user?groupname=${group_name}"
            $adduser = $_
            #Group 추가 
                try{
                    $form1 = '{"name": "' + ${adduser} + '"}'
                    $form = [System.Text.Encoding]::UTF8.GetBytes($form1)
                    $group_add = Invoke-WebRequest -Uri "${base_url}${group_api}" -Method Post -ContentType 'application/json' -Headers @{"Authorization"="Basic $BasicCreds"} -Body $form
                    $LogArray += "${adduser}: ${group_name}에 추가했습니다. "
                } catch{
                    $LogArray += "${adduser}: ${group_name}에 이미 있습니다. "
                }
        }
    }
    $number += 1000
}
$LogArray

...