powershellautomationazure-devopsazure-devops-analytics

Download audit logs in csv from azure DevOps UI using powershell


Hi I am looking for a script which can download export logs in csv from azure DevOps using powershell for given time frame. like I can enter time frame and then script will download and return csv file from azure DevOps ui -

enter image description here

I found this script here https://developercommunity.visualstudio.com/content/problem/615053/question-we-want-to-get-the-export-audit-log-using.html

I am not sure what username , password and url to use here

$Username = 'domain\user'
$Password = 'password'
$Url = "https://server:8080/tfs/_api/_licenses/Export"
$Path = "D:\temp\data.csv"
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object    
System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $url, $path )

I am not able to find any more data on this


Solution

  • The sample you shared is download audit log form Azure DevOps Server, the url is https://{your_server}/tfs/_api/_licenses/Export, username , password are your username and password to log on to TFS. Note: Do not forget add domain name before the username.

    If you are using Azure DevOps Service, you can try this REST API and power shell script to save the Audit log.

    $outfile = "{Local path}"
    $connectionToken="{PAT}"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    $AuditLogURL = "https://auditservice.dev.azure.com/{organization}/_apis/audit/downloadlog?format=csv&startTime={startTime}&endTime={endTime}&api-version=6.1-preview.1" 
    $AuditInfo = Invoke-RestMethod -Uri $AuditLogURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get –OutFile $outfile
    

    Result:

    enter image description here