Get-ADORepository.ps1
function Get-ADORepository { <# .Synopsis Gets repositories from Azure DevOps .Description Gets the repositories from Azure DevOps. By default, this will return the project's git repositories. Azure DevOps repositories can have more than one type of SourceProvider. To list the Source Providers, use -SourceProvider We can get repositories for a given -ProviderName .Example Get-ADORepository -Organization StartAutomating -Project PSDevOps .Link Remove-ADORepository .Link https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/list%20repositories?view=azure-devops-rest-5.1 #> [CmdletBinding(DefaultParameterSetName='git/repositories')] param( # The Organization [Parameter(Mandatory,ValueFromPipelineByPropertyName)] [Alias('Org')] [string] $Organization, # The Project [Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Project, # The Repository ID [Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)] [string] $RepositoryID, # If set, will list repository source providers [Parameter(Mandatory,ParameterSetName='sourceproviders',ValueFromPipelineByPropertyName)] [Alias('SourceProviders')] [switch] $SourceProvider, # The name of the Source Provider. This will get all repositories associated with the project. # If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required [Parameter(Mandatory,ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)] [Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] [Alias('EndpointType')] [string] $ProviderName, # The name of the Source Provider. This will get all repositories associated with the project. # If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required [Parameter(ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)] [Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] [string] $EndpointID, # The name of the repository [Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] [string] $RepositoryName, # The path within the repository. # To use this parameter, -ProviderName is also required, and -EndpointID will be required if the -ProviderName is not TFVC or TFGit [Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] [string] $Path, # The commit or branch. By default, Master. [Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] [string] $CommitOrBranch = 'master', # If set, will include the parent repository [Parameter(ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)] [switch] $IncludeParent, # If set, will get repositories from the recycle bin [Parameter(Mandatory,ParameterSetName='git/recycleBin/repositories',ValueFromPipelineByPropertyName)] [Alias('RecycleBin')] [switch] $Recycled, # The server. By default https://dev.azure.com/. # To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs). [Parameter(ValueFromPipelineByPropertyName)] [uri] $Server = "https://dev.azure.com/", # The api version. By default, 5.1-preview. # If targeting TFS, this will need to change to match your server version. # See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops [string] $ApiVersion = "5.1-preview", # A Personal Access Token [Alias('PAT')] [string] $PersonalAccessToken, # Specifies a user account that has permission to send the request. The default is the current user. # Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. [pscredential] [Management.Automation.CredentialAttribute()] $Credential, # Indicates that the cmdlet uses the credentials of the current user to send the web request. [Alias('UseDefaultCredential')] [switch] $UseDefaultCredentials, # Specifies that the cmdlet uses a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. [uri] $Proxy, # Specifies a user account that has permission to use the proxy server that is specified by the Proxy parameter. The default is the current user. # Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. # This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command. [pscredential] [Management.Automation.CredentialAttribute()] $ProxyCredential, # Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is specified by the Proxy parameter. # This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command. [switch] $ProxyUseDefaultCredentials ) begin { #region Copy Invoke-ADORestAPI parameters $invokeParams = & $getInvokeParameters $PSBoundParameters #endregion Copy Invoke-ADORestAPI parameters } process { $uri = # The URI is comprised of: @( "$server".TrimEnd('/') # the Server (minus any trailing slashes), $Organization # the Organization, $Project # the Project, '_apis' # the API Root ('_apis'), (. $ReplaceRouteParameter $PSCmdlet.ParameterSetName) # and any parameterized URLs in this parameter set. ) -as [string[]] -ne '' -join '/' $uri += '?' # The URI has a query string containing: $uri += @( if ($IncludeParent) { # includeParent=True (if it was set) "includeParent=True" } if ($EndpointID) { "serviceEndpointId=$EndpointID" } if ($repositoryName) { "repository=$repositoryName" "commitOrBranch=$CommitOrBranch" } if ($path) { "path=$path" } if ($ApiVersion) { # and the apiVersion "api-version=$ApiVersion" } ) -join '&' $InvokeParams.Property = @{ # Because we want to pipeline properly, also return the -Organization and -Project as properties. Organization = $Organization Project = $Project } $subTypeName = if ($psCmdlet.ParameterSetName -eq 'git/recycleBin/repositories') { '.Recycled' } elseif ($psCmdlet.ParameterSetName -eq 'sourceProviders') { '.SourceProvider' } elseif ($psCmdlet.ParameterSetName -eq 'sourceproviders/{ProviderName}/repositories') { ".$ProviderName.Repository" $invokeParams.ExpandProperty = 'repositories' $invokeParams.Property.EndpointID = $EndpointID $invokeParams.Property.ProviderName = $ProviderName } else { '' } # Invoke the ADO Rest API. Invoke-ADORestAPI @invokeParams -Uri $uri -PSTypeName @( # Because we want to format the output, decorate the object with the following typenames: "$Organization.$Project.Repository$subTypeName" # * $Organization.$Project.Repository$SubTypeName "$Organization.Repository$subTypeName" # * $Organization.Repository$SubTypeName "StartAutomating.PSDevOps.Repository$subTypeName" # * PSDevOps.Repository$SubTypeName ) } } |