-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_Template.ps1
382 lines (312 loc) · 16 KB
/
_Template.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<#
.SYNOPSIS
This script does magic things
.DESCRIPTION
This script does magic things using powershell
.NOTES
File-Name: _template.ps1
Author: Josh Burkard - josh@burkard.it
Version: 1.0.0
Changelog:
1.0.0, 2020-04-23, Josh Burkard, initial creation
.PARAMETER xyz
.LINK
https://github.com/joshburkard/___PowerShell-Functions
.EXAMPLE
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[String]$xyz
)
try {
#region declarations
#region get current path of the started script file
switch ( $ExecutionContext.Host.Name ) {
"ConsoleHost" { Write-Verbose "Runbook is executed from PowerShell Console"; if ( [boolean]$MyInvocation.ScriptName ) { if ( ( $MyInvocation.ScriptName ).EndsWith( ".psm1" ) ) { $CurrentFile = [System.IO.FileInfo]$Script:MyInvocation.ScriptName } else { $CurrentFile = [System.IO.FileInfo]$MyInvocation.ScriptName } } elseif ( [boolean]$MyInvocation.MyCommand ) { if ( [boolean]$MyInvocation.MyCommand.Source ) { if ( ( $MyInvocation.MyCommand.Source ).EndsWith( ".psm1" ) ) { $CurrentFile = [System.IO.FileInfo]$Script:MyInvocation.MyCommand.Source } else { $CurrentFile = [System.IO.FileInfo]$MyInvocation.MyCommand.Source } } else { $CurrentFile = [System.IO.FileInfo]$MyInvocation.MyCommand.Path } } }
"Visual Studio Code Host" { Write-Verbose 'Runbook is executed from Visual Studio Code'; If ( [boolean]( $psEditor.GetEditorContext().CurrentFile.Path ) ) { Write-Verbose "c"; $CurrentFile = [System.IO.FileInfo]$psEditor.GetEditorContext().CurrentFile.Path } else { if ( ( [System.IO.FileInfo]$MyInvocation.ScriptName ).Extension -eq '.psm1' ) { Write-Verbose "d1"; $PSCallStack = Get-PSCallStack; $CurrentFile =[System.IO.FileInfo] @( $PSCallStack | Where-Object { $_.ScriptName -match '.ps1'} )[0].ScriptName } else { Write-Verbose "d2"; $CurrentFile = [System.IO.FileInfo]$MyInvocation.scriptname } } }
"Windows PowerShell ISE Host" { Write-Verbose 'Runbook is executed from ISE'; Write-Verbose " CurrentFile"; $CurrentFile = [System.IO.FileInfo]( $psISE.CurrentFile.FullPath ) }
}
$CurrentPath = $CurrentFile.Directory.FullName
#endregion get current path of the started script file
#endregion declarations
#region functions
function Write-Log {
<#
.SYNOPSIS
Write to a Log File
.DESCRIPTION
Write to a Log File
.NOTES
File-Name: write-Log.ps1
Author: Josh Burkard - josh@burkard.it
Version: 1.0.0
Changelog:
1.0.0, 2020-04-23, Josh Burkard, initial creation
.PARAMETER Status
the status of the message.
this string parameter is mandatory and allows one off this values:
'INFO', 'WARN', 'ERROR', 'VERBOSE', 'OK', 'END', 'START'
.PARAMETER Message
the message to display
this string parameter is mandatory
.PARAMETER LogName
the file of the log
this string parameter is mandatory, it can be submitted through $PSDefaultParameterValues
.PARAMETER SubStepLevel
the level of the indentation
default is 0
this integer parameter is not mandatory
.PARAMETER NoOutput
if this switch parameter is set, there will be no console output. the logged message will only be writen to the logfile
.LINK
https://github.com/joshburkard/___PowerShell-Functions
.EXAMPLE
$PSDefaultParameterValues = @{}
$PSDefaultParameterValues.Add( "Write-Log:LogName", "c:\Admin\Logs\$Package-$Program-$( Get-Date -Format "yyyyMMdd-HHmmss" ).log" )
Write-Log -Message "Test Message" -Status INFO
Write-Log -Message "Test Message 1" -Status OK -SubStepLevel 1
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidateSet('INFO', 'WARN', 'ERROR', 'VERBOSE', 'OK', 'END', 'START')]
[Alias('Severity')]
[string]
$Status,
[parameter(Mandatory=$true)]
[string]
$Message,
[string]
$LogName = "c:\Admin\Logs\OSDeployment.log"
,
[switch]$NoOutput
,
[Parameter(Mandatory=$false)]
[int]$SubStepLevel = 0
)
<# $objSMSTS = New-Object -ComObject Microsoft.SMS.TSEnvironment
$SMSTSLogPath = $objSMSTS.Value("_SMSTSLogPath")
if (Test-Path $SMSTSLogPath) {
$LogFile = $(Join-Path $SMSTSLogPath $LogName)
} #>
$LogFile = $LogName
$Path = Split-Path -Path $LogFile
if (!(Test-Path -Path "$Path")) {
[void]( New-Item -Type directory -Path "$Path" -Force )
}
if( -not ( Test-Path -Path $LogFile ) ) {
[void]( New-Item -Path $LogFile -ItemType File )
}
$WriteSuccess = $false
$LogMessage = $Message -Replace "((\033\[)(\d*)m)", ''
$retry = 0
do {
try {
$retry++
$stream = [System.IO.StreamWriter]::new($LogFile, $true, ([System.Text.Utf8Encoding]::new()))
$stream.WriteLine( "$( ( [System.DateTime]::Now ).ToString() ) $( $Status.PadRight(8, ' ').ToUpper() ) - $( ''.PadRight( ($SubStepLevel * 2) , ' ') )$LogMessage" )
$stream.close()
$WriteSuccess = $true
}
catch {
# Write-Host "." -ForegroundColor Yellow -NoNewline
Start-Sleep -Milliseconds 10
}
} until ( ( $WriteSuccess -eq $true ) -or ( $retry -ge 5 ) )
if ( $WriteSuccess -eq $false ) {
try {
"$( ( [System.DateTime]::Now ).ToString() ) $( $Status.PadRight(8, ' ').ToUpper() ) - $( ''.PadRight( ($SubStepLevel * 2) , ' ') )$LogMessage" | Out-File -FilePath $LogFile -Encoding utf8 -Append
$WriteSuccess = $true
}
catch {
Write-Host "couldn't write to log" -ForegroundColor Red
}
}
Switch ($Status) {
'Info' {$FColor='gray'}
'Warning' {$FColor='yellow'}
'WARN' {$FColor='yellow'}
'Error' {$FColor='red'}
'Verbose' {$FColor='yellow'}
'Ok' {$FColor='green'}
Default {$FColor='gray'}
}
if ( $NoOutput -eq $false ) {
Write-Host "$(([System.DateTime]::Now).ToString()) [$($Status.PadRight(8, ' ').ToUpper())] - $( ''.PadRight( ($SubStepLevel * 2) , ' ') )$($Message)" -ForegroundColor $FColor
}
}
function Clear-LogFolder {
<#
.SYNOPSIS
Clears the Log Folder
.DESCRIPTION
Clears the Log Folder, remove the oldest files
if nothing else is defined, all files older than 14 days and all files older than the newest 1000 will be removed
.NOTES
File-Name: Clear-LogFolder.ps1
Author: Josh Burkard - josh@burkard.it
Version: 1.0.0
Changelog:
1.0.0, 2020-04-23, Josh Burkard, initial creation
.PARAMETER Path
the path to be cleaned
this path parameter is mandatory and allows only a path to a directory
.PARAMETER MaxDays
the max age of the files
this integer parameter is not mandatory, the default value is 14 days
.PARAMETER MaxCount
the max count of file to beleft after removing by MaxAge
the newest files will stay
this integer parameter is not mandatory, the default value is 1000
.LINK
https://github.com/joshburkard/___PowerShell-Functions
.EXAMPLE
Clear-LogFolder -Path "c:\Admin\Logs\"
.EXAMPLE
Clear-LogFolder -Path "c:\Admin\Logs\" -MaxDays 30
.EXAMPLE
Clear-LogFolder -Path "c:\Admin\Logs\" -MaxCount 500
.EXAMPLE
Clear-LogFolder -Path "c:\Admin\Logs\" -MaxDays 30 - MaxCount 250
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[System.IO.FileInfo]$Path # = 'c:\Admin\Logs\Get-EWSMail'
,
[int]$MaxDays = 14
,
[int]$MaxCount = 1000
)
if ( -Not ( Test-Path -Path $Path ) ) {
Write-Warning -Message "File or folder does not exist"
}
elseif ( ( Get-Item -Path $Path.FullName ) -isnot [System.IO.DirectoryInfo] ) {
Write-Warning -Message "Path must be a Directory"
}
else {
$AllChildItems = Get-ChildItem -Path $Path
$NewerThen = ( Get-Date ).AddDays( 0 - $MaxDays )
$ChildItems = $AllChildItems | Where-Object { ( Get-Date $_.LastWriteTime ) -ge $NewerThen }
$ChildItems = $ChildItems | Sort-Object LastWriteTime -Descending | Select-Object -First $MaxCount
$RemoveItems = $AllChildItems | Where-Object { $_.Name -notin $ChildItems.Name }
$RemoveItems | ForEach-Object { Remove-Item -Path $_.FullName -Confirm:$false -Force }
}
}
#endregion functions
#region execution
#region start logging
$PSDefaultParameterValues = @{}
# $PSDefaultParameterValues.Add( "Write-Log:LogName", "$( $CurrentPath )\Logs\$( $CurrentFile.BaseName )-$( Get-Date -Format "yyyyMMdd-HHmmss" ).log" )
$PSDefaultParameterValues.Add( "Write-Log:LogName", "C:\Admin\Logs\OSD\$( Get-Date -Format "yyyyMMdd-HHmmss" )-$( $CurrentFile.BaseName ).log" )
Write-Log -Message "started script $( $CurrentFile.Name )" -Status INFO
Write-Log -Message "started script as $( $env:USERDOMAIN )\$( $env:USERNAME )" -Status INFO
Write-Log -Message "write log to file $( $PSDefaultParameterValues.'Write-Log:LogName' )" -Status INFO -SubStepLevel 1
if ( [boolean]$PSBoundParameters.GetEnumerator() ) {
Write-Log "started with parameters:" -Status INFO -SubStepLevel 1
foreach ( $bp in $PSBoundParameters.GetEnumerator() ) {
Write-Log -Message "$( $bp.Key ) : $( $bp.Value )" -Status INFO -SubStepLevel 2
}
}
Clear-LogFolder -Path "$( $CurrentPath )\Logs" -MaxDays 30 -MaxCount 1000
#endregion start logging
#region import modules
Write-Log -Message 'import modules ...' -Status INFO
$NeededModules = @('CredentialManager')
foreach ( $NeededModule in $NeededModules) {
Write-Log -Message "${NeededModule} ..." -Status INFO -SubStepLevel 1
if ( -not ( Get-Module -ListAvailable -Name $NeededModule ) ) {
Write-Log -Message 'module not installed, trying to install it ...' -Status INFO -SubStepLevel 2
try {
Install-Module -Name $NeededModule -Scope AllUsers
Write-Log -Message 'module installed for all users' -Status OK -SubStepLevel 3
}
catch {
try {
Install-Module -Name $NeededModule -Scope CurrentUser
Write-Log -Message 'module installed for current user only' -Status OK -SubStepLevel 3
}
catch {
Write-Log -Message 'couldn''t install module' -Status ERROR -SubStepLevel 3
Exit 2
}
}
}
try {
Import-Module -Name $NeededModule
Write-Log -Message 'module imported' -Status OK -SubStepLevel 2
}
catch {
Write-Log -Message 'couldn''t import module' -Status OK -SubStepLevel 2
}
}
#endregion import modules
#region saving Credentials to Credentials Manager
Write-Log -Message 'saving Credentials to Credentials Manager ...' -Status INFO
<#
the cleartext password will be stored in a Credential Manager object instead clear text in this script. this has to be done in the context of the executing user.
if that's done once, at least the following 4 lines should be commented out and the password should be removed
#>
$Username_FJ_vCenter = "dika\srv_fj_osd"
$Password_FJ_vCenter = "..."
$Credentials_FJ_vCenter = New-Object System.Management.Automation.PSCredential -ArgumentList @($Username_FJ_vCenter,(ConvertTo-SecureString -String $Password_FJ_vCenter -AsPlainText -Force))
New-StoredCredential -Target "Fujitsu_DC_srv_fj_osd" -UserName $Username_FJ_vCenter -Password $Password_FJ_vCenter -Persist LocalMachine
#endregion saving Credentials to Credentials Manager
#region get Credentials from Credentials Manager
Write-Log -Message 'get Credentials from Credentials Manager ...' -Status INFO
$Credentials_FJ_vCenter = Get-StoredCredential -Target "Fujitsu_DC_srv_fj_osd"
#endregion get Credentials from Credentials Manager
Write-Verbose "This is only a verbose test"
#region create a handled error
try {
1/0
}
catch {
Write-Log -Message "invalid division by zero" -Status ERROR
Exit 500
}
#endregion create a handled error
#region end script
Write-Log -Message "script ended without failures" -Status OK
Exit 0
#endregion end script
#endregion execution
}
catch {
$err = $_
$Error.Clear()
Write-Output $err
$ScriptName = $err.InvocationInfo.ScriptName
$Content = $err.InvocationInfo.Line
if ( [boolean]$Content ) {
$Content = $Content.Trim()
}
$LineNumber = $err.InvocationInfo.ScriptLineNumber
$Col = $err.InvocationInfo.OffsetInLine
if ( Get-Command -Name Write-Log -ErrorAction SilentlyContinue ) {
Write-Log -Message "Error Occured at:" -Status ERROR
if ( [boolean]$ScriptName ) {
Write-Log -Message "ScriptName: $($ScriptName)" -Status ERROR -SubStepLevel 1
}
Write-Log -Message "LineNumber: $($LineNumber)" -Status ERROR -SubStepLevel 1
Write-Log -Message "Col: $($Col)" -Status ERROR -SubStepLevel 1
Write-Log -Message "Content: $($Content)" -Status ERROR -SubStepLevel 1
if ( [boolean]$err.Exception.Message ) {
Write-Log -Message $err.Exception.Message -Status ERROR -SubStepLevel 1
}
}
else {
Write-Output "Error Occured at:"
if ( [boolean]$ScriptName ) {
Write-Output " ScriptName: $($ScriptName)"
}
Write-Output " LineNumber: $($LineNumber)"
Write-Output " Col: $($Col)"
Write-Output " Content: $($Content)"
if ( [boolean]$err.Exception.Message ) {
Write-Output " $( $err.Exception.Message )"
}
}
Exit 1
}