<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Exporting to CSV in Dynatrace API</title>
    <link>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245899#M3126</link>
    <description>&lt;P&gt;I am trying to export 2 metrics of a specific management zone to CSV: the number of transactions and response time, however I get the following error with this powershell code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }&lt;/P&gt;
&lt;P&gt;$DYNATRACE_API_TOKEN = 'tu_token_de_acceso'&lt;BR /&gt;$ZONA_GESTION = 'Mngz-WSZonaClienteSFC'&lt;/P&gt;
&lt;P&gt;$METRICA_TRANSACCIONES = "builtin:service.requestCount.total:filter(eq(mzName,'$ZONA_GESTION')):splitBy()"&lt;BR /&gt;$METRICA_TIEMPO_RESPUESTA = "builtin:service.response.time:filter(eq(mzName,'$ZONA_GESTION')):splitBy()"&lt;/P&gt;
&lt;P&gt;$respuesta_transacciones = Invoke-RestMethod -Uri "https://{tu-dynatrace-managed}/e/{tu-environment-id}/api/v2/metrics/query?metricSelector=$METRICA_TRANSACCIONES" -Headers @{ "Authorization" = "Api-Token $DYNATRACE_API_TOKEN"; "Content-Type" = "application/json" } -Method Get&lt;BR /&gt;$respuesta_tiempo_respuesta = Invoke-RestMethod -Uri "https://{tu-dynatrace-managed}/e/{tu-environment-id}/api/v2/metrics/query?metricSelector=$METRICA_TIEMPO_RESPUESTA" -Headers @{ "Authorization" = "Api-Token $DYNATRACE_API_TOKEN"; "Content-Type" = "application/json" } -Method Get&lt;/P&gt;
&lt;P&gt;if ($respuesta_transacciones -eq $null -or $respuesta_tiempo_respuesta -eq $null) {&lt;BR /&gt;Write-Output "Error al obtener los datos."&lt;BR /&gt;exit&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;$transacciones = $respuesta_transacciones.result[0].data | ForEach-Object { [PSCustomObject]@{ ServiceID = $_.dimensions[0]; Transactions = $_.values[0] } }&lt;BR /&gt;$tiempo_respuesta = $respuesta_tiempo_respuesta.result[0].data | ForEach-Object { [PSCustomObject]@{ ServiceID = $_.dimensions[0]; ResponseTime = $_.values[0] } }&lt;/P&gt;
&lt;P&gt;$datos_combinados = $transacciones | ForEach-Object {&lt;BR /&gt;$id = $_.ServiceID&lt;BR /&gt;$respuesta = $tiempo_respuesta | Where-Object { $_.ServiceID -eq $id }&lt;BR /&gt;[PSCustomObject]@{ ServiceID = $_.ServiceID; Transactions = $_.Transactions; ResponseTime = $respuesta.ResponseTime }&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;$datos_combinados | Export-Csv -Path "dynatrace_service_metrics.csv" -NoTypeInformation&lt;BR /&gt;Write-Output "Datos exportados exitosamente a dynatrace_service_metrics.csv"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The error is: line 6 character 28 and line 7 char 31: Invalid operation (system.net.nhttpwebrequest:httpwebrequest)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;fullyqualifiederrorid: webcmdletwebresponseexception,microsoft.powershell.commands.invokerestmethodcommand&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I am doing wrong? Thanks.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 May 2024 07:20:56 GMT</pubDate>
    <dc:creator>geonaute123</dc:creator>
    <dc:date>2024-05-20T07:20:56Z</dc:date>
    <item>
      <title>Exporting to CSV</title>
      <link>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245899#M3126</link>
      <description>&lt;P&gt;I am trying to export 2 metrics of a specific management zone to CSV: the number of transactions and response time, however I get the following error with this powershell code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }&lt;/P&gt;
&lt;P&gt;$DYNATRACE_API_TOKEN = 'tu_token_de_acceso'&lt;BR /&gt;$ZONA_GESTION = 'Mngz-WSZonaClienteSFC'&lt;/P&gt;
&lt;P&gt;$METRICA_TRANSACCIONES = "builtin:service.requestCount.total:filter(eq(mzName,'$ZONA_GESTION')):splitBy()"&lt;BR /&gt;$METRICA_TIEMPO_RESPUESTA = "builtin:service.response.time:filter(eq(mzName,'$ZONA_GESTION')):splitBy()"&lt;/P&gt;
&lt;P&gt;$respuesta_transacciones = Invoke-RestMethod -Uri "https://{tu-dynatrace-managed}/e/{tu-environment-id}/api/v2/metrics/query?metricSelector=$METRICA_TRANSACCIONES" -Headers @{ "Authorization" = "Api-Token $DYNATRACE_API_TOKEN"; "Content-Type" = "application/json" } -Method Get&lt;BR /&gt;$respuesta_tiempo_respuesta = Invoke-RestMethod -Uri "https://{tu-dynatrace-managed}/e/{tu-environment-id}/api/v2/metrics/query?metricSelector=$METRICA_TIEMPO_RESPUESTA" -Headers @{ "Authorization" = "Api-Token $DYNATRACE_API_TOKEN"; "Content-Type" = "application/json" } -Method Get&lt;/P&gt;
&lt;P&gt;if ($respuesta_transacciones -eq $null -or $respuesta_tiempo_respuesta -eq $null) {&lt;BR /&gt;Write-Output "Error al obtener los datos."&lt;BR /&gt;exit&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;$transacciones = $respuesta_transacciones.result[0].data | ForEach-Object { [PSCustomObject]@{ ServiceID = $_.dimensions[0]; Transactions = $_.values[0] } }&lt;BR /&gt;$tiempo_respuesta = $respuesta_tiempo_respuesta.result[0].data | ForEach-Object { [PSCustomObject]@{ ServiceID = $_.dimensions[0]; ResponseTime = $_.values[0] } }&lt;/P&gt;
&lt;P&gt;$datos_combinados = $transacciones | ForEach-Object {&lt;BR /&gt;$id = $_.ServiceID&lt;BR /&gt;$respuesta = $tiempo_respuesta | Where-Object { $_.ServiceID -eq $id }&lt;BR /&gt;[PSCustomObject]@{ ServiceID = $_.ServiceID; Transactions = $_.Transactions; ResponseTime = $respuesta.ResponseTime }&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;$datos_combinados | Export-Csv -Path "dynatrace_service_metrics.csv" -NoTypeInformation&lt;BR /&gt;Write-Output "Datos exportados exitosamente a dynatrace_service_metrics.csv"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The error is: line 6 character 28 and line 7 char 31: Invalid operation (system.net.nhttpwebrequest:httpwebrequest)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;fullyqualifiederrorid: webcmdletwebresponseexception,microsoft.powershell.commands.invokerestmethodcommand&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I am doing wrong? Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2024 07:20:56 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245899#M3126</guid>
      <dc:creator>geonaute123</dc:creator>
      <dc:date>2024-05-20T07:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV</title>
      <link>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245901#M3127</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/75324"&gt;@geonaute123&lt;/a&gt;!&lt;/P&gt;&lt;P&gt;First of all, I'm not a PowerShell Script Guru, but I'll try to help you.&lt;/P&gt;&lt;P&gt;I did some changes in your code and I'll let my code snippet and my explanation below. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="php"&gt;# Disable SSL certificate validation (use with caution)
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

# Define variables
$DYNATRACE_API_TOKEN = 'your_access_token'
$ZONA_GESTION = 'Mngz-WSZonaClienteSFC'
$DYNATRACE_MANAGED = 'your_dynatrace_managed'
$ENVIRONMENT_ID = 'your_environment_id'

# Define metric selectors
$METRICA_TRANSACCIONES = "builtin:service.requestCount.total:filter(eq(mzName,'$ZONA_GESTION')):splitBy()"
$METRICA_TIEMPO_RESPUESTA = "builtin:service.response.time:filter(eq(mzName,'$ZONA_GESTION')):splitBy()"

# Perform API requests with error handling
try {
    $respuesta_transacciones = Invoke-RestMethod -Uri "https://$DYNATRACE_MANAGED/e/$ENVIRONMENT_ID/api/v2/metrics/query?metricSelector=$($METRICA_TRANSACCIONES)" -Headers @{ "Authorization" = "Api-Token $DYNATRACE_API_TOKEN"; "Content-Type" = "application/json" } -Method Get
    $respuesta_tiempo_respuesta = Invoke-RestMethod -Uri "https://$DYNATRACE_MANAGED/e/$ENVIRONMENT_ID/api/v2/metrics/query?metricSelector=$($METRICA_TIEMPO_RESPUESTA)" -Headers @{ "Authorization" = "Api-Token $DYNATRACE_API_TOKEN"; "Content-Type" = "application/json" } -Method Get
} catch {
    Write-Output "Error making the request: $_"
    exit
}

# Verify responses are not null
if ($respuesta_transacciones -eq $null -or $respuesta_tiempo_respuesta -eq $null) {
    Write-Output "Error obtaining data."
    exit
}

# Verify the response structure
if (-not $respuesta_transacciones.result -or -not $respuesta_tiempo_respuesta.result) {
    Write-Output "Unexpected response structure."
    exit
}

# Process the transaction data
$transacciones = $respuesta_transacciones.result[0].data | ForEach-Object { 
    [PSCustomObject]@{ 
        ServiceID = $_.dimensions[0]; 
        Transactions = $_.values[0] 
    } 
}

# Process the response time data
$tiempo_respuesta = $respuesta_tiempo_respuesta.result[0].data | ForEach-Object { 
    [PSCustomObject]@{ 
        ServiceID = $_.dimensions[0]; 
        ResponseTime = $_.values[0] 
    } 
}

# Combine the data based on ServiceID
$datos_combinados = $transacciones | ForEach-Object {
    $id = $_.ServiceID
    $respuesta = $tiempo_respuesta | Where-Object { $_.ServiceID -eq $id }
    [PSCustomObject]@{ 
        ServiceID = $_.ServiceID; 
        Transactions = $_.Transactions; 
        ResponseTime = $respuesta.ResponseTime 
    }
}

# Export combined data to CSV
$datos_combinados | Export-Csv -Path "dynatrace_service_metrics.csv" -NoTypeInformation
Write-Output "Data successfully exported to dynatrace_service_metrics.csv"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Variable Definitions&lt;/STRONG&gt;:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Ensure that the $DYNATRACE_MANAGED and $ENVIRONMENT_ID variables are correctly defined.&lt;/LI&gt;&lt;LI&gt;Properly interpolate variables within the URL by using $($variable) syntax.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;Error Handling&lt;/STRONG&gt;:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Added try-catch blocks around the API requests to catch and display any errors that occur during the HTTP requests.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;Response Verification&lt;/STRONG&gt;:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Added checks to verify that the responses are not null and have the expected structure before processing the data.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;Processing and Combining Data&lt;/STRONG&gt;:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Used ForEach-Object to process the transaction and response time data.&lt;/LI&gt;&lt;LI&gt;Combined the data based on the ServiceID to ensure that the final CSV contains both metrics for each service.&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Fri, 17 May 2024 08:56:03 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245901#M3127</guid>
      <dc:creator>MaximilianoML</dc:creator>
      <dc:date>2024-05-17T08:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV</title>
      <link>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245907#M3128</link>
      <description>&lt;P&gt;Thanks for the answer. I am trying to get these 2 metrics to a CSV from a list of host that pertains to the "&lt;SPAN&gt;Mngz-WSZonaClienteSFC" management zone.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2024 10:26:47 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Dynatrace-API/Exporting-to-CSV/m-p/245907#M3128</guid>
      <dc:creator>geonaute123</dc:creator>
      <dc:date>2024-05-17T10:26:47Z</dc:date>
    </item>
  </channel>
</rss>

