-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstp.ps1
1521 lines (1377 loc) · 83.1 KB
/
stp.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -Version 5.1
<#
.SYNOPSIS
Installation script for PowerShell managing solution hosted at https://github.com/ztrhgf/Powershell_CICD_repository
Contains same steps as described at https://github.com/ztrhgf/Powershell_CICD_repository/blob/master/1.%20HOW%20TO%20INSTALL.md
.DESCRIPTION
Installation script for PowerShell managing solution hosted at https://github.com/ztrhgf/Powershell_CICD_repository
Contains same steps as described at https://github.com/ztrhgf/Powershell_CICD_repository/blob/master/1.%20HOW%20TO%20INSTALL.md
.PARAMETER noEnvModification
Switch to omit changes of your environment i.e. just customization of cloned folders content 'repo_content_set_up' will be made.
.PARAMETER iniFile
Path to text ini file that this script uses as storage for values the user entered during this scripts run.
So next time, they can be used to speed up whole installation process.
Default is "Powershell_CICD_repository.ini" in root of user profile, so it can't be replaced when user reset cloned repository etc.
.NOTES
Author: Ondřej Šebela - ztrhgf@seznam.cz
#>
[CmdletBinding()]
Param (
[switch]$noEnvModification,
[string]$iniFile = (Join-Path -Path $env:USERPROFILE -ChildPath 'Powershell_CICD_repository.ini')
)
Begin {
#region Variables
$isserver = $notadmin = $notadadmin = $noadmodule = $nogpomodule = $skipad = $skipgpo = $mgmserver = $accessdenied = $repositoryhostsession = $mgmserversession = 0
$Host.UI.RawUI.Windowtitle = 'Installer of PowerShell CI/CD solution'
$transcript = Join-Path -Path $env:USERPROFILE -ChildPath ((Split-Path -Path $PSCommandPath -Leaf) + '.log')
Start-Transcript -Path $transcript -Force
$ErrorActionPreference = 'Stop'
# char that is between name of variable and its value in ini file
$divider = '='
# list of variables needed for installation, will be saved to iniFile
$setupVariable = @{}
# name of GPO that will be used for connecting computers to this solution
$GPOname = 'PS_env_set_up'
# hardcoded PATHs for TEST installation
$repositoryShare = "\\$env:COMPUTERNAME\repositoryShare"
$repositoryShareLocPath = Join-Path -Path $env:SystemDrive -ChildPath 'repositoryShare'
$remoteRepository = Join-Path -Path $env:SystemDrive -ChildPath 'myCompanyRepository_remote'
$userRepository = Join-Path -Path $env:SystemDrive -ChildPath 'myCompanyRepository'
#endregion Variables
### Detect if Server
If ((Get-WmiObject -Class Win32_OperatingSystem).ProductType -in (2, 3)) {$isServer++}
#region helper functions
Function Step-pressKeyToContinue {
Write-Host "`nPress any key to continue" -NoNewline
$null = [Console]::ReadKey('?')
}
Function Step-continue {
[CmdletBinding()]
Param ([string]$text, [switch]$passthru)
$t = 'Continue? (Y|N)'
If ($text) {$t = "$text. $t"}
$choice = ''
While ($choice -notmatch '^[Y|N]$') {$choice = Read-Host -Prompt $t}
If ($choice -eq 'N') {
If ($passthru) {Return $choice} else {break}
}
if ($passthru) {Return $choice}
}
Function Step-skip {
[CmdletBinding()]
Param ([string]$text)
$t = 'Skip? (Y|N)'
If ($text) {$t = "$text. $t"}
$t = "`n$t"
$choice = ''
While ($choice -notmatch '^[Y|N]$') {$choice = Read-Host -Prompt $t}
If ($choice -eq 'N') {Return $false} else {Return $true}
}
Function Step-getComputerMembership {
# Pull the gpresult for the current server
$Lines = & "$env:windir\system32\gpresult.exe" /s $env:COMPUTERNAME /v /SCOPE COMPUTER
# Initialize arrays
$cgroups = @()
# Out equals false by default
$Out = $False
# Define start and end lines for the section we want
$start = 'The computer is a part of the following security groups'
$end = 'Resultant Set Of Policies for Computer'
# Loop through the gpresult output looking for the computer security group section
ForEach ($Line In $Lines) {
If ($Line -match $start) {$Out = $True}
If ($Out -eq $True) {$cgroups += $Line}
If ($Line -match $end) {Break}
}
$cgroups | ForEach-Object { $_.trim() }
}
Function Step-startProcess {
[CmdletBinding()]
param (
[string]$filePath = '',
[string]$argumentList = '',
[string]$workingDirectory = (Get-Location),
[switch]$dontWait,
# lot of git commands output verbose output to error stream
[switch]$outputErr2Std
)
$p = New-Object -TypeName System.Diagnostics.Process
$p.StartInfo.UseShellExecute = $false
$p.StartInfo.RedirectStandardOutput = $true
$p.StartInfo.RedirectStandardError = $true
$p.StartInfo.WorkingDirectory = $workingDirectory
$p.StartInfo.FileName = $filePath
$p.StartInfo.Arguments = $argumentList
[void]$p.Start()
If (!$dontWait) {$p.WaitForExit()}
$p.StandardOutput.ReadToEnd()
If ($outputErr2Std) {
$p.StandardError.ReadToEnd()
} Else {
If ($err = $p.StandardError.ReadToEnd()) {Write-Error -Message $err}
}
}
Function Step-SetVariable {
# function defines variable and fills it with value find in ini file or entered by user
[CmdletBinding()]
Param ([string]$variable,[string]$readHost,[switch]$optional,[switch]$passThru)
$value = $setupVariable.GetEnumerator() | Where-Object {$_.name -eq $variable -and $_.value} | Select-Object -ExpandProperty value
If (!$value) {
If ($optional) {
$value = Read-Host -Prompt " - (OPTIONAL) Enter $readHost"
} Else {
While (!$value) {
$value = Read-Host -Prompt " - Enter $readHost"
}
}
} Else {
'' # Write-Host " - variable '$variable' will be: $value" -ForegroundColor Gray
}
If ($value) {
# replace whitespace so as quotes
$value = $value -replace '^\s*|\s*$' -replace "^[`"']*|[`"']*$"
$setupVariable.$variable = $value
New-Variable -Name $variable -Value $value -Scope script -Force -Confirm:$false
} Else {
If (!$optional) {Throw "Variable $variable is mandatory!"}
}
If ($passThru) {Return $value}
}
Function Step-saveInput {
# call after each successfully ended section, so just correct inputs will be stored
If (Test-Path -Path $iniFile -ErrorAction SilentlyContinue) {Remove-Item -Path $iniFile -Force -Confirm:$false}
$setupVariable.GetEnumerator() |
ForEach-Object {
If ($_.name -and $_.value) {
$_.name + '=' + $_.value | Out-File -FilePath $iniFile -Append -Encoding utf8
}
}
}
Function Step-setPermissions {
[cmdletbinding()]
Param (
[Parameter(Mandatory = $true,HelpMessage='Provide Path')][string]$path,
$readUser,
$writeUser,
[switch]$resetACL
)
If (!(Test-Path -Path $path)) {Throw "Path isn't accessible"}
$permissions = @()
If (Test-Path -Path $path -PathType Container) {
# it is folder
$acl = New-Object -TypeName System.Security.AccessControl.DirectorySecurity
If ($resetACL) {
# reset ACL, i.e. remove explicit ACL and enable inheritance
$acl.SetAccessRuleProtection($false, $false)
} Else {
# disable inheritance and remove inherited ACL
$acl.SetAccessRuleProtection($true, $false)
If ($readUser) {
$readUser | ForEach-Object {
$permissions += @(, ("$_", 'ReadAndExecute', 'ContainerInherit,ObjectInherit', 'None', 'Allow'))
}
}
If ($writeUser) {
$writeUser | ForEach-Object {
$permissions += @(, ("$_", 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow'))
}
}
}
} Else {
# it is file
$acl = New-Object -TypeName System.Security.AccessControl.FileSecurity
If ($resetACL) {
# reset ACL, ie remove explicit ACL and enable inheritance
$acl.SetAccessRuleProtection($false, $false)
} Else {
# disable inheritance and remove inherited ACL
$acl.SetAccessRuleProtection($true, $false)
If ($readUser) {
$readUser | ForEach-Object {
$permissions += @(, ("$_", 'ReadAndExecute', 'Allow'))
}
}
If ($writeUser) {
$writeUser | ForEach-Object {
$permissions += @(, ("$_", 'FullControl', 'Allow'))
}
}
}
}
$permissions | ForEach-Object {
$ace = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $_
$acl.AddAccessRule($ace)
}
Try {
# Set-Acl cannot be used because of bug https://stackoverflow.com/questions/31611103/setting-permissions-on-a-windows-fileshare
(Get-Item -Path $path).SetAccessControl($acl)
} Catch {
Throw "There was an error when setting NTFS rights: $_"
}
}
Function Step-copyFolder {
[cmdletbinding()]
Param (
[string]$source,
[string]$destination,
[string]$excludeFolder = '',
[switch]$mirror
)
Begin {
[Void][System.IO.Directory]::CreateDirectory($destination)
}
Process {
If ($mirror) {
$result = & "$env:windir\system32\robocopy.exe" "$source" "$destination" /MIR /E /NFL /NDL /NJH /R:4 /W:5 /XD "$excludeFolder"
} Else {
$result = & "$env:windir\system32\robocopy.exe" "$source" "$destination" /E /NFL /NDL /NJH /R:4 /W:5 /XD "$excludeFolder"
}
$copied = 0
$failures = 0
$duration = ''
$deleted = @()
$errMsg = @()
$result | ForEach-Object {
If ($_ -match '\s+Dirs\s+:') {
$lineAsArray = (($_.Split(':')[1]).trim()) -split '\s+'
$copied += $lineAsArray[1]
$failures += $lineAsArray[4]
}
If ($_ -match '\s+Files\s+:') {
$lineAsArray = ($_.Split(':')[1]).trim() -split '\s+'
$copied += $lineAsArray[1]
$failures += $lineAsArray[4]
}
If ($_ -match '\s+Times\s+:') {
$lineAsArray = ($_.Split(':', 2)[1]).trim() -split '\s+'
$duration = $lineAsArray[0]
}
If ($_ -match '\*EXTRA \w+') {
$deleted += @($_ | ForEach-Object { ($_ -split '\s+')[-1] })
}
If ($_ -match '^ERROR: ') {
$errMsg += ($_ -replace '^ERROR:\s+')
}
# captures errors like: 2020/04/27 09:01:27 ERROR 2 (0x00000002) Accessing Source Directory C:\temp
If ($match = ([regex]'^[0-9 /]+ [0-9:]+ ERROR \d+ \([0-9x]+\) (.+)').Match($_).captures.groups) {
$errMsg += $match[1].value
}
}
Return [PSCustomObject]@{
'Copied' = $copied
'Failures' = $failures
'Duration' = $duration
'Deleted' = $deleted
'ErrMsg' = $errMsg
}
}
}
Function Step-ChooseFile {
Param ([string]$Title)
Add-Type -AssemblyName system.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$FileBrowser.Filter = 'Application (*.exe)|*.exe'
$FileBrowser.Title = $Title
$FileChoice = $FileBrowser.ShowDialog()
If ($FileChoice -eq [System.Windows.Forms.DialogResult]::OK) {
$EXEPath = $FileBrowser.FileName
} else {
$EXEPath = 'NA'
}
Return $EXEPath#.FileName #$BackupLocation
}
Function Step-installGIT {
$iPath1 = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
$iPath2 = 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
$installedGITVersion = (
(Get-ItemProperty -Path $iPath1) + (Get-ItemProperty -Path $iPath2) |
Where-Object {$_.DisplayName -and $_.Displayname.Contains('Git version')}
) | Select-Object -ExpandProperty DisplayVersion
If (!$installedGITVersion -or $installedGITVersion -as [version] -lt '2.27.0') {
# Get latest download url for git-for-windows 64-bit exe
$url = 'https://api.github.com/repos/git-for-windows/git/releases/latest'
$install_args = '/SP- /VERYSILENT /SUPPRESSMSGBOXES /NOCANCEL /NORESTART /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS'
If ($asset = Invoke-RestMethod -Method Get -Uri $url | ForEach-Object {$_.assets} | Where-Object {$_.name -like '*64-bit.exe'}) {
### Download Git Installer File
Write-Host ' - downloading'
$installer = "$env:temp\$($asset.name)"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $installer
$ProgressPreference = 'Continue'
### Install Git
Write-Host ' - installing'
Start-Process -FilePath $installer -ArgumentList $install_args -Wait
Start-Sleep -Seconds 3
# update PATH
$env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User')
} Else {
$Choice = Read-Host -Prompt 'Unable to fetch. If you previously downloaded, would you like to browse to the installer? [Y/N]'
If ($Choice -match '[Yy]'){
$installer = Step-ChooseFile -Title 'Git-Installer'
Start-Process -FilePath $installer -ArgumentList $install_args -Wait
Start-Sleep -Seconds 3
# update PATH
$env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User')
} Else {
Write-Warning -Message "Skipped!`nURL $url isn't accessible, install GIT manually"
Step-continue
}
}
} Else {
Write-Host ' - already installed'
}
}
Function Step-installGITCredManager {
$ErrorActionPreference = 'Stop'
$url = 'https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/latest'
$asset = Invoke-WebRequest -Uri $url -UseBasicParsing
Try {
$durl = (($asset.RawContent -split "`n" | Where-Object { $_ -match '<a href="/.+\.exe"' }) -split '"')[1]
} Catch {
''
}
If ($durl) {
### Downloading Git Credential Manager
$url = 'github.com' + $durl
$installer = "$env:temp\gitcredmanager.exe"
Write-Host ' - downloading'
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $url -OutFile $installer
$ProgressPreference = 'Continue'
### Installing Git Credential Manager
Write-Host ' - installing'
$install_args = '/VERYSILENT /SUPPRESSMSGBOXES /NOCANCEL /NORESTART /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS'
Start-Process -FilePath $installer -ArgumentList $install_args -Wait
} Else {
Write-Warning -Message "Skipped!`nURL $url isn't accessible, install GIT Credential Manager for Windows manually"
Step-continue
}
}
Function Step-installVSC {
### Test if Microsoft VS Code is already installed
$codeCmdPath = "$env:ProgramFiles\Microsoft VS Code\bin\code.cmd"
$codeExePath = "$env:ProgramFiles\Microsoft VS Code\Code.exe"
If (Test-Path -Path $codeExePath) {
Write-Host ' - already installed'
Return
}
### Downloading Microsoft VS Code
$vscInstaller = "$env:TEMP\vscode-stable.exe"
Remove-Item -Force -Path $vscInstaller -ErrorAction SilentlyContinue
Write-Host ' - downloading'
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri 'https://update.code.visualstudio.com/latest/win32-x64/stable' -OutFile $vscInstaller
$ProgressPreference = 'Continue'
### Installing Microsoft VS Code
Write-Host ' - installing'
$loadInf = '@
[Setup]
Lang=english
Dir=C:\Program Files\Microsoft VS Code
Group=Visual Studio Code
NoIcons=0
Tasks=desktopicon,addcontextmenufiles,addcontextmenufolders,addtopath
@'
$infPath = Join-Path -Path $env:TEMP -ChildPath load.inf
$loadInf | Out-File -FilePath $infPath
Start-Process -FilePath $vscInstaller -ArgumentList "/VERYSILENT /LOADINF=${infPath} /mergetasks=!runcode" -Wait
}
Function Step-createSchedTask {
[CmdletBinding()]
Param ($xmlDefinition, $taskName)
$result = & "$env:windir\system32\schtasks.exe" /CREATE /XML "$xmlDefinition" /TN "$taskName" /F
If (!$?) {Throw "Unable to create scheduled task $taskName"}
}
Function Step-startSchedTask {
[CmdletBinding()]
Param ($taskName)
$result = & "$env:windir\system32\schtasks.exe" /RUN /I /TN "$taskName"
If (!$?) {Throw "Task $taskName finished with error. Check '$env:SystemRoot\temp\repo_sync.ps1.log'"}
}
Function Step-exportCred {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true,HelpMessage='Provide Credentials')][System.Management.Automation.Credential()][System.Management.Automation.PSCredential]$credential,
[string]$xmlPath = "$env:SystemDrive\temp\login.xml",
[Parameter(Mandatory = $true,HelpMessage='Credentials to Run As')][string]$runAs
)
Begin {
# transform relative path to absolute
Try {
$null = Split-Path -Path $xmlPath -Qualifier -ErrorAction Stop
} Catch {
$xmlPath = Join-Path -Path (Get-Location) -ChildPath $xmlPath
}
# remove existing xml
Remove-Item -Path $xmlPath -ErrorAction SilentlyContinue -Force
# create destination folder
[Void][System.IO.Directory]::CreateDirectory((Split-Path -Path $xmlPath -Parent))
}
Process {
$login = $credential.UserName
$pswd = $credential.GetNetworkCredential().password
$command = @"
# just in case auto-load of modules would be broken
Import-Module `$env:windir\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Security -ErrorAction Stop
`$pswd = ConvertTo-SecureString `'$pswd`' -AsPlainText -Force
`$credential = New-Object System.Management.Automation.PSCredential $login, `$pswd
Export-Clixml -inputObject `$credential -Path $xmlPath -Encoding UTF8 -Force -ErrorAction Stop
"@
# encode as base64
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedString = [Convert]::ToBase64String($bytes)
#TODO ~(ideal to help schtasks to be more universal)
$A = New-ScheduledTaskAction -Argument "-ExecutionPolicy Bypass -NoProfile -EncodedCommand $encodedString" -Execute "$PSHome\powershell.exe"
If ($runAs -match '\$') {
# under gMSA account
$P = New-ScheduledTaskPrincipal -UserId $runAs -LogonType Password
} Else {
# under SYSTEM account
$P = New-ScheduledTaskPrincipal -UserId $runAs -LogonType ServiceAccount
}
$S = New-ScheduledTaskSettingsSet
$taskName = 'cred_export'
Try {
$null = New-ScheduledTask -Action $A -Principal $P -Settings $S -ErrorAction Stop | Register-ScheduledTask -Force -TaskName $taskName -ErrorAction Stop
} Catch {
If ($_ -match 'No mapping between account names and security IDs was done') {
Throw "Account $runAs doesn't exist or cannot be used on $env:COMPUTERNAME"
} Else {
Throw "Unable to create scheduled task for exporting credentials.`nError was:`n$_"
}
}
Start-Sleep -Seconds 1
Start-ScheduledTask -TaskName $taskName
Start-Sleep -Seconds 5
$result = (Get-ScheduledTaskInfo -TaskName $taskName).LastTaskResult
Try {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction Stop
} Catch {
Throw "Unable to remove scheduled task $taskName. Remove it manually, it contains the credentials!"
}
If ($result -ne 0) {Throw 'Export of the credentials end with error'}
If ((Get-Item -Path $xmlPath).Length -lt 500) {
# sometimes sched. task doesn't end with error, but xml contained gibberish
Throw 'Exported credentials are not valid'
}
}
}
Function Test-Administrator {
<#
.SYNOPSIS
Checks if you are running as Administrator.
.DESCRIPTION
Tests if you are currently running powershell as administrator.
.EXAMPLE
Test-Administrator
If yes, returns true, if not, returns false
#>
If ($PSVersionTable.PSVersion.Major -le 5) {
$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())
Return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
}
#endregion helper functions
# store function definitions so I can recreate them in scriptblock
$allFunctionDefs = "Function Step-continue { ${function:Step-continue} };`
Function Step-pressKeyToContinue { ${function:Step-pressKeyToContinue} };`
Function Step-skip { ${function:Step-skip} };`
Function Step-installGIT { ${function:Step-installGIT} };`
Function Step-installGITCredManager { ${function:Step-installGITCredManager} };`
Function Step-createSchedTask { ${function:Step-createSchedTask} };`
Function Step-exportCred { ${function:Step-exportCred} };`
Function Step-startSchedTask { ${function:Step-startSchedTask} };`
Function Step-setPermissions { ${function:Step-setPermissions} };`
Function Step-getComputerMembership { ${function:Step-getComputerMembership} };`
Function Step-startProcess { ${function:Step-startProcess} }"
}
Process {
#region initial
Function Step-OnScreenMSG {
Param ([int]$z)
Switch ($z) {
1 {
Write-Host "####################################`n# INSTALL OPTIONS`n####################################" -ForegroundColor Magenta
Write-Host '1)' -NoNewline -ForegroundColor Yellow;Write-Host ' TEST installation' -ForegroundColor Cyan
Write-Host ' - PURPOSE : ' -NoNewLine -ForegroundColor Yellow;Write-Host 'Fast and safe test of the features, this solution offers.'
Write-Host ' - Run this installer on a test computer (preferably VM [Windows Sandbox, Virtualbox, Hyper-V, etc.])'
Write-Host " - No prerequisities needed like '" -NoNewLine;Write-Host 'Active Directory' -NoNewLine -ForegroundColor Cyan;Write-Host "' or a '" -NoNewLine;Write-Host 'Cloud Repository' -NoNewLine -ForegroundColor Cyan;Write-Host "'."
Write-Host ' - GOAL : ' -NoNewLine -ForegroundColor Yellow;Write-Host 'To have this as simple as possible - Installer automatically:'
Write-Host ' - Installs VSC, GIT.'
Write-Host ' - Creates GIT repository in ' -NoNewLine;Write-Host $remoteRepository -NoNewLine -ForegroundColor Cyan;Write-Host '. (and clone it to ' -NoNewLine;Write-Host $userRepository -NoNewLine -ForegroundColor Cyan;Write-Host '.)'
Write-Host ' - Creates folder ' -NoNewLine;Write-Host $repositoryShareLocPath -NoNewLine -ForegroundColor Cyan;Write-Host ' and shares it as ' -NoNewLine;Write-Host $repositoryShare -NoNewLine -ForegroundColor Cyan;Write-Host '.'
Write-Host ' - Creates security group repo_reader, repo_writer.'
Write-Host ' - Creates required scheduled tasks.'
Write-Host ' - Creates and sets global PowerShell profile.'
Write-Host " - Starts VSC editor with your new repository, so you can start your testing immediately. :)`n"
Write-Host '2)' -NoNewline -ForegroundColor Yellow;Write-Host ' Standard installation (Active Directory needed)' -ForegroundColor Cyan
Write-Host ' - This script will set up your own GIT repository and your environment by:'
Write-Host ' - Creating repo_reader, repo_writer AD groups.'
Write-Host ' - Creates shared folder for serving repository data to clients.'
Write-Host ' - Customizes generic data from repo_content_set_up folder to match your environment.'
Write-Host ' - Copies customized data to your repository.'
Write-Host ' - Sets up your repository: (Activate custom git hooks / Set git user name and email).'
Write-Host ' - Commit & Push new content of your repository.'
Write-Host ' - Sets up MGM server: (Copies the Repo_sync folder / Creates Repo_sync scheduled task / Exports repo_puller credentials).'
Write-Host ' - Copies exported credentials from MGM to local repository, Commit and Push it.'
Write-Host " - Creates a GPO '" -NoNewLine;Write-Host $GPOname -NoNewLine -ForegroundColor Cyan;Write-Host "' that will be used for connecting clients to this solution:"
Write-Host ' - NOTE: Linking GPO has to be done manually.' -ForegroundColor Yellow
Write-Host ' - NOTE: Every step has to be explicitly confirmed.' -ForegroundColor Red
Write-Host '3)' -NoNewline -ForegroundColor Yellow;Write-Host ' Update of existing installation' -ForegroundColor Cyan
Write-Host ' - NO MODIFICATION OF YOUR ENVIRONMENT WILL BE MADE.' -ForegroundColor Yellow
Write-Host ' - Just customization of generic data in repo_content_set_up folder to match your environment.'
Write-Host ' - Merging with your own repository etc has to be done manually.'
}
2 {
Write-Host "####################################`n# BEFORE YOU CONTINUE`n####################################" -ForegroundColor Magenta
Write-Host '- Create cloud or locally hosted GIT !private! repository (tested with Azure DevOps but probably will work also with GitHub etc).'
Write-Host ' - Create READ only account in that repository (repo_puller).'
Write-Host ' - Create credentials for this account, that can be used in unattended way (i.e. alternate credentials in Azure DevOps).'
Write-Host " - Install the newest version of 'Git' and 'Git Credential Manager for Windows' and clone your repository locally."
Write-Host " - Using 'git clone' command under account, that has write permission to the repository i.e. yours."
Write-Host ' - NOTE:' -ForegroundColor Yellow
Write-Host " - It is highly recommended to use 'Visual Studio Code (VSC)' editor to work with the repository content because it provides:"
Write-Host ' - Unified admin experience through repository VSC workspace settings.'
Write-Host ' - Integration & control of GIT.'
Write-Host ' - Auto-Formatting of the code, etc..'
Write-Host ' - More details can be found at https://github.com/ztrhgf/Powershell_CICD_repository/blob/master/1.%20HOW%20TO%20INSTALL.md'
}
3 {
Write-Host "############################`n!!! ANYONE WHO CONTROL THIS SOLUTION IS THE DE FACTO ADMINISTRATOR ON EVERY COMPUTER CONNECTED TO IT !!!`n" -ForegroundColor Red
Write-Host 'So:'
Write-Host ' - Only *Approved Users* should have write access to GIT repository.' -ForegroundColor Yellow
Write-Host ' - For accessing cloud GIT repository, use MFA if possible' -ForegroundColor Yellow
Write-Host ' - MGM server (processes repository data and uploads them to share) has to be protected same as the server that hosts that repository share.' -ForegroundColor Yellow
Write-Host '############################' -ForegroundColor Red
}
4 {
Write-Host '############################' -ForegroundColor Yellow
Write-Host "Your input will be stored to '" -NoNewLine -ForegroundColor Yellow;Write-Host $iniFile -NoNewLine;Write-Host "'. So next time you start this script, its content will be automatically used." -ForegroundColor Yellow
Write-Host '############################' -ForegroundColor Yellow
}
5 {
Write-Host " - copy Repo_sync folder to " -NoNewLine;Write-Host $MGMRepoSync -NoNewLine -ForegroundColor Magenta;Write-Host "'"
Write-Host " - install newest version of 'GIT'"
Write-Host " - create scheduled task '" -NoNewLine;Write-Host 'Repo_sync' -NoNewLine -ForegroundColor Magenta;Write-Host "' from '" -NoNewLine;Write-Host 'Repo_sync.xml' -NoNewLine -ForegroundColor Magenta;Write-Host "'"
Write-Host " - export '" -NoNewLine;Write-Host 'repo_puller' -NoNewLine -ForegroundColor Magenta;Write-Host "' account alternate credentials to '" -NoNewLine;Write-Host "$MGMRepoSync\login.xml" -NoNewLine -ForegroundColor Magenta;Write-Host "' (only SYSTEM account on " -NoNewLine;Write-Host $MGMServer -NoNewLine -ForegroundColor Magenta;Write-Host " will be able to read them!)"
Write-Host " - copy exported credentials from " -NoNewLine;Write-Host $MGMServer -NoNewLine -ForegroundColor Magenta;Write-Host " to " -NoNewLine;Write-Host $userRepoSync -ForegroundColor Magenta
Write-Host " - commit&push exported credentials (so they won't be automatically deleted from " -NoNewLine;Write-Host $MGMServer -NoNewLine -ForegroundColor Magenta;Write-Host ', after this solution starts working)'
}
6 {
Write-Host ' - For ASAP test that synchronization is working:'
Write-Host " - Run on client command '" -NoNewLine;Write-Host 'gpupdate /force' -NoNewLine -ForegroundColor Cyan;Write-Host "' to create scheduled task " -NoNewLine;Write-Host $GPOname -NoNewLine -ForegroundColor Magenta;Write-Host '.'
Write-Host ' - Run that sched. task and check the result in ' -NoNewLine;Write-Host "C:\Windows\Temp\$GPOname.ps1.log" -NoNewLine -ForegroundColor Cyan;Write-Host '.'
}
7 {
Write-Host "SUMMARY INFORMATION ABOUT THIS !TEST! INSTALLATION:"
Write-Host " - Central Repository share is at " -NoNewLine;Write-Host $repositoryShareLocPath -NoNewLine -ForegroundColor Magenta;Write-Host " (locally at " -NoNewLine;Write-Host $repositoryShareLocPath -NoNewLine -ForegroundColor Magenta;Write-Host ').'
Write-Host " - It is used by clients to synchronize their repository data."
Write-Host " - (Cloud) Repository is hosted locally at " -NoNewLine;Write-Host $remoteRepository -ForegroundColor Magenta
Write-Host " - Simulates for example GitHub private repository."
Write-Host " - (Cloud) Repository is locally cloned to " -NoNewLine;Write-Host $userRepository -ForegroundColor Magenta
Write-Host " - Here you make changes (creates new functions, modules, ...) and commit them to (Cloud) Repository."
Write-Host " - Scheduled Tasks:"
Write-Host " - Repo_sync - Pulls data from (Cloud) GIT repository, Process them, and Synchronize result to " -NoNewLine;Write-Host $repositoryShare -ForegroundColor Magenta
Write-Host " - Processing is done in C:\Windows\Scripts\Repo_sync"
Write-Host " - Log file in C:\Windows\Temp\Repo_sync.ps1.log"
Write-Host " - PS_env_set_up - Synchronizes local content from " -NoNewLine;Write-Host $repositoryShare -NoNewLine -ForegroundColor Magenta;Write-Host " (i.e. it is used to get repository data to clients)."
Write-Host " - Log file in C:\Windows\Temp\PS_env_set_up.ps1.log"
}
8 {
Write-Host '- Do NOT place your GIT repository inside Dropbox, Onedrive or other similar synchronization tool, it would cause problems!'
Write-Host "- To understand, what is purpose of this repository content check https://github.com/ztrhgf/Powershell_CICD_repository/blob/master/3.%20SIMPLIFIED%20EXPLANATION%20OF%20HOW%20IT%20WORKS.md"
Write-Host "- For immediate refresh of clients data (and console itself) use function Refresh-Console"
Write-Host " - NOTE: Available only on computers defined in Variables module in variable `$computerWithProfile"
Write-Host "- For examples check https://github.com/ztrhgf/Powershell_CICD_repository/blob/master/2.%20HOW%20TO%20USE%20-%20EXAMPLES.md"
Write-Host "- For brief video introduction check https://youtu.be/-xSJXbmOgyk and other videos at https://youtube.com/playlist?list=PLcNLAABGhY_GqrWfOZGjpgFv3fiaL0ciM"
Write-Host "- For master Modules deployment check \modules\modulesConfig.ps1"
Write-Host "- For master Custom section features check \custom\customConfig.ps1"
Write-Host "- To see what is happening in the background check logs"
Write-Host " - In VSC Output terminal (CTRL + SHIFT + U, there switch output to GIT) (pre-commit.ps1 checks)"
Write-Host " - C:\Windows\Temp\Repo_sync.ps1.log on MGM server (synchronization from GIT repository to share)"
Write-Host " - C:\Windows\Temp\PS_env_set_up.ps1.log on client (synchronization from share to client)"
Write-Host "ENJOY :)"
}
}
}
If (!$noEnvModification) {
Clear-Host
Step-OnScreenMSG 1
$choice = ''
While ($choice -notmatch '^[1|2|3]$') {$choice = Read-Host -Prompt 'Choose install option (1|2|3)'}
If ($choice -eq 1) {
$testInstallation = 1
If (Test-Administrator -eq $false) {
# not running "as Administrator" - so relaunch as administrator
# get command line arguments and reuse them
$arguments = $myInvocation.line -replace [regex]::Escape($myInvocation.InvocationName), ''
Start-Process -FilePath powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" {1}' -f ($myinvocation.MyCommand.Definition), $arguments) # -noexit nebo -WindowStyle Hidden
# exit from the current, unelevated, process
Exit
}
}
If ($choice -in 1, 2) {
$noEnvModification = $false
} Else {
$noEnvModification = $true
}
}
Clear-Host
If (!$noEnvModification -and !$testInstallation) {
Step-OnScreenMSG 2
Step-pressKeyToContinue
} ElseIf ($testInstallation) {
Write-Host " - installing 'GIT'"
Step-installGIT
Write-Host " - installing 'VSC'"
Step-installVSC
Install-PackageProvider -Name nuget -Force -ForceBootstrap -Scope allusers | Out-Null
<#
If (!(Get-Module -ListAvailable PSScriptAnalyzer)) {
" - installing 'PSScriptAnalyzer' PS module"
Install-Module PSScriptAnalyzer -SkipPublisherCheck -Force
}
#>
Write-Host " - updating 'PackageManagement' PS module"
# solves issue https://github.com/PowerShell/vscode-powershell/issues/2824
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module -Name PackageManagement -Force -ErrorActionPreference SilentlyContinue
Write-Host ' - enabling running of PS scripts'
# because of PS global profile loading
Set-ExecutionPolicy -ExecutionPolicy Bypass -Force
}
# TODO ~(somewhere to write that it is needed) psremoting
If (!$testInstallation) {Clear-Host} Else {''}
If (!$noEnvModification -and !$testInstallation) {
Step-OnScreenMSG 3
Step-pressKeyToContinue
Clear-Host
Step-OnScreenMSG 4
}
If (!$testInstallation) {
Step-pressKeyToContinue
Clear-Host
}
#endregion initial
Try {
#region import variables
# import variables from ini file
# '#' can be used for comments, so skip such lines
If (Test-Path -Path $iniFile) {
Write-host "- Importing variables from $iniFile" -ForegroundColor Green
Get-Content -Path $iniFile -ErrorAction SilentlyContinue | Where-Object {$_ -and $_ -notmatch '^\s*#'} | ForEach-Object {
$line = $_
If (($line -split $divider).count -ge 2) {
$position = $line.IndexOf($divider)
$name = $line.Substring(0, $position) -replace '^\s*|\s*$'
$value = $line.Substring($position + 1) -replace '^\s*|\s*$'
Write-Host " - variable $name` will have value: $value"
# fill hash so I can later export (updated) variables back to file
$setupVariable.$name = $value
}
}
Step-pressKeyToContinue
}
#endregion import variables
If (!$testInstallation) {Clear-Host}
#region checks
Write-host '- Checking permissions etc' -ForegroundColor Green
<# computer isn't in domain
If (!$noEnvModification -and !(Get-WmiObject -Class win32_computersystem).partOfDomain) {
Write-Warning 'This PC isn't joined to domain. AD related steps will have to be done manually.'
++$skipAD
Step-continue
}
#>
# is local administrator
If (Test-Administrator -eq $false) {
Write-Warning -Message "Not running as administrator. Symlink for using repository PowerShell snippets file in VSC won't be created"
$notAdmin++
Step-pressKeyToContinue
}
If (!$testInstallation) {
# is domain admin
If (!$noEnvModification -and !((& "$env:windir\system32\whoami.exe" /all) -match 'Domain Admins|Enterprise Admins')) {
Write-Warning -Message 'You are not member of Domain nor Enterprise Admin group. AD related steps will have to be done manually.'
$notADAdmin++
Step-continue
}
# ActiveDirectory PS module is available
$ADAvailable = Get-Module -Name ActiveDirectory -ListAvailable
If (!$noEnvModification -and !$ADAvailable) {
Write-Warning -Message "ActiveDirectory PowerShell module isn't installed (part of RSAT)."
If (!$notAdmin -and ((Step-continue -text 'Proceed with installation' -passthru) -eq 'Y')) {
If ($isServer) {
$null = Install-WindowsFeature -Name RSAT-AD-PowerShell -IncludeManagementTools
} Else {
Try {
$null = Get-WindowsCapability -Name '*activedirectory*' -Online -ErrorAction Stop | Add-WindowsCapability -Online -ErrorAction Stop
} Catch {
Write-Warning -Message "Unable to install RSAT AD tools.`nAD related steps will be skipped, so make them manually."
$noADmodule++
Step-pressKeyToContinue
}
}
} Else {
Write-Warning -Message 'AD related steps will be skipped, so make them manually.'
$noADmodule++
Step-pressKeyToContinue
}
}
# GroupPolicy PS module is available
$GPOAvailable = Get-Module -Name GroupPolicy -ListAvailable
If (!$noEnvModification -and !$GPOAvailable) {
Write-Warning -Message "GroupPolicy PowerShell module isn't installed (part of RSAT)."
If (!$notAdmin -and ((Step-continue -text 'Proceed with installation' -passthru) -eq 'Y')) {
If ($isServer) {
$null = Install-WindowsFeature -Name GPMC -IncludeManagementTools
} Else {
Try {
$null = Get-WindowsCapability -Name '*grouppolicy*' -Online -ErrorAction Stop | Add-WindowsCapability -Online -ErrorAction Stop
} Catch {
Write-Warning -Message "Unable to install RSAT GroupPolicy tools.`nGPO related steps will be skipped, so make them manually."
$noGPOmodule++
Step-pressKeyToContinue
}
}
} Else {
Write-Warning -Message 'GPO related steps will be skipped, so make them manually.'
$noGPOmodule++
Step-pressKeyToContinue
}
}
If ($notADAdmin -or $noADmodule) {$skipAD++}
If ($notADAdmin -or $noGPOmodule) {$skipGPO++}
}
If (!$testInstallation) {
Step-pressKeyToContinue
Clear-Host
}
#endregion checks
If (!$testInstallation) {
Step-SetVariable -variable MGMServer -readHost 'the name of the MGM server (will be used for pulling, processing and distributing of repository data to repository share).'
If ($MGMServer -like '*.*') {
$MGMServer = ($MGMServer -split '\.')[0]
Write-Warning -Message "$MGMServer was in FQDN format. Just hostname was used"
}
If (!$noADmodule -and !(Get-ADComputer -Filter "name -eq '$MGMServer'")) {
Throw "$MGMServer doesn't exist in AD"
}
} Else {
# test installation
$MGMServer = $env:COMPUTERNAME
Write-Host ' - For testing purposes, this computer will host MGM server role too'
}
If (!$testInstallation) {
Step-saveInput
Clear-Host
}
#region create repo_reader, repo_writer
If (!$testInstallation) {
Write-Host '- Creating repo_reader, repo_writer AD security groups' -ForegroundColor Green
If (!$noEnvModification -and !$skipAD -and !(Step-skip)) {
'repo_reader', 'repo_writer' | ForEach-Object {
If (Get-ADGroup -filter "samaccountname -eq '$_'") {
Write-Host " - $_ already exists"
} Else {
If ($_ -match 'repo_reader') {
$right = 'read'
} Else {
$right = 'modify'
}
New-ADGroup -Name $_ -GroupCategory Security -GroupScope Universal -Description "Members have $right permission to repository share content."
Write-Host " - created $_"
}
}
} Else {
Write-Warning -Message "Skipped!`n`nCreate them manually"
}
} Else {
Write-Host '- Creating repo_reader, repo_writer security groups' -ForegroundColor Green
'repo_reader', 'repo_writer' | ForEach-Object {
If (Get-LocalGroup -Name $_ -ErrorAction SilentlyContinue) {
Write-Host "$_ group already exists."
} Else {
If ($_ -match 'repo_reader') {
$right = 'read'
} Else {
$right = 'modify'
}
$null = New-LocalGroup -Name $_ -Description "Members have $right right to repository share." # max 48 chars!
}
}
}
#endregion create repo_reader, repo_writer
If (!$testInstallation) {
Step-pressKeyToContinue
Clear-Host
}
#region adding members to repo_reader, repo_writer
If (!$testInstallation) {
Write-Host '- Adding members to repo_reader, repo_writer AD groups' -ForegroundColor Green
Write-Host " - add 'Domain Computers' to repo_reader group"
Write-Host " - add 'Domain Admins' and $MGMServer to repo_writer group"
If (!$noEnvModification -and !$skipAD -and !(Step-skip)) {
Write-Host " - adding 'Domain Computers' to repo_reader group (DCs are not members of this group!)"
Add-ADGroupMember -Identity 'repo_reader' -Members 'Domain Computers'
Write-Host " - adding 'Domain Admins' and $MGMServer to repo_writer group"
Add-ADGroupMember -Identity 'repo_writer' -Members 'Domain Admins', "$MGMServer$"
} Else {
Write-Warning -Message "Skipped! Fill them manually.`n`n - repo_reader should contains computers which you want to join to this solution i.e. 'Domain Computers' (if you choose just subset of computers, use repo_reader and repo_writer for security filtering on lately created GPO $GPOname)`n - repo_writer should contains 'Domain Admins' and $MGMServer server"
}
Write-Warning -Message "`nRESTART $MGMServer (and rest of the computers) to apply new membership NOW!"
} Else {
Write-Host '- Adding members to repo_reader, repo_writer groups' -ForegroundColor Green
#Write-Host " - adding SYSTEM to repo_reader group"
#Add-LocalGroupMember -Name 'repo_reader' -Member "SYSTEM"
Write-Host ' - adding Administrators and SYSTEM to repo_writer group'
'Administrators', 'SYSTEM' | ForEach-Object {
If ($_ -notin (Get-LocalGroupMember -Name 'repo_writer' | Select-Object -Property @{n='Name';e={($_.Name -split '\\')[-1]}} | Select-Object -ExpandProperty Name)) {
Add-LocalGroupMember -Name 'repo_writer' -Member $_
}
}
}
#endregion adding members to repo_reader, repo_writer
If (!$testInstallation) {
Step-pressKeyToContinue
Clear-Host
}
#region set up shared folder for repository data
Write-Host '- Creating shared folder for hosting repository data' -ForegroundColor Green
If (!$testInstallation) {
Step-SetVariable -variable repositoryShare -readHost 'UNC path to folder, where the repository data should be stored (i.e. \\mydomain\dfs\repository)'
} Else {
Write-Host " - For testing purposes $repositoryShare will be used"
}
If ($repositoryShare -notmatch '^\\\\[^\\]+\\[^\\]+') {Throw "$repositoryShare isn't valid UNC path"}
$permissions = "`n`t`t- SHARE`n`t`t`t- Everyone - FULL CONTROL`n`t`t- NTFS`n`t`t`t- SYSTEM, repo_writer - FULL CONTROL`n`t`t`t- repo_reader - READ"
If ($testInstallation -or (!$noEnvModification -and !(Step-skip))) {
Write-Host " - Testing, whether '$repositoryShare' already exists"
Try {
$repositoryShareExists = Test-Path -Path $repositoryShare
} Catch {
# in case this script already created that share but this user isn't yet in repo_writer, he will receive access denied error when accessing it
If ($_ -match 'access denied') {$accessDenied++}
}
If ($repositoryShareExists -or $accessDenied) {
If (!$testInstallation) {
Write-Warning -Message "Share '$repositoryShare' already exists.`n`tMake sure, that ONLY following permissions are set:$permissions`n`nNOTE: it's content will be replaced by repository data eventually!"
}
} Else {
# share or some part of its path doesn't exist
$isDFS = ''
If (!$testInstallation) {
# for testing installation I will use common UNC share
While ($isDFS -notmatch '^[Y|N]$') {$isDFS = Read-Host -Prompt "`n - Is '$repositoryShare' DFS share? (Y|N)"}
}
If ($isDFS -eq 'Y') {
#TODO ~(add support for creating a DFS share)
Write-Warning -Message "Skipped! Currently this installer doesn't support creation of DFS share.`nMake share manually with ONLY following permissions:$permissions"
} Else {
# creation of non-DFS shared folder
$repositoryHost = ($repositoryShare -split '\\')[2]
If (!$testInstallation -and !$noADmodule -and !(Get-ADComputer -Filter "name -eq '$repositoryHost'")) {Throw "$repositoryHost doesn't exist in AD"}
$parentPath = '\\' + [string]::join('\', $repositoryShare.Split('\')[2..3])
If (($parentPath -eq $repositoryShare) -or ($parentPath -ne $repositoryShare -and !(Test-Path -Path $parentPath -ErrorAction SilentlyContinue))) {
# shared folder doesn't exist, can't deduce local path from it, so get it from the user
If (!$testInstallation) {
Write-Host ''
Step-SetVariable -variable repositoryShareLocPath -readHost "local path to folder, which will be than shared as '$parentPath' (on $repositoryHost)"
} Else {
Write-Host " - For testing purposes, repository share will be stored locally in '$repositoryShareLocPath'"
}
} Else {
Write-Host "`n - Share $parentPath already exists. Folder for repository data will be created (if necessary) and JUST NTFS permissions will be set."
Write-Warning -Message 'So make sure, that SHARE permissions are set to: Everyone - FULL CONTROL!'
Step-pressKeyToContinue
}
$invokeParam = @{}
If (!$testInstallation) {
If ($notADAdmin) {
While (!$repositoryHostSession) {
$repositoryHostSession = New-PSSession -ComputerName $repositoryHost -Credential (Get-Credential -Message "Enter admin credentials for connecting to $repositoryHost through psremoting") -ErrorAction SilentlyContinue
}
} Else {
$repositoryHostSession = New-PSSession -ComputerName $repositoryHost
}
$invokeParam.Session = $repositoryHostSession
} Else {
Write-Host 'Testing installation i.e. locally'
}
$invokeParam.argumentList = $repositoryShareLocPath, $repositoryShare, $allFunctionDefs
$invokeParam.ScriptBlock = {
[CmdletBinding()]
Param ($repositoryShareLocPath, $repositoryShare, $allFunctionDefs)
# recreate function from it's definition