A PowerScript (as a function) for generating the IoT Hub Connection Strings as environment variables.

function set-env{
    param (
    [string]$Subscription = '' ,
    [string]$GroupName = '' ,
    [string]$HubName = '' ,
    [string]$DeviceName = ''

    write-host '  S E T   E N V I R O N M E N T  V A R S   '  -BackGroundColor Yellow -ForeGroundColor Red

    write-Host ''
    write-Host 'Note: Environment Variables only exist for the life of the current Shell' -BackGroundColor DarkRed -ForeGroundColor White
    write-Host ''

    $SharedAccesKeyName = 'iothubowner'
    # $SharedAccesKeyName = 'service'
    write-Host "SharedAccesKeyName is:  $SharedAccesKeyName"
    $env:SHARED_ACCESS_KEY_NAME = $SharedAccesKeyName

    If ([string]::IsNullOrEmpty($HubName ))
      write-host 'HubName required`
        write-Host "DeviceName is: $DeviceName" 
        $env:DEVICE_NAME = $DeviceName
        $env:DEVICE_ID = $DeviceName

    If (-not ([string]::IsNullOrEmpty($DeviceName )))
      write-host 'Device name required`
        write-Host "DeviceName is: $DeviceName" 
        $env:DEVICE_NAME = $DeviceName
        $env:DEVICE_ID = $DeviceName

    # Device Connection String
    write-Host 'Getting IOTHUB_DEVICE_CONN_STRING'
    $cs = az iot hub device-identity connection-string show --hub-name $HubName --device-id $DeviceName  --output json  | out-string
    $IOTHUB_DEVICE_CONN_STRING = ($cs   | ConvertFrom-Json).connectionString

    # Hub Coonection String
    write-host 'Getting IOTHUB_CONN_STRING_CSHARP'
    $cs = az iot hub connection-string show --hub-name $HubName --policy-name iothubowner --key primary  --resource-group $GroupName --output json  
    $IOTHUB_CONN_STRING_CSHARP = ($cs   | ConvertFrom-Json).connectionString

    # Service Connection string
     write-host 'Getting Service Connection string'
      $cs = az iot hub connection-string show --policy-name service --hub-name $HubName --output json | out-string
      $SERVICE_CONNECTION_STRING = ($cs   | ConvertFrom-Json).connectionString

    # EventHubsCompatibleEndpoint
    write-host 'Getting EventHubsCompatibleEndpoint'
    $cs =  az iot hub show --query properties.eventHubEndpoints.events.endpoint --name $HubName --output json |out-string
    $cs = trimm($cs)
    $EventHubsCompatibleEndpoint = $cs.Replace('"','')
    write-host $EventHubsCompatibleEndpoint
    $env:EVENT_HUBS_COMPATIBILITY_ENDPOINT = $EventHubsCompatibleEndpoint
    # EventHubsCompatiblePath
    write-host 'Getting EventHubsCompatiblePath'
    $cs = az iot hub show --query properties.eventHubEndpoints.events.path --name $HubName --output json  |out-string
    $cs = trimm $cs
    $EventHubsCompatiblePath = $cs
    write-host $EventHubsCompatiblePath 
    $env:EVENT_HUBS_COMPATIBILITY_PATH =$EventHubsCompatiblePath

    # EventHubsSasKey
    write-host 'Getting EventHubsSasKey'
    $cs = az iot hub policy show --name iothubowner --query primaryKey --hub-name $HubName   |out-string
    $cs = trimm $cs
    $EventHubsSasKey = $cs
    write-host  $EventHubsSasKey

    # EventHubsConnectionString
    write-host 'Calculating the Builtin Event Hub-Compatible Endpoint Connection String' 
    write-host $EventHubsConnectionString
    $env:EVENT_HUBS_CONNECTION_STRING = $EventHubsConnectionString

    write-Host 'Done'
    read-Host 'Press any key to finish' >$null

 This Category Links 
Category:Pico W AzSDK Index:Pico W AzSDK
  Next: > RPI-Pico-Arduino-AzSDK
<  Prev:   RPI-Pico-Arduino-AzSDK