-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSModuleScripts.psm1
More file actions
98 lines (86 loc) · 3.95 KB
/
PSModuleScripts.psm1
File metadata and controls
98 lines (86 loc) · 3.95 KB
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
# Copyright (c) nOsliw Solutions. All rights reserved.
#
# THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
# CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
# Error default handling. Check Get-Help about_commonparameters
$ErrorActionPreference = "Stop" # Do not change. This shows all error upon import-module
#$VerbosePreference = "Continue"
$script:modulename = ($ExecutionContext.SessionState.Module).ToString()
$script:identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$script:IsAdmin = (New-Object Security.Principal.WindowsPrincipal $identity).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if (!$IsAdmin) {
$msg = "Problem`n`tThis $modulename module requires an Elevated Mode session environment.`n`tPlease re-import the module with an administrator elevated environment"
Write-Host -Object "$msg" -ForegroundColor Red
Break
}
# Clears all variables
Remove-Variable -Name __n* | Out-Null
# Global Variables
$path = Split-Path $MyInvocation.MyCommand.Path -Parent
$hash = New-Object -TypeName HashTable -ArgumentList @{
Module = $ExecutionContext.SessionState.Module
Name = $ExecutionContext.SessionState.Module.Name
Path = $path
Registry = New-Object -TypeName HashTable -ArgumentList @{
LocalMachine = "HKLM:\SOFTWARE\nOsliw Solutions\PSModules\$modulename"
CurrentUser = "HKCU:\SOFTWARE\nOsliw Solutions\PSModules\$modulename"
}
IsMaintenanceMode = $False
IsSystem = "$Env:USERNAME" -eq "$($Env:COMPUTERNAME)`$"
IsInModuleGroup = $True
IsInteractive = [Environment]::UserInteractive
IsPS3 = $PSVersionTable.PSVersion.Major -ge 3
HasExcel = $False
EventLogSource = "PowerShell Module " + $modulename.ToString().toUpper()
Settings = New-Object -TypeName HashTable -ArgumentList @{
InitializedCommon = $false
}
Invocation = New-Object -TypeName HashTable -ArgumentList @{
Arguments = ""
Identity = ""
Time = Get-Date
LastPrefix = ""
}
Errors = [System.Collections.ArrayList] @() # [Environment]::ExitCode, [Environment]::Exit(8)
Warnings = [System.Collections.ArrayList] @() # [Environment]::ExitCode, [Environment]::Exit(8)
ErrorsCmdlet = @{}
WarningsCmdlet = @{}
DoCmdOnRemove = [System.Collections.ArrayList] @()
DoPSHOnRemove = [System.Collections.ArrayList] @()
}
# Main Global variable
New-Variable -Name __nPSMS -Value $hash -Option AllScope -Scope Global -ErrorAction Stop -Description "Global Module variable" -Force
# Loading PowerShell Script files
$Files = Get-ChildItem -Path "$path\Scripts" -Recurse -Include "*.ps1" -Filter "*-n*"
ForEach ($File in $Files) {
try {
$FullName = $File.FullName
Write-Verbose -Message "Loading: $FullName"
. $FullName
}
catch [Exception] {
$Message = ($_.Exception.Message).ToString().Trim()
$Message = "Loading error file=$FullName, Message: $Message"
Write-Host -Object $Message -ForegroundColor Red
break
}
}
# Initializing the Module
Initialize-nCommon -InputObject $__nPSMS
$script:mObj = $MyInvocation.MyCommand.ScriptBlock.Module
$script:mName = $MyInvocation.MyCommand.ScriptBlock.Module.name
$mObj.OnRemove = {
if (Test-path -path Variable:\__nPSMS -PathType Leaf) {
$__nPSMS.Errors.Clear()
$__nPSMS.ErrorsCmdlet.Clear()
$__nPSMS.WarningsCmdlet.Clear()
$__nPSMS.DoCmdOnRemove.Clear()
$__nPSMS.DoPSHOnRemove.Clear()
$__nPSMS.Settings.Clear()
Remove-Variable -Name __n* -Force | Out-Null
}
$Error.Clear()
}