-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-GitConfig.ps1
More file actions
373 lines (318 loc) · 13.6 KB
/
Copy pathSet-GitConfig.ps1
File metadata and controls
373 lines (318 loc) · 13.6 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
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
#region declaration
$ProxyServer = "aproxy.corproot.net"
$ProxyPort = 8080
#endregion declaration
#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', 'Warning', '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', 'Warning', '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
$retry = 0
do {
try {
$retry++
Add-Content -Path "$($LogFile)" -Value "$(([System.DateTime]::Now).ToString()) $( $Status.PadRight(8, ' ').ToUpper() ) - $Message" -ErrorAction Stop
$WriteSuccess = $true
}
catch {
# Write-Host "." -ForegroundColor Yellow -NoNewline
Start-Sleep -Milliseconds 10
}
} until ( ( $WriteSuccess -eq $true ) -or ( $retry -ge 5 ) )
if ( $WriteSuccess -eq $false ) {
Write-Host "couldn't write to log" -ForegroundColor Red
}
Switch ($Status) {
'Info' {$FColor='gray'}
'Warning' {$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())] - $($Message)" -ForegroundColor $FColor
}
}
function Start-ProcessAdv {
<#
.SYNOPSIS
This function executes an executable file with parameters and and captures its exit code, stdout and stderr.
.DESCRIPTION
This function executes an executable file with parameters and and captures its exit code, stdout and stderr.
.PARAMETER FilePath
the full path to the executable file
only for compatibility with old versions of this function, the parameter has an alias to FileName. do not use this alias in new scripts.
.PARAMETER ArgumentList
the arguments which should be transmitted to the executable process
only for compatibility with old versions of this function, the parameter has an alias to Arguments. do not use this alias in new scripts.
.PARAMETER VERB
should the process run as Administrator
.PARAMETER TimeOut
TimeOut in seconds
.PARAMETER WorkingDirectory
the directory in which the procss should work
.EXAMPLE
Start-ProcessAdv -FilePath 'git' -ArgumentList 'status' -TimeOut 60
#>
[OutputType('PSCustomObject')]
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Alias('FileName')]
[String]$FilePath
,
[Parameter(Mandatory=$false)]
[Alias('Arguments')]
[String[]]$ArgumentList
,
[Parameter(Mandatory=$false)]
[ValidateSet('','runas')]
[String]$Verb = ''
,
[Parameter( Mandatory = $false)]
[int]$TimeOut
,
[Parameter( Mandatory = $false)]
[string]$WorkingDirectory = $null
,
[Parameter( Mandatory = $false)]
[System.Management.Automation.PSCredential]$Credential
)
# Setting process invocation parameters.
$ProcessStartInfo = New-Object -TypeName System.Diagnostics.ProcessStartInfo
$ProcessStartInfo.CreateNoWindow = $true
$ProcessStartInfo.UseShellExecute = $false
$ProcessStartInfo.RedirectStandardOutput = $true
$ProcessStartInfo.RedirectStandardError = $true
$ProcessStartInfo.FileName = $FilePath
if ( [boolean]$WorkingDirectory ) {
$ProcessStartInfo.WorkingDirectory = $WorkingDirectory
}
if (! [String]::IsNullOrEmpty($ArgumentList)) {
$ProcessStartInfo.Arguments = $ArgumentList
}
if ( -not [String]::IsNullOrEmpty( $Verb ) ) {
$ProcessStartInfo.Verb = $Verb
}
if ( [boolean]$Credential ) {
$ProcessStartInfo.UserName = $Credential.GetNetworkCredential().UserName
$ProcessStartInfo.Domain = $Credential.GetNetworkCredential().Domain
$ProcessStartInfo.Password = $Credential.Password
}
# Creating process object.
$Process = New-Object -TypeName System.Diagnostics.Process
$Process.StartInfo = $ProcessStartInfo
# Creating string builders to store stdout and stderr.
$StdOutBuilder = New-Object -TypeName System.Text.StringBuilder
$StdErrBuilder = New-Object -TypeName System.Text.StringBuilder
# Adding event handers for stdout and stderr.
$ScripBlock = {
if ( -not [String]::IsNullOrEmpty( $EventArgs.Data ) ) {
$Event.MessageData.AppendLine( $EventArgs.Data )
}
}
$StdOutEvent = Register-ObjectEvent -InputObject $Process `
-Action $ScripBlock -EventName 'OutputDataReceived' `
-MessageData $StdOutBuilder
$StdErrEvent = Register-ObjectEvent -InputObject $Process `
-Action $ScripBlock -EventName 'ErrorDataReceived' `
-MessageData $StdErrBuilder
# Starting process.
[Void]$Process.Start()
$Process.BeginOutputReadLine()
$Process.BeginErrorReadLine()
if ( [boolean]$TimeOut ) {
[Void]$Process.WaitForExit( $TimeOut * 1000 )
}
else {
[Void]$Process.WaitForExit()
}
# Unregistering events to retrieve process output.
Unregister-Event -SourceIdentifier $StdOutEvent.Name
Unregister-Event -SourceIdentifier $StdErrEvent.Name
$Result = New-Object -TypeName PSObject -Property ( [Ordered]@{
"ExeFile" = $FilePath;
"Parameters" = $ArgumentList -join " ";
"ExitCode" = $Process.ExitCode;
"StdOut" = $StdOutBuilder.ToString().Trim();
"StdErr" = $StdErrBuilder.ToString().Trim()
} )
return $Result
}
function Invoke-GITCommand {
Param (
[Parameter(Mandatory=$true)]
[string]$Command
,
[Parameter(Mandatory=$false)]
[string]$Message
,
[Parameter(Mandatory=$false)]
[int]$SubStepLevel = 0
,
[Parameter(Mandatory=$false)]
[string]$WorkingDirectory
,
[Parameter(Mandatory=$false)]
[switch]$Passthru
,
[Parameter(Mandatory=$false)]
[switch]$NoLog
)
$ParamsGeneral = @{}
$ParamsLevel = @{}
if ( [boolean]$Message ) {
$ParamsGeneral.Add( 'Message', $Message )
}
else {
$ParamsGeneral.Add( 'Message', $Command )
}
if ( [boolean]$SubStepLevel ) {
$ParamsGeneral.Add( 'SubStepLevel', $SubStepLevel )
$ParamsLevel.Add( 'SubStepLevel', $SubStepLevel )
}
$ProcessParams = @{
FilePath = 'git'
ArgumentList = $Command
}
if ( [boolean]$WorkingDirectory ) {
$ProcessParams.Add( 'WorkingDirectory', $WorkingDirectory )
}
$res = Start-ProcessAdv @ProcessParams
if ( -not $NoLog ) {
if ( $res.ExitCode -eq 0 ) {
Write-Log -Status OK @ParamsGeneral
}
else {
$CommandPart = @( $Command -split ' ' )[0]
switch ( $CommandPart ) {
'config' {
switch ( $res.ExitCode ) {
1 {
Write-Log -Status ERROR @ParamsGeneral
Write-Log -Message "The section or key is invalid" -Status ERROR @ParamsLevel
}
2 {
Write-Log -Status ERROR @ParamsGeneral
Write-Log -Message "no section or name was provided" -Status ERROR @ParamsLevel
}
3 {
Write-Log -Status ERROR @ParamsGeneral
Write-Log -Message "the config file is invalid" -Status ERROR @ParamsLevel
}
4 {
Write-Log -Status ERROR @ParamsGeneral
Write-Log -Message "the config file cannot be written" -Status ERROR @ParamsLevel
}
5 {
Write-Log -Status WARN @ParamsGeneral
Write-Log -Message "you try to unset an option which does not exist" -Status WARN @ParamsLevel
}
6 {
Write-Log -Status ERROR @ParamsGeneral
Write-Log -Message "you try to use an invalid regexp" -Status ERROR @ParamsLevel
}
}
}
}
if ( [boolean]( $res.StdErr ) ) {
Write-Log -Message $res.StdErr -Status ERROR @ParamsLevel
}
elseif ( [boolean]( $res.StdOut ) ) {
Write-Log -Message $res.StdOut -Status ERROR @ParamsLevel
}
}
}
if ( $Passthru ) {
return $res
}
}
#endregion functions
#region execution
#region test connectivity to Proxy
$ProxyConnection = [boolean]( Test-NetConnection -ComputerName $ProxyServer -Port $ProxyPort -WarningAction SilentlyContinue ).TcpTestSucceeded
#endregion test connectivity to Proxy
#region get git config
$ConfiguredProxyHTTP = ( Invoke-GITCommand -Command 'config http.proxy' -Passthru -NoLog -WorkingDirectory $CurrentLocation ).StdOut
$ConfiguredProxyHTTPS = ( Invoke-GITCommand -Command 'config https.proxy' -Passthru -NoLog -WorkingDirectory $CurrentLocation ).StdOut
#endregion get git config
#region configure git proxy
if ( $ProxyConnection ) {
$NeededProxy = "http://$( $ProxyServer ):$( $ProxyPort )"
Invoke-GITCommand -Command "config --global --unset http.proxy" -SubStepLevel 1
Invoke-GITCommand -Command "config --global --unset https.proxy" -SubStepLevel 1
Invoke-GITCommand -Command "config --global --add http.proxy ${NeededProxy}" -SubStepLevel 1
Invoke-GITCommand -Command "config --global --add https.proxy ${NeededProxy}" -SubStepLevel 1
}
else {
Invoke-GITCommand -Command "config --global --unset http.proxy" -SubStepLevel 1
Invoke-GITCommand -Command "config --global --unset https.proxy" -SubStepLevel 1
}
#endregion configure git proxy
#endregion execution