Azure Storage Account Deployment

The following Method can be run in by creating an Azure blob storage container. a Temporary folder for Autodesk Inventory Tool will be created on the local machine. The XML generated by AIT will be written back the aitdata container and the temporary ait folder removed, negating the need to install AIT. This method will require Remote management software executing the script to the Endpoints.

The script works as following 

  • Ensure Administrative Privileges:

  • Checks if the script is running with administrative privileges and relaunches with elevated permissions if necessary.

  • Create Temporary Folder and Copy AIT to Local Machine:

  • Run AIT to Perform Inventory Scan:

  • Executes AIT to perform an inventory scan on the local machine.

  • Waits for the scan to complete and generates data files.

  • Rename and Handle Generated XML File:

  • Renames the generated XML file to include the computer name.

  • Install and Import Azure Storage Blob Module:

    • Imports the Azure Storage Blob module for use in the script.

  • Connect to Azure Blob Storage and Upload XML File:

    • Connects to Azure Blob Storage using specified credentials.

    • Checks if the specified storage container exists and creates it if necessary.

    • Uploads the renamed XML file to the specified Azure Blob Storage container.

  • Remove Temporary AIT Folder:

    • Removes the temporary AIT folder from the local machine.

Step One if you have Azure Resource group, Create a storage account

 

 Choose a unique name for your storage account. Storage account names must be between 3 and 24 characters in length and might contain numbers and lowercase letters only in the example our storage account name is called aitdata but this will need to be unique. Select Primary service as Azure Blob Storage or Azure Data Lake Storage Gen 2

And review and Create

Once storage account is opened go to the resource and search for Containers

Go to Add container. call the container datastore and create

Create another container called ait.Install Autodesk Inventory Tool on a device and copy the files from C:\Program Files (x86)\Autodesk\Autodesk Inventory Tool and paste into the ait container

go back to the storage Account go to Security and Networking > Access Keys

Show the Key and Copy

Copy the Below Script and Add the Access Key to line 3 in between the brackets.

Save Script as AutodeskInventoryTool.PS1

param (
    [string]$accountName = "ADD STORAGE ACCOUNT NAME HERE", # Make sure to provide a unique name for the storage account
    [string]$accountKey = "ADD ACCOUNT KEY HERE",
    [string]$containerName = "datastore",
    [string]$containerNameAIT = "ait"
)

$localFolder = "C:\Temp\AIT" #This path can be adjusted based on your preference or specific requirements. WARNING: This folder will be permanently deleted once the script is executed. Please ensure that you do not set this to a common folder and verify that the folder is unique to avoid unintended data loss.
$PCN = "$env:COMPUTERNAME"
$data1 = "$localFolder\datastore.xml"
$data2 = "$localFolder\datastore.xml.md5"
$ADScan = "$localFolder\AIT.exe"
$localFilePath = "$localFolder\$PCN.xml"
$blobName = "$PCN.xml"
$logFile = "$env:TEMP\AIT_Script.log"
$errorLogFile = "$localFolder\AIT_Script_Error.log"

function Log-Message {
    param (
        [string]$message,
        [switch]$isError
    )
    $timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
    $logMessage = "$timestamp - $message"
    Write-Host $logMessage
    Add-Content -Path $logFile -Value $logMessage
    
    if ($isError) {
        Add-Content -Path $errorLogFile -Value $logMessage
    }
}

function Ensure-Admin {
    if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
        Log-Message "Script is not running as Administrator. Restarting with elevated permissions..."
        Start-Process -FilePath "powershell" -ArgumentList "-File `"$PSCommandPath`"" -Verb RunAs
        exit
    }
}

Ensure-Admin

try {
	
	Log-Message "Checking Azure Storage Blob module..."
    if (-not (Get-Module -ListAvailable -Name Az.Storage)) {
        Log-Message "Installing Azure Storage Blob module..."
        Uninstall-Module -Name Az -AllVersions -Force
        Install-Module -Name Az -AllowClobber -Force
    }

    Log-Message "Importing Azure Storage Blob module..."
    Import-Module Az.Storage

    Log-Message "Connecting to Azure Blob Storage..."
    $context = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey

    Log-Message "Checking if container '$containerName' exists..."
    $container = Get-AzStorageContainer -Context $context -Name $containerName -ErrorAction SilentlyContinue
    if (-not $container) {
        Log-Message "Creating container '$containerName'..."
        New-AzStorageContainer -Context $context -Name $containerName
    } else {
        Log-Message "Container '$containerName' already exists."
    }
	
    Log-Message "Downloading AIT folder..."
	$blobs = Get-AzStorageBlob -Container $containerNameAIT -Context $context
	$localFile = Join-Path -Path $localFolder -ChildPath $blob.Name
    $localDir = [System.IO.Path]::GetDirectoryName($localFile)

	Log-Message "Checking if Temp AIT folder exists..."
    if (-not (Test-Path -Path $localDir)) {
		Log-Message "Creating Temp AIT folder..."
        New-Item -ItemType Directory -Path $localDir -Force | Out-Null
    } else {
        Log-Message "Temp AIT folder already exists."
    }

	foreach ($blob in $blobs) {
    Get-AzStorageBlobContent -Blob $blob.Name -Container $containerNameAIT -Destination $localFile -Context $context | Out-Null
	}
	Log-Message "Download of folder AIT complete..."

    Log-Message "Running AIT.exe..."
    Start-Process -FilePath $ADScan -ArgumentList "/c $PCN /fp /lu /rp /sl" -WindowStyle Hidden -Wait

    if (-Not (Test-Path $data1)) {
        throw "Datastore.xml file does not exist."
    }

    Log-Message "Renaming XML file..."
    rename-item -Path $data1 -NewName $localFilePath

    Log-Message "Uploading XML file to Azure Blob Storage..."
    Set-AzStorageBlobContent -File $localFilePath -Container $containerName -Blob $blobName -Context $context | Out-Null
    Log-Message "XML file uploaded successfully."

    Log-Message "Removing Temp AIT folder..."
    Remove-Item -Path $localFolder -Recurse -Force
    Log-Message "Temp AIT folder removed successfully."
	Log-Message "Complete."
} catch {
    Log-Message "Error: $_" -isError
    exit 1
}
Click to copy

The Script can then be deployed to Endpoints using your Remote Management Software