Register-CentralFeedbackProject.ps1 1004 B

12345678910111213141516171819202122
  1. [CmdletBinding()]
  2. param(
  3. [Parameter(Mandatory = $true)][ValidatePattern('^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$')][string]$ProjectId,
  4. [string]$ConfigPath = (Join-Path $PSScriptRoot 'config\central-feedback.config.dpapi')
  5. )
  6. . (Join-Path $PSScriptRoot 'Common.ps1')
  7. $config = Get-ProtectedConfig -Path $ConfigPath
  8. if (@($config.allowed_projects | Where-Object { $_.project_id -eq $ProjectId }).Count -gt 0) { throw "Project already registered: $ProjectId" }
  9. $submitKey = New-SubmitKey
  10. try {
  11. $entry = [pscustomobject]@{ project_id = $ProjectId; key_hash = (Get-Sha256Hex -Bytes ([Text.Encoding]::UTF8.GetBytes($submitKey))); enabled = $true }
  12. $config.allowed_projects = @($config.allowed_projects) + $entry
  13. Save-ProtectedConfig -Config $config -Path $ConfigPath
  14. Write-Output 'Project registration created. Copy the following key through an approved private channel; it is shown only once:'
  15. Write-Output $submitKey
  16. }
  17. finally {
  18. $submitKey = $null
  19. $config.app_secret = $null
  20. }