Intune vs Registry key and spaces in path

159 views Asked by At

I am trying to run script that would return me work or school account connected to the device. The script should be distributed over Intune. Everything elser works except this part. The code works in local powershell.

$path = 'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WorkplaceJoin\JoinInfo'
Get-ChildItem -Path $path | Get-ItemPropertyValue | Select-Object UserEMail | select UserEmail | ft -HideTableHeaders | out-string

But when it's published to intune then it ends with error, that in the path is space (between Windows and NT)

Cannot find path 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows \r\nNT\CurrentVersion\WorkplaceJoin\JoinInfo' because it does not exist.

I've also tried this code

$x=(dsregcmd /status | select-string -Pattern "WorkplaceThumbprint" | out-string).Trim()
$y=$x.LastIndexOf(":")
$AccountThumbPrint=$x.Substring($y+2)
$AccountMailAddress=(Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WorkplaceJoin\JoinInfo\$AccountThumbPrint" -Name UserEmail | select UserEmail | ft -HideTableHeaders | out-string).Trim()

Write-Host "$AccountMailAddress"

ending with the same error.

I think I've tried almost everything what could be found on google like Single quote marks, double quote marks

Thank you

edit: full code I am running to all Windows devices connected in itune:

## setting variables
$cpu = Get-WmiObject -Class Win32_Processor  |  select @{n="ComputerName";e={$_.systemname}},@{n="CPU";e={$_.name}}
$model = Get-WmiObject -Class Win32_ComputerSystem | Select-Object Model,Manufacturer
$serialNumber = Get-WmiObject win32_bios | Select-Object SerialNumber
$WinVer = Get-WmiObject Win32_OperatingSystem | Select-Object Version
$WinBuild = Get-WmiObject Win32_OperatingSystem | Select-Object BuildNumber
$memory = (Get-WmiObject Win32_PhysicalMemory  | Measure-Object -Sum -Property Capacity).Sum / (1024*1024*1024)
$owner = Get-WmiObject Win32_ComputerSystem | Select-Object PrimaryOwnerName
$totalStorage = (Get-WmiObject Win32_DiskDrive | Measure-Object -Sum -Property Size).Sum / (1000*1000*1000)   

## this is not used, but it was taken as an example
## variables to get work or school account connected to "Current user"
##  $x=(dsregcmd /status | select-string -Pattern "WorkplaceThumbprint" | out-string).Trim()
##  $y=$x.LastIndexOf(":")
##  $AccountThumbPrint=$x.Substring($y+2)
$path = 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\WorkplaceJoin\JoinInfo'
##Get-ChildItem -Path $path | Get-ItemPropertyValue | Select-Object UserEMail | select UserEmail | ft -HideTableHeaders | out-string

## getting the mail itself
$AccountMailAddress = (Get-ChildItem $path | Get-ItemProperty).UserEmail

                                                                                                                                          
[PSCUStOMOBJECT]@{
    "ComputerName"=$env:COMPUTERNAME;
    "Model"=$model.Model;
    "SerialNumber"=$serialNumber.SerialNumber;
    "Owner"=$owner.PrimaryOwnerName;
    "Work or School account" = $AccountMailAddress;
    "Windows Version" = $WinVer.Version;
    "Windows Build" = $WinBuild.BuildNumber;
    "RAM(GB)"=$memory;
    "CPU"=$cpu.CPU;
    "TotalCStorage"=$totalStorage
    } | ConvertTo-Json

I've obtained example from https://techcommunity.microsoft.com/t5/device-management-in-microsoft/how-to-collect-custom-inventory-from-azure-ad-joined-devices/ba-p/2280850

0

There are 0 answers