common/generic.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#Convert a text from the DOS format to the UNIX format.
#The format is different in the last character of each line.
#The DOS format ends with a carriage return (Cr) line feed (Lf)
#character whereas the UNIX format uses the line feed (Lf) character.
function ConvertTo-Unix
{
    begin
    {}
    process
    {
        ($_ | Out-String) -replace "`r`n","`n"
    }
    end
    {}
}

#Convert a text from the UNIX format to the DOS format.
#The format is different in the last character of each line.
#The DOS format ends with a carriage return (Cr) line feed (Lf)
#character whereas the UNIX format uses the line feed (Lf) character.
function ConvertFrom-Unix
{
    begin
    {}
    process
    {
        ($_ | Out-String) -replace "`n","`r`n"
    }
    end
    {}
}


function Set-StandardMembers
{
    # http://stackoverflow.com/questions/1369542/can-you-set-an-objects-defaultdisplaypropertyset-in-a-powershell-v2-script/1891215#1891215
    Param([PSObject]$MyObject,[String[]]$DefaultProperties)
        try
        {
            $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultProperties)
            $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
            $MyObject | Add-Member MemberSet PSStandardMembers $PSStandardMembers -Force
        }
        catch [Exception]
        {
            Write-Log -message $_ -Level EXCEPTION
            Write-Debug $_.Exception.Message
        }
}


# Teste Write permissions
function Test-Write {
    [CmdletBinding()]
    param (
        [parameter()] [ValidateScript({[IO.Directory]::Exists($_.FullName)})]
        [IO.DirectoryInfo] $Path
    )
    try {
        $testPath = Join-Path $Path ([IO.Path]::GetRandomFileName())
        [IO.File]::Create($testPath, 1, 'DeleteOnClose') > $null
        # Or...
        <# New-Item -Path $testPath -ItemType File -ErrorAction Stop > $null #>
        return $true
    } catch {
        return $false
    } finally {
        Remove-Item $testPath -ErrorAction SilentlyContinue -WhatIf:$false
    }
}
#.ExternalHelp ..\psKeePass.Help.xml
function New-ParamHistory
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)] 
            [String]$Function,
        [parameter(Mandatory=$true)] 
            [String]$Parameter,
        [parameter(Mandatory=$true)] 
            [String]$Content
    )
    try 
    {
        [Array]$paramHistory = Get-ParamHistory | select -First 5
        if (-not ($paramHistory | ? {($_.Parameter -eq $Parameter) -and ($_.Content -eq $Content)}) )
        {
            $item = "" | Select Function,Parameter,Content,DateTime
            $item.Function = $Function
            $item.Parameter = $Parameter
            $item.Content = $Content
            $item.DateTime = (Get-Date).DateTime
            $paramHistory += $item
            $null = New-Item -Path  (Join-Path -Path $Script:parent_appdata -ChildPath enviroment) -Name history.json -ItemType File -Value ($paramHistory | ConvertTo-Json) -Force
        }
    }
    catch [Exception]
    {
        Write-Log -message $_ -Level EXCEPTION
    }
}
#.ExternalHelp ..\psKeePass.Help.xml
function Get-ParamHistory
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$False)] 
            [String]$Function,
        [parameter(Mandatory=$False)] 
            [String]$Parameter
    )
    try 
    {
        $path = (Join-Path -Path $Script:parent_appdata -ChildPath enviroment/history.json)
        if (Test-Path -LiteralPath $path)
        {
            $output = Get-Content $path | ConvertFrom-Json
            $output = $output | sort -Property DateTime -Descending

            if ($Function)
            {
                $output = $output | ? {$_.Function -like $Function}
            }
            if ($Parameter)
            {
                $output = $output | ? {$_.Parameter -like $Parameter}
            }
            return $output
        }
        else
        {
            return $null
        }
    }
    catch [Exception]
    {
        Write-Log -message $_ -Level EXCEPTION
    }
}


Function Get-RandomPassword 
{
    Param($Length = 15, [Switch]$Complex)
    $chars = $letters = 65..90 + 97..122
    if ($Complex.IsPresent)
    {
        $chars += $punc = 33..33 + 35..38 + 40..43 + 45..46
        $chars += $digits = 48..57
    }
    # Thanks to
    # https://blogs.technet.com/b/heyscriptingguy/archive/2012/01/07/use-pow
    $password = get-random -count $length -input ($chars) | % `
            -begin { $aa = $null } `
            -process {$aa += [char]$_} `
            -end {$aa}

    return $password
}