Intune does not deploy powershell script

302 views Asked by At

My company acquired a smaller company with about 70 laptops. We are trying to get the Hardware Hash of all those laptops in an automated way so we can reimage the laptops with your Intune using autopilot. For that, I build a PowerShell script that I am trying to deploy as a Win32 app on that company. This script will get the hardware hash and upload it to an Azure Blob.

The script works perfectly if I run it locally with admin rights on the machine. The detection method, I am using manual detection with a file that the scripts create called hashidconfirm.txt. The command that I am using is powershell.exe -ExecutionPolicy Bypass -File .\hashid.ps1 in the install field in the Win32 configuration.

However, the script does not work when deployed this way, and I can not figure out why. Am I doing something wrong?

#Define variables

Install-Module -Name Az.Storage -force

Install-Script -Name Get-WindowsAutoPilotInfo -force

$StorageAccountName = "XXXXX"

$StorageAccountKey = "XXXXXX"

$ContainerName = "XXXXX"

$Serial = (Get-CimInstance win32_bios).SerialNumber

$Context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

New-Item -ItemType Directory -Path C:\Intune\

#Create the local file

Get-WindowsAutoPilotInfo -Outputfile C:\Intune\$Serial.csv

#Upload the file to Azure blob storage

Set-AzStorageBlobContent -Context $Context -Container $ContainerName -File "C:\Intune\$Serial.csv"

#Delete the local file

Remove-Item -Path "C:\Intune\$Serial.csv"

New-Item -Path "C:\Intune\hashidconfirm.txt"

I am expeting to get the hardware hash ids using a powershell script deployed with intune

1

There are 1 answers

0
Sampath On

I tried this below to get the hardware hash and upload it to an Azure Blob.

  • To Display the collected hardware hash:
$hashFilePath = "C:\hashidconfirm.txt"
$hardwareHash | Set-Content -Path $hashFilePath
Write-Host "Hardware Hash: $hardwareHash"

enter image description here

  • Creates the logs data with time with hardware hash:
# Define the log file path
$logFilePath = "C:\hashid.log"

# Define a function to log messages
function Write-Log {
    param (
        [string]$message
    )
    $logMessage = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $message"
    Add-Content -Path $logFilePath -Value $logMessage
}

# Set up a trap to log errors
trap {
    Write-Log "Error: $_"
}

try {
    # Collect the hardware hash
    $hardwareHash = (Get-WmiObject -Class Win32_ComputerSystemProduct).UUID

    # Create a detection file
    $hashFilePath = "C:\hashidconfirm.csv"
    $hardwareHash | Set-Content -Path $hashFilePath

    # Log the collected hardware hash
    Write-Log "Hardware Hash: $hardwareHash"

} catch {
    # Handle any errors and log them
    Write-Log "Error: $_"
}

enter image description here

With powershell:

$Serial = (Get-CimInstance win32_bios).SerialNumber
 $LocalDirectory = "C:\"

$Context = New-AzStorageContext -StorageAccountName 
$StorageAccountName -StorageAccountKey $StorageAccountKey
Set-AzStorageBlobContent -Context $Context -Container 

$ContainerName -File "$LocalDirectory\$BlobName" -Blob $BlobName 

enter image description here

With ISE:

Install-Module -Name Az -Force -AllowClobber

# Connect to your Azure account
Connect-AzAccount

# Set your Azure subscription (replace with your subscription ID)
$subscriptionId = "your-subscription-id"
Select-AzSubscription -SubscriptionId $subscriptionId

# Azure Storage account and container information
$storageAccountName = "yourstorageaccountname"
$containerName = "yourcontainername"
$localFilePath = "  "
$blobName = "FJH7WZ2.csv"

# Check if the Azure Storage account exists and get the storage context
$storageAccount = Get-AzStorageAccount -ResourceGroupName "YourResourceGroup" -Name $storageAccountName

if ($storageAccount -eq $null) {
    Write-Host "Storage account not found. Please check the account name and resource group."
} else {
    $context = $storageAccount.Context

    # Upload the file to Azure Storage
    Set-AzStorageBlobContent -Container $containerName -File $localFilePath -Blob $blobName -Context $context

    Write-Host "File uploaded successfully to Azure Storage."
}

enter image description here