Translate

Showing posts with label Azure. Show all posts
Showing posts with label Azure. Show all posts

Wednesday, January 21, 2026

Powershell - how to gather data with your scripts in Azure Log Analytics

For specific use cases (e.g. to collect the state of your secure boot certificate updates) it might be required to run a script on your hosts and collect the data centrally. The best way to achieve this is to use Azure Log Analytics. 

Here is how you can achieve this:


1. Setup the Azure Log Analytics Workspace in Azure 

1.1 Search in the Azure Portal for "Azure Log Analytics Workspace" and add a new one.

Hint: Formerly Microsoft was showing this data in the portal but they removed it. Its still supported but not the preferred method anymore. Any other methods does not allow the PowerShell based upload. Instead you need the AMA client to gather data. Which does not work for a PowerShell script based solutions were you want to gather your own custom generated data.


2. Gather the required data (WorkspaceID and Preshared Key) for your script

2.1 connect with Powershell to your Subscription

2.2 Ensure Azure modules are loaded (Import-Module Az) or you use the Azure Cloud shell (easiest method)

2.3 Retrieve the Workspace ID (replace RessourceGroup name and Workspace name!)

(Get-AzOperationalInsightsWorkspace -ResourceGroupName rg-xxx -Name "law-xxx-test").CustomerID



Shown ID is your Workspace ID (note/copy it we need it later again!)


2.4 Retrieve the Workspace Keys for uploading

Get-AzOperationalInsightsWorkspaceSharedKeys -ResourceGroupName rg-xxx -Name "law-xxx-test"


Shown SharedKeys are required for the script as well. Please note / copy them and keep the data in a save place!

3. Powershell Code to send some data
For demonstration purposes we gather hostnamen and IP addresses. So you can simply modify the relevant part. This thing will generate your first data.

If there is no log created with this name (here MyHostLog) then it will be generated automatically for you when you send the first data.

Keep in mind you need to modify the former captured WorkspaceID and Workspace Key (I would recommend you use the primary key).

function Send-Ip {
 # Function to log Host Name and IP Address to custom log in Azure Log Analytics!
 # Simply use start-transcript at the beginning your script and 
 # stop-transcript as last line in your script for easy logging. 
 # This will catch up all the write-host and other output as well!
    Write-Host "Log Host Name and IP Address to custom log"
    $apiVersion = "2016-04-01"
 # IMPORTANT! Adjust the code with your own data gathered in step 2!
    $workspaceId = "11111111-2222-3333-4444-555555555555"
    $workspaceKey = "wJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0ww=="
   
 # Name of your log in your Azure Log Analytics Workspace
    $logType = "MyHostLog"
 # Generation of some sample custom log data. Change this for your purpose
    $hostname = $env:COMPUTERNAME
    $ipAddress = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -match "Ethernet*" }).IPAddress
    $timestamp = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
 # Creation of the log entry. Its good to have a timestamp and may be a host reference if you want! And add here whatever you want!
    $logEntry = @(
        @{
            TimeGenerated = $timestamp
            Hostname      = $hostname
            IPAddress     = $ipAddress
        }
    )
 # Generate log entry body and header to send. It will be encrypted and signed with your shared Workspace key from above.
    $body = $logEntry | ConvertTo-Json -Depth 3
    # Ensure the date is in RFC1123 format
    $date = [DateTime]::UtcNow.ToString("r")
    # Construct the string to sign
    $stringToHash = "POST`n$($body.Length)`napplication/json`nx-ms-date:$date`n/api/logs"
    $hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha256.Key = [Convert]::FromBase64String($workspaceKey)
    $hash = $hmacsha256.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToHash))
    $signature = [Convert]::ToBase64String($hash)
    $authkey = $workspaceId + ":" + $signature
    $authorization = "SharedKey $authkey"
    $headers = @{
        "Content-Type"  = "application/json"
        "Authorization" = $authorization
        "Log-Type"      = $logType
        "x-ms-date"     = $date
    }
# Lets look into the header we have created (this is just for reference in your local logs if you want!)
   Write-Host "Header:"
   $headers
   Write-Host "=================================================="
# Lets look into the body we have created (this is just for reference in your local logs if you want!)
    Write-Host "Body:"
    $body
# Now lets send the data to the workspace (may be you want to adjust the catch block & finally text)
    try {
                $response = Invoke-RestMethod -Method Post -Uri "https://$workspaceId.ods.opinsights.azure.com/api/logs?api-version=$apiVersion" -Headers $headers -Body $body
                # Output the response (so it got catched in start-transcript log, no response is fine as well!)
                Write-Host "Response: $response"         
        }
        catch {
            Write-Error "Failed to send the IpReport: $_"
        }
        finally {
            Write-Host "Processing done for IP reporting!"
        }
}
# Now call the function
Send-Ip


Thats it!  Simply ajdust the sample code as you want. 

You find in Azure Log Analytics workspace table "MyHostLog_CL" your first new entry:



Enjoy the show!

Sunday, April 3, 2022

Azure - How to move resources between subscriptions under different tenants

To make a long story short. Directly this does not work at least until now (2022-04). But as often there is a good workaround.

ATTENTION! This is an unsupported workaround. So do not blame me if you loose something important. Backup (e.g. ARM Templates, data etc.) is always a good idea! So this is out of any warranty. These steps were working a couple times for me but that does not mean it will work for you. THIS IS ON YOUR OWN RISK!

First of all you need to know. Not all resources can be moved. Check here which resource you can move.
Move operation support by resource type - Azure Resource Manager | Microsoft Docs

You need to differentiate between move from:

  • Resource to another resource group
  • Resource to another subscription
  • Resource to another region

Also keep mind some pieces you can not move at all. Whenever you have some access assignments based on your active directory you will loose them and you need to recreate them in a another tenants active directory.

Also if you have some scripts or resource identifiers you may need to update them as well.

As a direct move is not possible you can do it with a intermediate "Pay as you go" subscription.
This type of subscription can easily move between tenants. But you need to be member of the tenant and need the right to add subscriptions (in my case I am global admin so I do not care. But you need to be at least Account Administrator).


And then simply move the subscription from one to another directory. All directories your account had sufficient access should be shown in the selection list.

Therefore go to to your resource group and ensure you have selected all resources.

Then click on MOVE (dropdown) in the top toolbar  -> Select there the "Move to another subscription".

While you do this you are asked for the right subscription. In case of the transfer you use the Pay as you go one. And in the second step you repeat it and you select your final target subscription.

While you do this you need to move the resources into a new resource group name. I use there the old one and add an -payg at the end. And in the target subscription I use another extension. But that's your choice what you use there. It must be just different then the one you use in the pay as you go subscription.

Actually you do the same thing twice (moving resources between subscriptions):
1. Moving resources from source tenant resource group to PAYG-subscription into an intermediate resource group.

2. Moving the PAYG-subscription to another tenant.

3. Ensuring you are owner of the PAYG-subscription in the target tenant.

4. Moving the resources from the PAYG-subscription to the target subscription in the target tenant.

And do that stuff immediately. Otherwise the PAYG subscription might be charged for you! So do not wait one or a few days in between! Especially if you have to pay with your own credit card!

Are you in doubt if the target resource group remains still empty (after a few seconds/minutes)? Be patient it will take a while. Sometimes even hours. So do not get nervous.

This hint was not by my own. I found an article from Damir Dobric (MVP). So credits go to him (including the great graphics above!): Azure: How to move resources between subscriptions under different tenants? - TechNet Articles - United States (English) - TechNet Wiki (microsoft.com)



Wednesday, December 11, 2019

Advanced Azure Cloud Computing Series - Part 1

When it comes to advanced workloads in Azure like CAx engineering or cloud based gaming some people are not really convinced about the overal performance. Some meaning that the performance cant reach the end user. To show you that this is not the case here a few examples in my series about advanced Azure cloud computing performance.




This is a high performance benchmark example. You will see at the end a demo video based on a rendering benchmark and also a demo video with a CAD program (Demo version of Autodesk Inventor 2019).

For a better understanding you see below also an illustration about the setup used for this demonstration. When you do your own tests and you are working with "RDP only" then the the performance on the WAN will be very poor for this benchmark.

Therefore I added a Citrix Netscaler and optimized the HDX protocol (Kudos to my colleague Dennis Reimer!)


To get a great performance for heavy graphic workloads (e.g. CAD) or cloud gaming you would need the Microsoft Azure NV-series machines. The server running the VMs have additional NVIDIA graphic cards and their performance is directly accessible from the VM itself. More information you will find here.

To get a real world impression from what you get - see here a locally captured video from the benchmark which was running in the cloud!

(These videos are uploaded to Youtube. So please ensure you have setup in Youtube fix resolution with HD1080p60 and a good internet connection!)


And to see the real world behaviour of a CAD program running in the cloud - see here also a locally captured video from Autodesk Inventor 2019. To ensure you that this is no fake have a clooser look to the RDP Connection Analyzer in the beginning which reveals that this is truely a remote computer using HDX (Thinwire) protocol for optimal graphics. In the middle of the video you can see also shiny metal surfaces with realtime reflection rendering.

(These videos are uploaded to Youtube. So please ensure you have setup in Youtube fix resolution with HD1080p60 and a good internet connection!)


As you can see you can benefit from high performance machines without having huge investments under your own desk. (And no I get not paid by MS for this. I am an Azure fan and want to make you frenetic as I am :-) )

PS: In one of my next blogs I plan to write about cloud gaming with Azure. There are different free tools available to make this working with DirectX as well.

Tuesday, December 3, 2019

Bad web server performance in Azure - dont forget the right view for diagnostics

Recently I had an issue with bad performance of a webservice in Azure.

First look you typically take is to check the main metrics for the webservice like CPU and RAM. But CPU was always around 10%-15% and RAM was around 60% - 70%. So no big deal.

But the time to answer requests was very bad. (Left side without modification. Right side with modification).





Then I checked the database and it looked fine on the first place. When you see this you would never mind that this is still an issue! But when you look deeper into it you will find that Azure starts to show this with aggregation type  "AVG"= Average values. (You will find the dropdown in the right upper corner of the graph) So what you see is just a cummulation over time. And this can be really misleading!  (Left side still the bad situation before modification, right side with modification)




After changing the aggregation to the aggregation type "MAX" you will see this. And this makes really much more sense now. So in my situation very often the SQL server in Azure hits its limits. But not for a long time as my requests are from hundreds of users but not permanently.  (Left side still the bad situation before modification, right side with modification)




But now the big question - how to fix this?

The cool thing in Azure is that nearly all ressources scale in the one or other way. And the scaling is incredible. In my situation I have initially operated the website with the smallest production SQL server instance in Azure with 10 DTUs. (For DTU definition checkout here).

To finally improve my website application performance the simple change was from 10 DTU to 20 DTU (in S1 Standard Tier). This cost me actually 12€ more per month and all users are happy again.

To do this simply move the slider in the "Configure" section of your Azure DB.


And thats it!

Thursday, July 14, 2016

Azure AD Connect: Synced attributes

I am asked from time to time what attributes are synced with Azure AD through Azure Active Directory Connect tool:


In general its just selected user, group and contact information.

Here you find a list of synced attributes:
https://azure.microsoft.com/en-us/documentation/articles/active-directory-aadconnectsync-attributes-synchronized


Depending on Windows 10 features there are also a few machine attributes synced to Azure AD as well. This is necessary for specific scenarios like Passport for work and requires actual versions of Windows 10 build (build 10551 or newer) for devices:
https://azure.microsoft.com/en-us/documentation/articles/active-directory-azureadjoin-devices-group-policy/


Be carefull and dont think you know what you are doing by partially not syncing them. Depending on the services they are really necessary. E.g. Exchange onpremise stores a lot of informations in AD. So Exchange online do as well. Therefore these attributes are necessary for proper function.


Unless you are the developer of the cloud application like Exchange Online you are not the one to judge if an attribute is necessary for correct function or not.


So either you feel comfortable with the attributes or just dont use Azure AD at all. Everything else will just mess up the AAD information and the cloud applications will not work properly.


Azure AD also stores Bitlocker keys but only for Azure AD joined machines.
https://blogs.technet.microsoft.com/home_is_where_i_lay_my_head/2016/03/14/automatic-bitlocker-on-windows-10-during-azure-ad-join/



Saturday, March 19, 2016

Azure transfer of administrative user to organizational user (Azure AD synced account)

I had recently a customer they played around with Azure RMS. We used a trial account with a Microsoft Account (azureadminCUSTOMER@live.com) this becomes in the Azure Default Directory the user azureadminCUSTOMERlivecom.onmicrosoft.com.






















No wonder that the customer was not happy with this username to administer his Azure tenant. It created some headache and many discussions with many People at MS.

And dont believe that everybody there is on the same level and topic a subject matter expert. It may take some time to reach out to the right people.

Finally I found the right distribution list to ask excactly for this topic. And guess what. It is definitelly possible to move your Azure admin account to an organizational Account (local AD synced with Azure Active Directory).




Azure payment via Invoicing

I had recently a customer they dont want to pay Azure with credit Card. Instead they want to use invoicing. To make this happen, simply follow the steps described here: