Module/Administration/New-BCSPasswordStateEntry.ps1

<#
.SYNOPSIS
    Sets up a new Brightcom Tenant
.DESCRIPTION
     
.PARAMETER ListID
  Specifies PasswordState List ID, default 6
 
.PARAMETER Tenant
  Specifies Tenant to use in URL
 
.PARAMETER businessCentralserverInstance
  Specifies Service Instance to use in URL, default BCNUP
 
.PARAMETER Title
  Specifies Password Entry Title, Mandatory
 
.PARAMETER UserName
  Specifies Password Entry UserName, default Admin
 
.PARAMETER PasswordStateURL
  Specifies the URL to PasswordSate
 
.PARAMETER PasswordStateApiKey
  Secure string containing your Password State Api Key
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
 
.EXAMPLE
    New-BCSPasswordStateEntry -ListID 1 -CustomerName "Your Customer Name" -PasswordStateURL "https://passwordstate" -PasswordStateApiKey (Get-BCSSecureString "Your Private API Key")
#>

function New-BCSPasswordStateEntry {
  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingUsernameAndPasswordParams', '')]
  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]

  Param (
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [string]$PasswordStateListID = "6",
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    [string]$CustomerName,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    [string]$businessCentralserverInstance,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [string]$UserName = "admin",
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    [string]$PasswordStateURL,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    [securestring]$PasswordStateApiKey    
  )
  begin {}

  process {
    try {
      $tenant = $CustomerName.replace(' ', '')

      $PSapiKey = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PasswordStateApiKey))

      $url = ('{0}/api/searchpasswords/{1}?title={2}' -f $PasswordStateURL, $PasswordStateListID, $CustomerName)

      try {
        $PasswordEntry = Invoke-Restmethod -Method Get -Uri $url -ContentType "application/json" -Header @{ "APIKey" = "$PSapiKey" }
      }
      catch {}

      if ($null -eq $PasswordEntry) {
        Write-Host "Creating new passwordstate entry with Title $CustomerName"
        $jsonData = ('"PasswordListID":"{0}","Title":"{1}","UserName":"{2}","GeneratePassword": true,"Url": "https://{3}.brightcom.online/{4}"' -f $PasswordStateListID, $CustomerName, $UserName, $tenant.ToLower(), $businessCentralserverInstance)
        $jsonData = "{" + $jsonData + "}"

        $url = "$PasswordStateURL/api/passwords"
        $PasswordEntry = Invoke-Restmethod -Method Post -Uri $url -ContentType "application/json" -Body $jsonData -Header @{ "APIKey" = "$PSapiKey" }
      }
      else {
        Write-Host "Passwordstate entry already exists for $CustomerName, reusing information from this entry."
      }

      $PasswordEntry
    }       
    catch {
      Write-Host -ForegroundColor Red "Error Creating password: exception: $($_.Exception.Message)"
      Throw
    }
  }
  end {
  }
}

Export-ModuleMember -Function New-BCSPasswordStateEntry