Modules/system.psm1

# /rest/v1.0/dd-systems/{SYSTEM-ID}/alerts

function Get-DDSystem {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "GET"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $apiver = "rest/v1.0"
            $uri = "system"
         }
         'byID' {
            $apiver = "rest/v1.0"
            $uri = "dd-systems/$($SYSTEM_ID)/systems"
         }               
      }
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response[-1] | Select-Object -ExcludeProperty link
         }        
      } 
       
   }
}

# /rest/v1.0/dd-systems/{SYSTEM-ID}/systems

function Get-DDSystems {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'space')]
      [switch]$space,
      [Parameter(Mandatory = $true, ParameterSetName = 'repl')]
      [switch]$repl,        
      [Parameter(Mandatory = $false, ParameterSetName = 'default')]
      [Parameter(Mandatory = $false, ParameterSetName = 'space')]
      [Parameter(Mandatory = $false, ParameterSetName = 'repl')]
      [string]
      $SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "GET"
   }
   process {
      $apiver = "rest/v2.0"
      switch ($PsCmdlet.ParameterSetName) {
         'default' {
            $uri = "dd-systems/$($SYSTEM_ID)/systems"
         }
         default {
            $uri = "dd-systems/$($SYSTEM_ID)/systems/stats/$($PsCmdlet.ParameterSetName)"
         }                    
      }
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         'default' {
            $response[-1] | Select-Object -ExcludeProperty link
         }
         default   
         {
            $response[-1].system_stats | Select-Object -ExcludeProperty links 
         }      
      } 
      
   }
}


function Set-DDSystems {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'change_pphrase')]
      [Parameter(Mandatory = $true, ParameterSetName = 'set_pphrase')]
      [securestring]$NewPassphrase,
      [Parameter(Mandatory = $false, ParameterSetName = 'change_pphrase')]
      [securestring]$OldPassphrase, 
      [Parameter(Mandatory = $true, ParameterSetName = 'set_pphrase')]
      [switch]$set_pphrase,
      [Parameter(Mandatory = $true, ParameterSetName = 'change_pphrase')]
      [switch]$change_pphrase,
      [Parameter(Mandatory = $false, ParameterSetName = 'change_pphrase')]
      [System.Management.Automation.PSCredential]$Sec_Officer_Credentials,                     
      $SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "PUT"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         'set_pphrase' {
            $apiver = "rest/v3.0"
            $uri = "dd-systems/$($SYSTEM_ID)/systems"
            $json = [ordered]@{ 
               operation       = 'set_pphrase';
               pphrase_request = @{
                  new_pphrase = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassphrase))
               }
            }   | ConvertTo-Json -Depth 3
         } 
         'change_pphrase' {
            if (!$Sec_Officer_Credentials) {
               Write-Host "Security Officer required for this operation"   
               $Sec_Officer = Read-Host -Prompt "Please Enter DD Security Officer´s username"
               $Sec_Officer_Password = Read-Host -Prompt "Password for user $Sec_Officer" -AsSecureString
               $Sec_Officer_Credentials = New-Object System.Management.Automation.PSCredential($Sec_Officer, $Sec_Officer_Password)
      
            }
            $apiver = "rest/v3.0"
            $uri = "dd-systems/$($SYSTEM_ID)/systems"
            $json = [ordered]@{ 
               operation       = 'change_pphrase';
               pphrase_request = @{
                  old_pphrase = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($OldPassphrase))                    
                  new_pphrase = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassphrase))
               }
            }   | ConvertTo-Json -Depth 3
         }          
         default {

         }
      }
      Write-Verbose ($body | out-string)
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $json
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
      }
      switch ($PsCmdlet.ParameterSetName) {
         'change_pphrase' {

            $private:sec_body = @{
               username = $Sec_Officer_Credentials.Username
               password = $Sec_Officer_Credentials.GetNetworkCredential().password
            }  | convertto-json   
            Write-Verbose ($private:sec_body | out-string) 
            $EncodedString = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Private:sec_body))
            $Global:DD_Headers.Add('X-DD-SEC-OFFICER', "USER $EncodedString")  
         }
      } 
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_       
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response[-1] | Select-Object -ExcludeProperty link
         }         
      } 
      
   }
}