locked
PowerShell Script Text Output RRS feed

  • Question

  • Hi

    I am using the below script and its working accept the text it adds to the file.

    It adds the below to the file:

    <InstrumentationKey>@{InstrumentationKey=xxxxxx-xxxxx-xxxx-xxxxxx-xxxxxxxxx}</InstrumentationKey>

    It should be adding the below without the @{Instrumentaion key}

    <InstrumentationKey>xxxxxx-xxxxx-xxxx-xxxxxx-xxxxxxxxx</InstrumentationKey>

    Does anyone know whey it would be adding the extra text?

    # Check if Application Insights Resource Exists
    
    $EnvironmentName = "Testing"
    $ResourceGroup = "TestRG"
    
    $AIResource = Get-AzureRmApplicationInsights -ResourceGroupName "$ResourceGroup" -Name "$EnvironmentName" -ErrorAction SilentlyContinue
    
    if ($AIResource -eq $null) 
    
    {
    # Create the App Insights Resource
    
    $resource = New-AzureRMResource `
      -ResourceName $EnvironmentName `
      -ResourceGroupName $resourceGroup `
      -Tag @{ applicationType = "web"; applicationName = $EnvironmentName} `
      -ResourceType "Microsoft.Insights/components" `
      -Location "North Europe" `
      -PropertyObject @{"Application_Type"="web"} `
      -Force
    
    
    }
    
    $Key = Get-AzureRmApplicationInsights -ResourceGroupName "$ResourceGroup" -Name "$EnvironmentName" | select InstrumentationKey
    
    
    # Modify Application Insights Config Files
    
    $fileNames = "C:\temppath\ApplicationInsights.config","c:\Temp\ApplicationInsights.config"
    $pattern = '<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">'
    $IKey1 = "<InstrumentationKey>"
    $IKey2 = "</InstrumentationKey>"
    
    Foreach ($fileName in $fileNames)
    {
    (Get-Content $fileName) | 
        Foreach-Object {
            $_ # send the current line to output
            if ($_ -match "$pattern") 
            {
                #Add Lines after the selected pattern 
                "<InstrumentationKey>$Key</InstrumentationKey>"
            }
        } | Set-Content $fileName
    
    }

    Tuesday, April 23, 2019 8:53 AM

Answers

  • Your variable $Key is an array. To extract this array you should use -ExpandProperty for your Select-Object like this:
    $Key = Get-AzureRmApplicationInsights -ResourceGroupName $ResourceGroup -Name $EnvironmentName | Select-Object -ExpandProperty InstrumentationKey

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''




    • Marked as answer by Shane W Tuesday, April 23, 2019 10:38 AM
    • Edited by BOfH-666 Tuesday, April 23, 2019 10:54 AM
    Tuesday, April 23, 2019 10:17 AM

All replies

  • Your variable $Key is an array. To extract this array you should use -ExpandProperty for your Select-Object like this:
    $Key = Get-AzureRmApplicationInsights -ResourceGroupName $ResourceGroup -Name $EnvironmentName | Select-Object -ExpandProperty InstrumentationKey

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''




    • Marked as answer by Shane W Tuesday, April 23, 2019 10:38 AM
    • Edited by BOfH-666 Tuesday, April 23, 2019 10:54 AM
    Tuesday, April 23, 2019 10:17 AM
  • That worked perfectly, thanks for the help.

    Cheers

    Shane

    Tuesday, April 23, 2019 10:38 AM