# Write info to Log Analytics

**URL:** <https://forums.powershell.org/t/write-info-to-log-analytics/22965>\
**Category:** PowerShell Help\
**Created:** [October 9, 2023, 10:26pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965 "2023-10-09T22:26:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mh7777](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mh7777](https://forums.powershell.org/u/mh7777)\
**Post date:** [October 9, 2023, 10:26pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/1 "2023-10-09T22:26:15Z")

</div>

I have this code:

```auto

# Define your Log Analytics Workspace ID and Key
$WorkspaceID = "d929aff4-6e8a-47f8-94a5-a8466376b044"
$WorkspaceKey = "oDmfRIXSTV7irLLTloKOQjBYwiSNE+rG5i/j7SCEEvcvrD/2BH0P899TOCl6KZfkXO5/r3oPIZ1RlzMWddX5EQ=="

# Define the registry key path and value names
$RegistryPath = "HKLM:\SOFTWARE\DELL\Docks"
$RegistryValueNames = @("Model", "Firmware", "AssetTag")

# Get the registry values
$RegistryValues = @{}
ForEach ($ValueName in $RegistryValueNames) {
    $RegistryValue = Get-ItemProperty -Path $RegistryPath | Select-Object -ExpandProperty $ValueName
    $RegistryValues[$ValueName] = $RegistryValue
}

# Create a custom log entry
$LogEntry = @{
    "RegistryKey" = $RegistryPath
    "Model" = $RegistryValues["Model"]
    "Firmware" = $RegistryValues["Firmware"]
    "AssetTag" = $RegistryValues["AssetTag"]
    $Timestamp = [System.DateTime]::UtcNow.ToString("r")
}

# Convert the log entry to JSON
$JsonLogEntry = $LogEntry | ConvertTo-Json

# Create a timestamp
$Timestamp = Get-Date -Format "yyyy-MM-ddTHH:mm:ss"

# Create a signature for authentication
$Signature = [System.Text.Encoding]::UTF8.GetBytes("POST`n$jsonLogEntry`n$Timestamp")
$HMACSHA256 = New-Object System.Security.Cryptography.HMACSHA256
$HMACSHA256.Key = [System.Convert]::FromBase64String($WorkspaceKey)
$Hash = $HMACSHA256.ComputeHash($Signature)
$Signature = [System.Convert]::ToBase64String($Hash)

# Create headers for the request
$Headers = @{
    "Log-Type" = "CustomRegistryData" # Define your Log Type
    "x-ms-Date" = $Timestamp
    "Authorization" = "SharedKey $WorkspaceID $Signature"
}

# Send data to Azure Log Analytics
$Uri = "https://$WorkspaceID.ods.opinsights.azure.com/api/logs?api-version=2016-04-01"

Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body $JsonLogEntry -UseBasicParsing

Write-Host "Registry information sent to Azure Log Analytics."

```

when I run it I get the following error:

```auto
Invoke-RestMethod : {"Error":"InvalidAuthorization","Message":"An invalid date 
format used in the x-ms-date header."}
At C:\Users\Test\OneDrive\Scripts\PowerShell\DELL\SVEN\My Log 
Analytics\testing.ps1:53 char:1
+ Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body $Jso ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt 
   pWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe 
   ll.Commands.InvokeRestMethodCommand

```

also, nothing gets written up to Intune. Any help would be great. Also, if anyone has a good doc or video that clearly shows how to get information written up to Intune / Log Analytics using PowerShell that would be awesome.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 10, 2023, 1:01pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/2 "2023-10-10T13:01:45Z")

</div>

It looks like the date format should be in RFC 1123 format, try:

```auto
$timeStamp = Get-Date -Format "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"

```

> **[Representation of date/time values in headers (REST API) - Azure Storage](https://learn.microsoft.com/en-us/rest/api/storageservices/representation-of-date-time-values-in-headers)**
>
> Representation of date/time values in headers.

---

<div class="post-metadata">

**Author:** ![mh7777](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mh7777](https://forums.powershell.org/u/mh7777)\
**Post date:** [October 10, 2023, 3:35pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/3 "2023-10-10T15:35:08Z")

</div>

Thank you that time stamp worked. But now I get a new error.

```auto
Invoke-RestMethod : The remote name could not be resolved: 
'd929aff4-6e8a-478f-94a5-a8415376b934.ods.opinsights.azure.com'
At line:53 char:1

```

I have no proxy setup in my environment and I’ve confirmed that I have the correct WorkspaceID (shown above)

Thank you.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 10, 2023, 5:29pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/4 "2023-10-10T17:29:17Z")

</div>

> [@mh7777](#):
>
> `"Authorization" = "SharedKey $WorkspaceID $Signature"`

Don’t believe it would cause your error, but the above should be:

```auto
"Authorization" = "SharedKey $WorkspaceID:$Signature"

```

Might be worth fixing that and retesting it just to rule it out.

---

<div class="post-metadata">

**Author:** ![mh7777](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mh7777](https://forums.powershell.org/u/mh7777)\
**Post date:** [October 10, 2023, 6:48pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/5 "2023-10-10T18:48:05Z")

</div>

the only thing that works is if I use a Colon with no space before or after. But then I get the same remote name could not be resolved 🙂

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 10, 2023, 8:23pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/6 "2023-10-10T20:23:00Z")

</div>

Relevant?

> **[Unable to Connect Log Analytic Agent to Log Analytic Workspace via custom DNS...](https://learn.microsoft.com/en-us/answers/questions/557905/unable-to-connect-log-analytic-agent-to-log-analyt)**
>
> Hi all,    
> When I am connecting log analytic agent to log analytic workspace via azure provided dns server, it is working fine, however when I changed the dns server to a custom one, then it failed with following message: …

> **[Name resolution for resources in Azure virtual networks](https://learn.microsoft.com/en-us/azure/virtual-network/virtual-networks-name-resolution-for-vms-and-role-instances?tabs=redhat#vms-and-role-instances)**
>
> Name resolution scenarios for Azure IaaS, hybrid solutions, between different cloud services, Active Directory, and using your own DNS server.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/write-info-to-log-analytics/22965/7 "2024-05-16T20:14:38Z")

</div>

Closed on May 16, 2024, 8:14pm UTC


