AzDevOps.psm1

# .ExternalHelp AzDevOps-help.xml
Write-Verbose 'Importing from [D:\CODE\projects\Azure-Devops-PowerShell-Module\AzDevOps\AzDevOps\AzDevOps]'
# .ConnectOrganization
function Connect-Organization
{
 [CmdletBinding(
  HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/core/blob/master/docs/Connect-AdoOrganization.md#connect-adoorganization',
  PositionalBinding = $true)]
 [OutputType([String])]
 param (
  [Parameter(Mandatory = $true)]
  [string]$Orgname,
  [Parameter(Mandatory = $true)]
  [string]$PAT,
  [Parameter(Mandatory = $false)]
  [ValidateSet('5.1', '7.1-preview.4')]
  [string]$ApiVersion = '7.1-preview.4'
 )
 begin
 {
  Write-Verbose "ConnectOrganization : Begin Processing";
  Write-Verbose " Orgname : $($Orgname)";
  Write-Verbose " ApieVersion : $($ApiVersion)";
  try
  {
   $ErrorActionPreference = 'Stop';
   $Error.Clear();
   $azDevOpsHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) };
   $azDevOpsOrg = "https://dev.azure.com/$($Orgname)/";

   $uriProjects = $azDevOpsOrg + "_apis/projects?api-version=$($ApiVersion)";
   $Result = Invoke-AdoEndpoint -Uri ([System.Uri]::new($uriProjects)) -Method get -Headers $azDevOpsHeader -Verbose:$VerbosePreference;
   if ($Result.GetType().Name -ne 'String')
   {
    Set-Variable -Name azDevOpsHeader -Value $azDevOpsHeader -Scope Global;
    Set-Variable -Name azDevOpsOrg -Value $azDevOpsOrg -Scope Global;
    Set-Variable -Name azDevOpsConnected -Value $true -Scope Global;
    return "Connected to $($azDevOpsOrg)"
   }
  }
  catch
  {
   throw $_;
  }
 }
}
# .InvokeEndpoint
function Invoke-Endpoint
{
 [CmdletBinding(
  HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Invoke-AdoEndpoint.md#invoke-adoendpoint',
  PositionalBinding = $true)]
 [OutputType([Object])]
 param (
  [Parameter(Mandatory = $true)]
  [System.Uri]$Uri,
  [Parameter(Mandatory = $true)]
  [validateset('GET', 'PATCH', 'POST', 'PUT', 'DELETE')]
  [string]$Method,
  [Parameter(Mandatory = $false)]
  [hashtable]$Headers = $Global:azDevOpsHeader,
  [Parameter(Mandatory = $false)]
  [string]$ContentType,
  [Parameter(Mandatory = $false)]
  [string]$Body
 )
 begin
 {
  Write-Verbose "InvokeRestMethod : Begin Processing";
  Write-Verbose " Uri : $($Uri.AbsoluteUri)";
  Write-Verbose " Method : $($Method)";
  Write-Verbose " Headers : $($Headers)";
  try
  {
   $ErrorActionPreference = 'Stop';
   $Error.Clear();
   #
   # Are we connected
   #
   switch ($Method)
   {
    "POST"
    {
     return (Invoke-RestMethod -Uri $Uri.AbsoluteUri -Method $Method -Headers $Headers -ContentType $ContentType -Body $Body -Verbose:$VerbosePreference);
    }
    "PUT"
    {
     return (Invoke-RestMethod -Uri $Uri.AbsoluteUri -Method $Method -Headers $Headers -ContentType $ContentType -Body $Body -Verbose:$VerbosePreference);
    }
    "PATCH"
    {
     return (Invoke-RestMethod -Uri $Uri.AbsoluteUri -Method $Method -Headers $Headers -ContentType $ContentType -Body $Body -Verbose:$VerbosePreference);
    }
    default
    {
     return (Invoke-RestMethod -Uri $Uri.AbsoluteUri -Method $Method -Headers $Headers -Verbose:$VerbosePreference);
    }
   }
   else
   {
   }
  }
  catch
  {
   throw $_;
  }
 }
}