YandexPdd.psm1

Function Set-YandexPddCredential {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$YandexPddDomain,
        [Parameter(Mandatory=$true)][string]$YandexPddToken,
        [switch]$Save
    )

    $Global:YandexPddCredential = [ordered]@{
        'domain' = $YandexPddDomain
        'Token' = $YandexPddToken
    }
        
    if($Save){
        $CredString = '$Global:YandexPddCredential = @{"domain"="_domain_";"Token"="_Token_"}' -replace '_domain_',$YandexPddDomain -replace '_Token_',$YandexPddToken
        $FilePath = "$env:APPDATA\Microsoft\Windows\PowerShell\YandexPdd\Token"
        if(!(Test-Path $FilePath)){New-Item -Force -Type File -Path $FilePath}
        Out-File -FilePath $FilePath -Encoding UTF8 -InputObject $CredString 
    }
}

Function Import-YandexPddCredential {
    $FilePath = "$env:APPDATA\Microsoft\Windows\PowerShell\YandexPdd\Token"
    if((Test-Path $FilePath) -and !$Global:YandexPddCredential){Get-Content -Encoding UTF8 -Path $FilePath | Invoke-Expression}
    if(!$Global:YandexPddCredential){Set-YandexPddCredential}
}

# YandexDomain

Function Get-YandexDomainDetails {

    [CmdletBinding()]
    param()
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/domain/details'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method GET -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
        } catch {Write-Error -Message "Request Error";break}
    $Json
    
}

# YandexDNSrecord

Function Add-YandexDNSrecord {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][ValidateSet("SRV","TXT","NS","MX","SOA","A","AAAA","CNAME")][string]$type,
        [string]$admin_mail,
        [Parameter(Mandatory=$true)][string]$content,
        [int]$priority = 10,
        [int]$weight,
        [ValidateRange(1,65534)][string]$port,
        [string]$target,
        [string]$subdomain,
        [ValidateRange(900,1209600)][int]$ttl = 21600
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/dns/add'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.type = $type
    $Body.admin_mail = $admin_mail
    $Body.content = $content
    $Body.priority = $priority
    $Body.weight = $weight
    $Body.port = $port
    $Body.target = $target
    $Body.subdomain = $subdomain
    $Body.ttl = $ttl
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json.record
}

Function Get-YandexDNSrecord {

    [CmdletBinding()]
    param(
        [string]$FilterRegex = '.+'
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/dns/list?'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method GET -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json.records | ? {@($_.content,$_.fqdn,$_.record_id,$_.subdomain,$_.type) | Select-String -Pattern $FilterRegex -Quiet}
    
}

Function Set-YandexDNSrecord {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][int64]$record_id,
        [string]$subdomain,
        [ValidateRange(900,1209600)][int]$ttl = 21600,
        [ValidateRange(900,86400)][int]$refresh = 10800,
        [ValidateRange(90,3600)][int]$retry = 900,
        [ValidateRange(90,3600)][int]$expire = 900,
        [ValidateRange(90,86400)][int]$neg_cache = 10800,
        [string]$admin_mail,
        [Parameter(Mandatory=$true)][string]$content,
        [int]$priority = 10,
        [string]$port,
        [int]$weight,
        [string]$target
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/dns/edit'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.record_id = $record_id
    $Body.subdomain = $subdomain
    $Body.ttl = $ttl
    $Body.refresh = $refresh
    $Body.retry = $retry
    $Body.expire = $expire
    $Body.neg_cache = $neg_cache
    $Body.admin_mail = $admin_mail
    $Body.content = $content
    $Body.priority = $priority
    $Body.port = $port
    $Body.weight = $weight
    $Body.target = $target
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    
}

Function Remove-YandexDNSrecord {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][int64]$record_id
    )
    
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/dns/del'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.record_id = $record_id
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    
}

# YandexMailbox

Function Add-YandexMailbox {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$login,
        [Parameter(Mandatory=$true)][string]$password
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/add'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.login = $login
    $Body.password = $password
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json
}

Function Get-YandexEmailList {

    [CmdletBinding()]
    param(
        [string]$FilterRegex = '.+',
        [ValidateRange(1,1000)][int]$on_page = 100
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/list'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.page = 1
    $Body.on_page = $on_page
    
    do{    
        try{
            $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method GET -Headers $Headers -Body $Body
            $Json = $InvokeWebRequest.Content | ConvertFrom-Json
            if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
            [int]$FoundCounter += $Json.found
            if($Json.total -ne 0){
                Write-Progress -Activity "Page $($Json.page) from $($Json.pages) pages" -Status "Found $FoundCounter from $($Json.total) accounts"
            }
            $Body.page += 1
        } catch {Write-Error -Message "Request Error";break}

        $Json.accounts | ? {@($_.uid,$_.iname,$_.fname,$_.aliases,$_.fname,$_.login,$_.fio) | Select-String -Pattern $FilterRegex -Quiet}

    }while($Json.page -le $Json.pages)
}

Function Set-YandexMailbox {

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string]$login,
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][int64]$uid,
        [ValidateLength(6,20)][string]$password,
        [string]$iname,
        [string]$fname,
        [ValidateSet("yes","no")][string]$enabled,
        [ValidatePattern("^[0-9]{4}(-[0-9]{2}){2}$")][string]$birth_date,
        [ValidateSet("0","1","2")][string]$sex,
        [string]$hintq,
        [string]$hinta
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/edit'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.login = $login
    $Body.uid = $uid
    $Body.password = $password
    $Body.iname = $iname
    $Body.fname = $fname
    $Body.enabled = $enabled
    $Body.birth_date = $birth_date
    $Body.sex = $sex
    $Body.hintq = $hintq
    $Body.hinta = $hinta
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    
    
}

Function Remove-YandexMailbox {

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string]$login,
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][int64]$uid
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/del'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.login = $login
    $Body.uid = $uid
    
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    
    
}

# YandexMailgroup

Function Add-YandexMailgroup {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$maillist
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/ml/add'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.maillist = $maillist

    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json
}

Function Get-YandexMailgroup {

    [CmdletBinding()]
    param(
        [string]$FilterRegex = '.+'
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/ml/list?'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain

    
        try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method GET -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json.maillists
}

Function Remove-YandexMailgroup {

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string]$maillist,
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][alias("uid")][int64]$maillist_uid
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/ml/del'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.maillist = $maillist
    $Body.maillist_uid = $maillist_uid
    
    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    
    
}

# YandexMailgroupMember

Function Add-YandexMailgroupMember {

    [CmdletBinding()]
    param(
        [string]$maillist,
        [int64]$maillist_uid,
        [string]$subscriber,
        [int64]$subscriber_uid,
        [string]$can_send_on_behalf
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/ml/subscribe'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.maillist = $maillist
    $Body.maillist_uid = $maillist_uid
    $Body.subscriber = $subscriber
    $Body.subscriber_uid = $subscriber_uid
    $Body.can_send_on_behalf = $can_send_on_behalf

    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json
}

Function Get-YandexMailgroupMember {

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string]$maillist,
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][alias("uid")][string]$maillist_uid,
        [string]$FilterRegex = '.+'
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/ml/subscribers?'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.maillist = $maillist
    $Body.maillist_uid = $maillist_uid

    
        try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method GET -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json.maillists
}

Function Remove-YandexMailgroupMember {

    [CmdletBinding()]
    param(
        [string]$maillist,
        [int64]$maillist_uid,
        [string]$subscriber,
        [int64]$subscriber_uid
    )
    if(!$Global:YandexPddCredential){Import-YandexPddCredential}
    
    $pddimpUrl = 'https://pddimp.yandex.ru'
    $api = '/api2/admin/email/ml/unsubscribe'
    $URL = "$pddimpUrl"+"$api"
    $Headers = @{}
    $Headers.PddToken = $YandexPddCredential.Token
    $Body = @{}
    $Body.domain = $YandexPddCredential.domain
    $Body.maillist = $maillist
    $Body.maillist_uid = $maillist_uid
    $Body.subscriber = $subscriber
    $Body.subscriber_uid = $subscriber_uid

    try{
        $InvokeWebRequest = Invoke-WebRequest -URI $URL -Method POST -Headers $Headers -Body $Body
        $Json = $InvokeWebRequest.Content | ConvertFrom-Json
        if($Json.success -ne 'ok'){Write-Error -Message $Json.error;break}
    } catch {Write-Error -Message "Request Error";break}
    $Json
}