Functions/Get-SettingsFiles.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
function Get-SettingsFiles {
    <#
        .Synopsis
           Get a list of setting files
        .DESCRIPTION
           Using [System.Security.Principal.WindowsIdentity]::getCurrent() ths function returns a list of setting files with the following content:
     
            - File named as user LogonID in caller location or current location(?)
            - LogonDomain (or machine name) file at caller location
            - Caller settingsfile at caller location
            - LogonDoamin (or machine name) file at this PSM-mudules location
         
        .PARAMETER CallerInvocation
           The invocation object from the caller.
        .PARAMETER extension
           File name suffix to use.
    #>

    Param(
         [parameter(Position=0,mandatory=$true)]
         $CallerInvocation,
         [parameter(Position=1,mandatory=$true)]
         [string]$extension
    ) 
    
        $globalLocation =  $PSScriptRoot        
        $callerLocation = Split-Path -parent $CallerInvocation.MyCommand.Definition
    
        [reflection.assembly]::LoadWithPartialName("System.Security.Principal.WindowsIdentity") |Out-Null
        $user = [System.Security.Principal.WindowsIdentity]::getCurrent()    
        $UserID = ($user.Name -split '\\')[1]
        $LogonContext = ($user.Name -split '\\')[0]
        
        #Add local environment settingsfiles (user specific or domain/computer specific)
        #also script specific defaults (local vars??)
        $settingFiles = @(        
            "$callerLocation\$UserID$extension"
            "$callerLocation\$LogonContext$extension"
            ($CallerInvocation.MyCommand.Definition -replace ".ps1","") + "$extension"
            "$globalLocation\$LogonContext$extension"        
        )
    
        #Add module specific setting xml-files
        Get-Module | Select-Object -ExpandProperty Name | ForEach-Object {
            $settingFiles += "$globalLocation\$_$extension"
        }
        
        $settingFiles
    }