SimplyCredential.psm1

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
Set-StrictMode -Version Latest
#$ErrorActionPreference = "Stop"

[string]$dataPath = "$home\AppData\Local\SimplyCredential"
$script:AppListPath = Join-Path $dataPath "AppList.clixml"
$script:CredListPath = Join-Path $dataPath "CredList.clixml"

#2018-12-03 changing folder location...
if(Test-Path "$home\AppData\Low\SimplyCredential") {
    Write-Warning "Found data stored in old path (AppData\Low\SimplyCredential), migrating to new path (AppData\Local\SimplyCredential)!"
    Move-Item -Path "$home\AppData\Low\SimplyCredential" -Destination $dataPath
}

if(-not (Test-Path -Path $dataPath)) { New-Item -Path $dataPath -ItemType Directory -Force | Out-Null }

if(Test-Path $Script:AppListPath) { $script:AppList = Import-Clixml $script:AppListPath }
else { $script:AppList = @{} }

if(Test-Path $Script:CredListPath) { $script:CredList = Import-Clixml $script:CredListPath }
else { $script:CredList = @{} }

$CreateProcessWithLogonW = @"
    [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
       public static extern bool CreateProcessWithLogonW(
          String userName,
          String domain,
          String password,
          int logonFlags,
          String applicationName,
          String commandLine,
          int creationFlags,
          int environment,
          String currentDirectory,
          ref StartupInfo startupInfo,
          out ProcessInformation processInformation);
 
    [StructLayout(LayoutKind.Sequential)]
    public struct ProcessInformation
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessId;
        public int dwThreadId;
    }
     
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct StartupInfo
    {
        public Int32 cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public Int32 dwX;
        public Int32 dwY;
        public Int32 dwXSize;
        public Int32 dwYSize;
        public Int32 dwXCountChars;
        public Int32 dwYCountChars;
        public Int32 dwFillAttribute;
        public Int32 dwFlags;
        public Int16 wShowWindow;
        public Int16 cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }
"@


Add-Type -MemberDefinition $CreateProcessWithLogonW -Namespace SimplyCredential -Name PInvoke

#Load up base Classes

#Dot source functions
ForEach($f in Get-ChildItem $PSScriptRoot -filter "*.ps1" -File) {
    Try { . $f.fullname }
    Catch { Write-Error "Failed to import function $($f.fullname): $_" }
}


# Register ArgumentCompleter
Register-ArgumentCompleter -CommandName @("Use-Credential", "Save-Credential", "Remove-Credential", "Show-Credential") -ParameterName Name -ScriptBlock {
    Param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
    $script:CredList.Keys | 
        Where-Object { $_ -like "$wordToComplete*" } |
        ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', ("UserName: {0}" -f $script:CredList[$_].UserName))
        }
}

Register-ArgumentCompleter -CommandName @("Use-Application", "Save-Application", "Remove-Application", "Show-Application") -ParameterName Name -ScriptBlock {
    Param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
    $script:AppList.Keys | 
        Where-Object { $_ -like "$wordToComplete*" } |
        ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $script:AppList[$_].Path) }
}

# Clean variables
Remove-Variable f, CreateProcessWithLogonW, dataPath