-
Notifications
You must be signed in to change notification settings - Fork 17
Adds PSU Install Option to Environment #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "[powershell]": { | ||
| "debug.saveBeforeStart": "nonUntitledEditorsInActiveGroup", | ||
| "editor.semanticHighlighting.enabled": false, | ||
| "editor.wordSeparators": "`~!@#$%^&*()=+[{]}\\|;:'\",.<>/?", | ||
| "editor.formatOnSave": true, | ||
| "editor.formatOnSaveMode": "modificationsIfAvailable", | ||
| }, | ||
| "diffEditor.experimental.showMoves": true, | ||
| "diffEditor.experimental.useTrueInlineView": true, | ||
| "powershell.codeFormatting.autoCorrectAliases": true, | ||
| "powershell.codeFormatting.avoidSemicolonsAsLineTerminators": true, | ||
| "powershell.codeFormatting.newLineAfterCloseBrace": false, | ||
| "powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationForFirstPipeline", | ||
| "powershell.codeFormatting.preset": "OTBS", | ||
| "powershell.codeFormatting.trimWhitespaceAroundPipe": true, | ||
| "powershell.codeFormatting.useCorrectCasing": true, | ||
| "powershell.codeFormatting.whitespaceAfterSeparator": true, | ||
| "powershell.codeFormatting.whitespaceBetweenParameters": false, | ||
| "powershell.debugging.executeMode": "DotSource", | ||
| "powershell.pester.useLegacyCodeLens": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| #requires -Modules C4B-Environment | ||
| <# | ||
| .SYNOPSIS | ||
| C4B Quick-Start Guide PowerShell Universal setup script | ||
|
|
||
| .DESCRIPTION | ||
| - Performs the following PowerShell Universal setup | ||
| - Install of PowerShell Universal package | ||
| - Creation of Chocolatey-specific jobs from template files | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| # The certificate thumbprint that identifies the target SSL certificate in | ||
| # the local machine certificate stores. | ||
| [Parameter()] | ||
| [ArgumentCompleter({ | ||
| Get-ChildItem Cert:\LocalMachine\TrustedPeople | ForEach-Object { | ||
| [System.Management.Automation.CompletionResult]::new( | ||
| $_.Thumbprint, | ||
| $_.Thumbprint, | ||
| "ParameterValue", | ||
| ($_.Subject -replace "^CN=(?<FQDN>.+),?.*$", '${FQDN}') | ||
| ) | ||
| } | ||
| })] | ||
| [ValidateScript({ Test-CertificateDomain -Thumbprint $_ })] | ||
| [string]$Thumbprint = $( | ||
| if ((Test-Path C:\choco-setup\clixml\chocolatey-for-business.xml) -and (Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).CertThumbprint) { | ||
| (Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).CertThumbprint | ||
| } else { | ||
| Get-ChildItem Cert:\LocalMachine\TrustedPeople -Recurse | Sort-Object { | ||
| $_.Issuer -eq $_.Subject # Prioritise any certificates above self-signed | ||
| } | Select-Object -ExpandProperty Thumbprint -First 1 | ||
| } | ||
| ), | ||
|
|
||
| # Optional: Sets PSU to use a provided SQL database instead of SQLLite. | ||
| [string]$ConnectionString | ||
| ) | ||
| try { | ||
| $DefaultEap = $ErrorActionPreference | ||
| $ErrorActionPreference = 'Stop' | ||
| Start-Transcript -Path "$env:SystemDrive\choco-setup\logs\Start-C4bPsuSetup-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt" | ||
|
|
||
| Invoke-Choco upgrade powershelluniversal-remove-default-listener.hook --confirm --no-progress | ||
|
|
||
| # Install PowerShell Universal | ||
| Invoke-Choco upgrade powershelluniversal --confirm --no-progress --install-args="STARTSERVICE=0$(if ($ConnectionString) {" CONNECTIONSTRING=$ConnectionString DATABASETYPE=SQL"})" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PowerShellUniversal CCR package needs to be added to the chocolatey.json file to enable offline installations. |
||
| $PSUPort = 5000 | ||
|
|
||
| # Handle configuration | ||
| $ConfigurationFile = Join-Path $env:ProgramData "PowerShellUniversal/appsettings.json" | ||
| $CurrentConfiguration = Get-Content $ConfigurationFile | ConvertFrom-Json | ||
|
|
||
| if ($Thumbprint) { | ||
| $PSUPort = 5000 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we allow the individual running the guide to select the TCP port for their PSU instance? If not in this initial PR, an issue should be raised to support this customization in the future. |
||
| $CurrentConfiguration.Kestrel.Endpoints = @{ | ||
| HTTPS = @{ | ||
| Url = "https://$(Get-ChocoEnvironmentProperty CertSubject):$PSUPort" | ||
| Certificate = @{ | ||
| Thumbprint = $Thumbprint | ||
| Store = "TrustedPeople" | ||
| Location = "LocalMachine" | ||
| AllowInvalid = "true" | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($ConnectionString) { | ||
| $CurrentConfiguration.Data.ConnectionString = $ConnectionString | ||
| } | ||
|
|
||
| if ((Get-Content $ConfigurationFile -Raw) -ne ($CurrentConfiguration | ConvertTo-Json -Depth 10)) { | ||
| $CurrentConfiguration | ConvertTo-Json -Depth 10 | Set-Content $ConfigurationFile | ||
| } | ||
|
|
||
| # Future consideration: parameter to disable external access? | ||
| $FwRuleParams = @{ | ||
| DisplayName = "PowerShellUniversal Access" | ||
| Direction = 'Inbound' | ||
| LocalPort = $PSUPort | ||
| Protocol = 'TCP' | ||
| Action = 'Allow' | ||
| } | ||
| $null = New-NetFirewallRule @FwRuleParams # Set-EnvFirewallRule @FwRuleParams | ||
|
|
||
| # Create admin user | ||
| if (-not ($User = Get-ChocoEnvironmentProperty PSUCredential)) { | ||
| $User = [pscredential]::new( | ||
| "admin", | ||
| (New-ServicePassword) | ||
| ) | ||
| Set-ChocoEnvironmentProperty PSUCredential $User | ||
|
|
||
| [System.Environment]::SetEnvironmentVariable('PSUDefaultAdminName', $User.UserName, [System.EnvironmentVariableTarget]::Machine) | ||
| [System.Environment]::SetEnvironmentVariable('PSUDefaultAdminPassword', $User.Password.ToPlainText(), [System.EnvironmentVariableTarget]::Machine) | ||
|
JPRuskin marked this conversation as resolved.
|
||
| } | ||
|
|
||
| # Set Security Defaults | ||
| $RepositoryDirectory = Join-Path $env:ProgramData UniversalAutomation\Repository | ||
|
|
||
| if (-not (Test-Path $RepositoryDirectory -PathType Container)) { | ||
| $null = New-Item -Path $RepositoryDirectory -ItemType Directory | ||
| } | ||
|
|
||
| if (-not (Test-Path $RepositoryDirectory\.universal\settings.ps1)) { | ||
| $null = New-Item -Path $RepositoryDirectory\.universal\settings.ps1 -Value @' | ||
| $Parameters = @{ | ||
| EnhancedAppTokenSecurity = $true | ||
| ApiSecurityModel = $true | ||
| } | ||
| Set-PSUSetting @Parameters | ||
| '@ -Force | ||
| } | ||
|
|
||
| # Deploy jobs and dashboards | ||
| Invoke-Choco upgrade chocolatey-licensed-psu-environment --confirm --no-progress | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the PR for this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The chocolatey-licensed-psu-environment package must also be added to the chocolatey.json file. Please link to a PR for this package to review or pull down for testing. |
||
|
|
||
| # Start service | ||
| Start-Service PowerShellUniversal | ||
|
|
||
| # Wait until the username and password have been initialized on the first run | ||
| if ([System.Environment]::GetEnvironmentVariable('PSUDefaultAdminPassword', [System.EnvironmentVariableTarget]::Machine)) { | ||
| Write-Verbose "[$(Get-Date -Format 'hh:mm:ss')] Waiting for PowerShell Universal to start..." | ||
| while (-not (Select-String -Path $env:ProgramData\PowerShellUniversal\Logs\System\systemLog$(Get-Date -Format "yyyyMMdd").txt -Pattern "\[INF\]\[UniversalAutomation\.StartupService\] Startup complete.$")) { | ||
| Start-Sleep -Seconds 5 | ||
| } | ||
| Write-Verbose "[$(Get-Date -Format 'hh:mm:ss')] PowerShell Universal has successfully started." | ||
| } | ||
|
|
||
| # Save useful params | ||
| Update-Clixml -Properties @{ | ||
| PowerShellUniversalUri = "https://$(Get-ChocoEnvironmentProperty CertSubject):$PSUPort" | ||
| } | ||
| } finally { | ||
| [System.Environment]::SetEnvironmentVariable('PSUDefaultAdminName', $null, [System.EnvironmentVariableTarget]::Machine) | ||
| [System.Environment]::SetEnvironmentVariable('PSUDefaultAdminPassword', $null, [System.EnvironmentVariableTarget]::Machine) | ||
|
JPRuskin marked this conversation as resolved.
|
||
|
|
||
| $ErrorActionPreference = $DefaultEap | ||
| Stop-Transcript | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it ran before PowerShell Universal is installed? What listener is it removing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a hook package, so (unless written to run on install as well as a hook) it needs to be in place before the main package is installed. Though thinking about it, this one is fine to move to after, we're making the adjustment in the setup script... so I can move it to after. Easy!
The listener it's removing is the http :5000 from one of the multiple configuration files, so that we can use it ourselves (and so that we don't have an additional http listener that we don't want to have available).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this stays as a package, it will need to be added to the chocolatey.json file for the packages used by the guide.