PPDD-pwsh.psm1
function Unblock-DDSSLCerts { Add-Type -TypeDefinition @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ -ErrorAction SilentlyContinue [System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy } function Get-OLDDDtoken { [CmdletBinding()] param ( $DDusername = $DDusername, $DDpassword = $DDpassword, $apiver = "v1", $baseuri = $baseuri ) $body = @{username = $DDusername; password = $DDpassword; grant_type = "password"; scope = "write" } Invoke-RestMethod -Uri "https://$($baseuri)/api/$($apiver)/oauth/swagger" ` -body $body ` -ContentType "application/x-www-form-urlencoded" ` -SkipCertificateCheck ` -Method POST } function Get-DDtoken { [CmdletBinding()] param( [Parameter(Mandatory = $false, ParameterSetName = 'User', ValueFromPipelineByPropertyName = $true)] [pscredential]$DD_Credentials = $Global:DD_Credentials, [Parameter(Mandatory = $false, Position = 0, ValueFromPipelineByPropertyName = $true)] [string] $DD_BaseURI = $Global:DD_BaseUri, [switch]$trustCert, [switch]$force, $apiver = "rest/v1.0" ) Begin { if ($trustCert.IsPresent) { if ($($PSVersionTable.PSVersion.Major) -ge 6) { $global:SkipCertificateCheck = $TRUE } else { Unblock-DDSSLCerts } } if ($force.IsPresent) { write-verbose "Removing old Scope" Remove-Variable DD_BaseUri -Scope Global -ErrorAction SilentlyContinue Remove-Variable DD_Headers -Scope Global -ErrorAction SilentlyContinue Remove-Variable DD_ClientCredentials -Scope Global -ErrorAction SilentlyContinue Remove-Variable DD_Credentials -Scope Global -ErrorAction SilentlyContinue Remove-Variable DD_Scope -Scope Global -ErrorAction SilentlyContinue Remove-Variable DD_jti -Scope Global -ErrorAction SilentlyContinue } $headers = @{ 'content-type' = "application/json" } } Process { $Global:DD_BaseUri = "https://$($DD_BaseURI)" $Global:DD_PORT = "3009" $Method = "POST" Write-Verbose $Global:DD_BaseUri switch ($PsCmdlet.ParameterSetName) { default { if (!$($Global:DD_Credentials)) { $DDusername = Read-Host -Prompt "Please Enter DD username" $SecurePassword = Read-Host -Prompt "Password for user $DDusername" -AsSecureString $DD_Credentials = New-Object System.Management.Automation.PSCredential($DDusername, $Securepassword) } $DDpassword = $($DD_Credentials.GetNetworkCredential()).password $Body = [ordered]@{ username = $($DD_Credentials.username) password = $DDpassword } | ConvertTo-Json } 'User' { $DDpassword = $($DD_Credentials.GetNetworkCredential()).password $Body = [ordered]@{ username = $($DD_Credentials.username) password = $DDpassword } | ConvertTo-Json } } Write-Verbose ($body | Out-String) Write-Verbose ( $headers | Out-String ) # Building Hash Literal for unified call $Parameters = @{ body = $body Method = $Method Uri = "$($Global:DD_BaseUri):$($Global:DD_PORT)/$($apiver)/auth" Verbose = $PSBoundParameters['Verbose'] -eq $true Debug = $PSBoundParameters['Debug'] -eq $true } if ($Global:SkipCertificateCheck) { $Parameters.ADD('SkipCertificateCheck', $true) } try { $Result = Invoke-webrequest @Parameters } catch { Get-DDWebException -ExceptionMessage $_ switch ($PsCmdlet.ParameterSetName) { 'USER' { Remove-Variable DD_Credentials } 'CLIENT' { Remove-Variable DD_ClientCredentials } } Break } } End { switch ($PsCmdlet.ParameterSetName) { 'Client' { $Global:DD_ClientCredentials = $DD_ClientCredentials } default { $Global:DD_Credentials = $DD_Credentials } } $Global:DD_Headers = @{ 'X-DD-AUTH-TOKEN' = $($Result.Headers.'X-DD-AUTH-TOKEN') } $Global:DD_UUID = [System.Web.HttpUtility]::UrlEncode($Result.Headers.'X-DD-UUID') Write-Host "Connected to $DD_BaseUri " Write-Output $Result } } function Invoke-DDapirequest { [CmdletBinding(HelpUri = "")] #[OutputType([int])] Param ( [Parameter(Mandatory = $true, ParameterSetName = 'default')] [Parameter(Mandatory = $true, ParameterSetName = 'infile')] $uri, [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $true, ParameterSetName = 'infile')] [ValidateSet('Get', 'Delete', 'Put', 'Post', 'Patch')] $Method, [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $true, ParameterSetName = 'infile')] $Query, [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $false, ParameterSetName = 'infile')] $ContentType = 'application/json', [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $false, ParameterSetName = 'infile')] $apiver = "v1", [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $false, ParameterSetName = 'infile')] $apiport = "$($Global:DD_PORT)", [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $false, ParameterSetName = 'infile')] $DD_BaseUri = $($Global:DD_BaseUri), [Parameter(Mandatory = $false, ParameterSetName = 'default')] [Parameter(Mandatory = $false, ParameterSetName = 'infile')] [ValidateSet('Rest', 'Web')]$RequestMethod, [Parameter(Mandatory = $false, ParameterSetName = 'default')] $Body, [Parameter(Mandatory = $false, ParameterSetName = 'default')] $form, [Parameter(Mandatory = $false, ParameterSetName = 'default')] $Filter, [Parameter(Mandatory = $true, ParameterSetName = 'infile')] $InFile ) $uri = "$($Global:DD_BaseUri):$apiport/$apiver/$uri" if ($Global:DD_Headers) { $Private:Headers = $Global:DD_Headers Write-Verbose ($Headers | Out-String) Write-Verbose "==> Calling $uri" $Parameters = @{ UseBasicParsing = $true Method = $Method Headers = $Private:Headers ContentType = $ContentType Verbose = $PSBoundParameters['Verbose'] -eq $true Debug = $PSBoundParameters['Debug'] -eq $true } switch ($PsCmdlet.ParameterSetName) { 'infile' { $Parameters.Add('InFile', $InFile) } default { if ($Body) { $Parameters.Add('body', $body) Write-Verbose ($body | Out-String) } if ($form) { $Parameters.Add('form', $form) Write-Verbose ($body | Out-String) } if ($query) { $Parameters.Add('body', $query) Write-Verbose $Query | Out-String } if ($filter) { $filterstring = [System.Web.HTTPUtility]::UrlEncode($filter) $filterstring = "filter=$filterstring" Write-Verbose $filterstring | Out-String $uri = "$($uri)?$filterstring" Write-Verbose $uri } } } $Parameters.Add('URI', $uri) if ($Global:SkipCertificateCheck) { $Parameters.Add('SkipCertificateCheck', $True) } Write-Verbose ( $Parameters | Out-String ) try { switch ($RequestMethod) { 'Web' { $Result = Invoke-WebRequest @Parameters } 'Rest' { $Result = Invoke-RestMethod @Parameters } default { $Result = Invoke-WebRequest @Parameters } } } catch { # Write-Warning $_.Exception.Message Get-DDWebException -ExceptionMessage $_ Break } finally { $Global:DD_Headers.Remove('X-DD-SEC-OFFICER') $Global:DD_Headers.Remove('Accept') } } else { Write-Warning "DD_Headers are not present. Did you connect to DD using get-DDtoken ? " break } Write-Output $Result } |