0

This is the very basic picture I am working with. It doesnt show the device as 'compliant' or 'non-compliant', but rather with 'error' or 'pending'. I noticed backslashes used as escape character in the logs. Could the path be a problem? Please advise.

$filePath = "C:\ProgramData\Autodesk\_PennCompliance"
$currentFileName = Get-ChildItem -Path $filePath -Name companyCompliance*.txt
$hash = @{
    FileName = $currentFileName
}
Write-Output $hash
return $hash | ConvertTo-Json -Compress

My JSON file has the matching key as in the hashTable 'FileName'

{
    "Rules":[
        {
           "SettingName":"FileName",
           "Operator":"IsEquals",
           "DataType":"String",
           "Operand":"PennCompliance_2021-0921.txt",
           "MoreInfoUrl":"https://call4cloud.nl/2021/11/the-last-days-of-custom-compliance/#part1",
           "RemediationStrings":[
              {
                 "Language":"en_US",
                 "Title":"Must update text file suffix.",
                 "Description": "Must update the suffix containing the date (PennCompliance_yyyy-mmdd.txt) of the PennCompliance text file."
              }
           ]
        }
    ]
}

Now I am looking for the status of 'Compliant' or 'Not Compliant' but instead see 'error' or 'pending'. I have deployed a Custom compliance policy from a sample and it worked fine so its something with this code. I also looked at the intuneManagementExtension logs at the time it failed.

{"PolicyId":"0cb83122-b322-45f5-9ab1-8e75c28ce7f5","UserId":"3dc325b8-6b7b-4b95-9e56-df64471366e0","PolicyHash":null,"Result":3,"ResultDetails":null,"InternalVersion":2,"ErrorCode":0,"ResultType":3,"PreRemediationDetectScriptOutput":"{\"FileName\":{\"value\":\"PennoniCompliance_2021-0921.txt\",\"PSPath\":\"Microsoft.PowerShell.Core\\\\FileSystem::C:\\\\ProgramData\\\\Autodesk\\\\_PennCompliance\\\\PennoniCompliance_2021-0921.txt\",\"PSParentPath\":\"Microsoft.PowerShell.Core\\\\FileSystem::C:\\\\ProgramData\\\\Autodesk\\\\_PennCompliance\",\"PSChildName\":\"PennoniCompliance_2021-0921.txt\",\"PSDrive\":{\"CurrentLocation\":\"WINDOWS\\\\system32\",\"Name\":\"C\",\"Provider\":\"Microsoft.PowerShell.Core\\\\FileSystem\",\"Root\":\"C:\\\\\",\"Description\":\"Windows\",\"MaximumSize\":null,\"Credential\":\"System.Management.Automation.PSCredential\",\"DisplayRoot\":null},\"PSProvider\":{\"ImplementingType\":\"Microsoft.PowerShell.Commands.FileSystemProvider\",\"HelpFile\":\"System.Management.Automation.dll-Help.xml\",\"Name\":\"FileSystem\",\"PSSnapIn\":\"Microsoft.PowerShell.Core\",\"ModuleName\":\"Microsoft.PowerShell.Core\",\"Module\":null,\"Description\":\"\",\"Capabilities\":52,\"Home\":\"C:\\\\WINDOWS\\\\system32\\\\config\\\\systemprofile\",\"Drives\":\"C\"},\"PSIsContainer\":false}}","PreRemediationDetectScriptError":null,"RemediationScriptErrorDetails":null,"PostRemediationDetectScriptOutput":null,"PostRemediationDetectScriptError":null,"RemediationStatus":4,"Info":{"RemediationExitCode":null,"FirstDetectExitCode":0,"LastDetectExitCode":null,"ErrorDetails":null},"TargetType":1,"RunAsAccount":1,"AssignmentFilterIds":null,"BiosMetadata":null} 

I just don't know what to make of it. It looks like the backslash is escaping the absolute path, Im not sure. What I do know is any help would be much appreciated.

1 Answer 1

0

You're getting an object back and passing the whole thing to the output. $currentFileName could possibly return multiple files, and each of those files has multiple properties.

You can inspect this on your box by passing $currentFileName to get member to see what what properties are available:

$currentFileName | Get-Member

To get back just the file name (of the first result [0]) you can do:

$currentFileName = (Get-ChildItem -Path $filePath -Name companyCompliance*.txt)[0].Name

Just be warned, if Get-ChildItem doesn't return anything, the [0] will throw an error. You can use an if block to check the count.

I also removed the Write-Output because that could cause issues.

Here's the full solution:

$filePath = "C:\ProgramData\Autodesk\_PennoniCompliance"
$currentFile = Get-ChildItem -Path $filePath -Name companyCompliance*.txt
if($currentFile.Count -ge 1){
  $currentFileName = $currentFile[0].Name
}
else {
  $currentFileName = 'None'
}
$hash = @{
    FileName = $currentFileName
}
return $hash | ConvertTo-Json -Compress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.