functions/CouchDBprivate.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Private functions of CouchDB module

function ConvertTo-CouchDBPassword ([SecureString] $SecurePassword) {
    <#
    .SYNOPSIS
    Convert SecureString.
    .DESCRIPTION
    Convert to SecureString to simple string.
    .PARAMETER SecurePassword
    Password format in [SecureString].
    .EXAMPLE
    $password = "password" | ConvertTo-SecureString -AsPlainText -Force
    ConvertTo-CouchDBPassword -SecurePassword $password
    This example return a readable password.
    .LINK
    https://pscouchdb.readthedocs.io/en/latest/permission.html#create-admin-user
    #>

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
    $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    return $UnsecurePassword
}

function Register-TemporaryEvent () {
    <#
    .SYNOPSIS
    Registers an event action for an object, and automatically unregisters
    itself afterward. In PowerShell version three, use the -MaxTriggerCount
    parameter of the Register-*Event cmdlets.
    .DESCRIPTION
    Registers an event action for an object, and automatically unregisters
    itself afterward. In PowerShell version three, use the -MaxTriggerCount
    parameter of the Register-*Event cmdlets.
    .PARAMETER Object
    Job object to execute in background.
    .PARAMETER Event
    Event catched.
    .PARAMETER Action
    ScriptBlock powershell commands to post execution.
    .EXAMPLE
    PS > $timer = New-Object Timers.Timer
    PS > Register-TemporaryEvent $timer Disposed { [Console]::Beep(100,100) }
    PS > $timer.Dispose()
    PS > Get-EventSubscriber
    PS > Get-Job
    .LINK
    https://pscouchdb.readthedocs.io/en/latest/documents.html#get-a-bulk-documents
    #>

    param(
        $Object,
        $EventName,
        [ScriptBlock] $Action
    )

    $actionText = $Action.ToString()
    $actionText += @'
    $eventSubscriber | Unregister-Event
    $eventSubscriber.Action | Remove-Job
'@


    $eventAction = [ScriptBlock]::Create($actionText)
    $null = Register-ObjectEvent $Object $EventName -Action $eventAction

}