Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

PowerShell Logging / Error Handling

Hi All!

So I am trying to create a PowerShell step in PDQ and I am having a hard time with the output log. Here is a sample of my code.

Write-Output "Starting Script" # I have tried Echo's instead as well
Try {
[XML]$Output = .\BiosConfigUtility64.exe /getvalue:"TPM Device"
$TMPDevice = Select-Xml -Xml $Output -XPath "//VALUE" | foreach {$_.node.InnerXML}
IF ($TMPDevice -eq "<![CDATA[*Hidden,Available]]>")
{
Write-Output "Enabling TPM Device"
.\BiosConfigUtility64.exe /cpwdfile:default_password.bin /setvalue:"TPM Device","Available"
}ELSE{
Write-Output "TPM Device is Available"
}
}Catch {
$_.Exception #I have also tried just $_
exit 6
}

Ultimately I understand the exe command on line 8 is causing the exception but I am trying to understand how to handle this properly.

I guess I am not asking how to fix the exe exception and trying to figure out how to get data into my output.log. No matter what output type or error type I use I do not get any output log. I would hope to at least see "Starting Script"

For this script I would like to see the following in the Output Log.

Starting Script
Enabling TPM Device
<BIOSCONFIG Version="" Computername="#######" Date="2020/05/14" Time="23:42:16" UTC="20">
<SUCCESS msg="Successfully read password from file" />
<SETTING changeStatus="fail" name="TPM State" returnCode="4">
<OLDVALUE><![CDATA[Disable]]></OLDVALUE>
<VALUE><![CDATA[]]></VALUE>
</SETTING>
<ERROR msg="An operation failed" />
<ERROR msg="BCU return value" real="4" translated="4" />
</BIOSCONFIG>
#and maybe some exception code down here for $_

Is what I am looking for even possible? I just find error handling hard in PDQ. Once I write my PowerShell our deployment users may not understand the errors and how to move forward. This way I can enter my own error handling and they can see in plain text what broke and what needs to be done manually.

Any help would be great.

Thanks!

1

Comments

2 comments
Date Votes
  • Breaking it down to its simplest form:

    $Output = .\BiosConfigUtility64.exe /getvalue:"TPM Device"

    <BIOSCONFIG Version="" Computername="MyComputer" Date="2020/05/18" Time="10:05:01" UTC="10">
        <ERROR msg="System not supported. Unable to connect to HP WMI namespace: 'root\HP'/>
        <ERROR msg="BCU return value" real="23" translated="23" />
    </BIOSCONFIG>


    [XML]$Output = .\BiosConfigUtility64.exe /getvalue:"TPM Device"

    Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'<', hexadecimal value 0x3C, is an invalid attribute
    character. Line 3, position 2."
    At line:1 char:1
    + [XML]$Output = .\BiosConfigUtility64.exe /getvalue:"TPM Device"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

    It appears that the tool doesn't return valid XML...
    0
  • Hmm, Thanks for the response Ben.

    I was working with support on this issue as well since I was not able to get output.log's. I narrowed this down to something similar.

    I didn't post my entire code but I am working on an error a bit higher up in sequence same error different switches for the exe. 

    [XML]$Output = .\BiosConfigUtility64.exe /npwdfile:default_password_1.bin

    The EXE would output

    <BIOSCONFIG Version="" Computername="#####" Date="2020/05/19" Time="12:19:30" UTC="-4">
      <ERROR msg="Unable to process command. Password is set, but no password file is provided" />
      <ERROR msg="BCU return value" real="10" translated="10" />
    </BIOSCONFIG>

     

    This is where I was receiving weird error results in PDQ.

    After dumping all properties of $_ I was able to locate this error which was being suppressed by both PS/ISE at least on my computer.

    The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.

    After Adjusting my code to

    [XML]$Output = .\BiosConfigUtility64.exe /npwdfile:default_password_1.bin | ConvertTo-Xml

     

    Everything is working as intended now.

    I hope the same can be said for

    $Output = .\BiosConfigUtility64.exe /getvalue:"TPM Device"

     

    I will post an update if this works for the TPM Device as well.

     

    0