# Downloads an archive with reporting statistics for the specified tenant
function GetDataFile {
Param (
[Parameter(Mandatory=$true)][String]$AppServer,
[Parameter(Mandatory=$true)][AllowEmptyString()][string]$TenantName,
[Parameter(Mandatory=$true)][string]$User,
[Parameter(Mandatory=$true)][string]$Password,
[Parameter(Mandatory=$true)][Int]$Year,
[Parameter(Mandatory=$true)][Int]$Month,
[Parameter(Mandatory=$true)][string]$DownloadPath,
[Parameter(Mandatory=$true)][string]$ReportingServiseUri
)
# Use the tenant name and the application server address passed in the function parameters
$URI = "https://$AppServer/Flexicapture12/Server/FCAuth/API/wsdl?Tenant=$TenantName"
# Create a PSCredential object with the given username and password
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList @($user,(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Create a proxy web service that will authenticate with these credentials
$proxy = New-WebServiceProxy -Uri $URI -Credential $creds
$proxy.Url = "https://$AppServer/flexicapture12/Server/FCAuth/API/Soap?Tenant=$TenantName"
$proxy.Credentials = $creds
# And use it to ask FlexiCapture API for the token
$ticket = $proxy.GetCurrentUserAuthTicket()
$nameFile = $year.ToString() + "-" + $month.ToString()
# Create path for download file
$path = "$DownloadPath\$nameFile.zip"
# Create a request with Bearer authentication header containing the token
# and send it to Reporting data access service to get the archive for the specified tenant and period
$headers = @{ Authorization = "Bearer " + $ticket}
Invoke-RestMethod -Uri $ReportingServiseUri"?tenantName=$TenantName&year=$year&month=$month" -Headers $headers -OutFile $path
}
GetDataFile