Tests/CredentialsTest.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
 <#
    .Synopsis
       Template
    .DESCRIPTION
       This template will load $PSDefaultParameterValues and the PSJumpStart module
       and has support for Write-Verbose, -WhatIf and whatnot.
    .Notes
       Author date
       Changes
#>

[CmdletBinding(SupportsShouldProcess = $True)]
param ()

#region local functions
function Get-LocalDefaultVariables {
   [CmdletBinding(SupportsShouldProcess = $False)]
   param(
       [parameter(Position=0,mandatory=$true)]
       $CallerInvocation,
       [switch]$defineNew,
       [switch]$overWriteExisting
   )
   foreach($settingsFile in (Get-SettingsFiles $CallerInvocation ".json")) {        
       if (Test-Path $settingsFile) {        
           Write-Verbose "Reading file: [$settingsFile]"
           $DefaultParamters = Get-Content -Path $settingsFile -Encoding UTF8 | ConvertFrom-Json
           ForEach($prop in $DefaultParamters | Get-Member -MemberType NoteProperty) {        
               
               if (($prop.Name).IndexOf(':') -eq -1) {
                   $key=$prop.Name
                   $var = Get-Variable $key -ErrorAction SilentlyContinue
                   $value = $DefaultParamters.($prop.Name)                    
                   if (!$var) {
                       if ($defineNew) {
                           Write-Verbose "New Var: $key" 
                           if ($value.GetType().Name -eq "String" -and $value.SubString(0,1) -eq '(') {
                               $var = New-Variable -Name  $key -Value (Invoke-Expression $Value) -Scope 1
                           } else {
                               $var = New-Variable -Name  $key -Value $value -Scope 1
                           }
                       }
                   } else {

                       #We only overwrite non-set values if not forced
                       if (!($var.Value) -or $overWriteExisting)
                       {
                           try {                
                               Write-Verbose "Var: $key" 
                               if ($value.GetType().Name -eq "String" -and $value.SubString(0,1) -eq '(') {
                                   $var.Value = Invoke-Expression $value
                               } else {
                                   $var.Value = $value
                               }
                           } Catch {
                               $ex = $PSItem
                               $ex.ErrorDetails = "Err adding $key from $settingsFile. " + $PSItem.Exception.Message
                               throw $ex
                           }
                       }
                   }
               }
           }
       } else {
           Write-Verbose "File not found: [$settingsFile]"
       }

   }
}
#endregion

#region Init
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
if (-not (Get-Module PSJumpStart)) {
   Import-Module PSJumpStart -Force -MinimumVersion 1.2.1
}

Get-LocalDefaultVariables $MyInvocation -defineNew -overWriteExisting

#Get global deafult settings when calling modules
$PSDefaultParameterValues = Get-GlobalDefaultsFromJsonFiles $MyInvocation -Verbose:$VerbosePreference

#endregion

Msg "Start Execution"

#Cred files path is set in the json file for this test script
$Credentials=Get-AccessCredential -AccessName "Office365"

Msg ("User name 4 access: " + $Credentials.UserName)

Msg ("Credential was/is saved here " + $PSDefaultParameterValues["Get-AccessCredential:CredFilesPath"] + ":")

Msg (Get-ChildItem -Path ($PSDefaultParameterValues["Get-AccessCredential:CredFilesPath"] + "\" + $env:ComputerName + "*.xml"))

Msg "End Execution"