install-eNLibModule.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
<#
.SYNOPSIS
    eNLib module installation script
.DESCRIPTION
    Copies module files to PATH diectory. by default choses user directory.
    use -useGlobalModuleFolder parameter to install for all users. requires to be run
    from elevated console.
 
.EXAMPLE
    .\install-eNLibModule.ps1
    copies module files to user module folder
.EXAMPLE
    .\install-eNLibModule.ps1 -useGlobalModuleFolder
    copies module files to global module folder. must be run in eveleted mode.
.INPUTS
    None.
.OUTPUTS
    None.
.LINK
    https://w-files.pl
.NOTES
    nExoR ::))o-
    version 210208
        last changes
        - 210208 run using relative path fix
        - 201019 initialized
#>


[CmdletBinding()]
param (
    [Parameter(mandatory=$false,position=0)]
        [switch]$useGlobalModuleFolder
)
if($useGlobalModuleFolder) {
    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    
    if(-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) ) {
        write-host 'you need to run console in elevated mode in order to copy module to global path' -ForegroundColor Red
        exit -1
    }
    $moduleFolder="C:\Program Files\WindowsPowerShell\Modules"

} else {
    $moduleFolder = [string]($Env:PSModulePath -split ';'|select-string $env:username)
    if( [string]::IsNullOrEmpty($moduleFolder) ) {
        write-host 'user module folder not defined. please change $PSModulePath variable to include your folder name
or use -useGlobalMOduleFolder parameter'
 -ForegroundColor Yellow
        write-host -ForegroundColor Red "not installed."
        exit -2
    }
}

remove-module -name eNLib -ErrorAction SilentlyContinue

$eNLibFolder="$moduleFolder\eNLib"
try {
    if(-not (test-path $eNLibFolder) ) {
        new-item -Type Directory $eNLibFolder|Out-Null 
    }
    $sourceFolder = get-item $MyInvocation.InvocationName
    Copy-Item -Path "$($sourceFolder.Directory.FullName)\*.psd1","$($sourceFolder.Directory.FullName)\*.psm1" -Destination $eNLibFolder
} catch {
    throw
}
Get-ChildItem $eNLibFolder
write-host "files copied to $eNLibFolder." -ForegroundColor Green