0

I have been having an issue with parsing an xml file

<DisplayName>Remote_Take_Over_S1_0002</DisplayName>

I am trying to get the above information from "C:\ProgramData\App-V\1BBEDDA5-595E-4CF7-834A-B282C4981469\0C71FE9F-F1C0-47F9-9518-E94898B6424F\AppxManifest.xml"

I have know idea how or where to start but i have got this far.

            $ComputerName = $txb_hostname.Text

        $RemoteParentPath = Join-Path -Path "\\$ComputerName" -ChildPath 'c$\ProgramData\App-V'

        $RemoteManifestPaths = Get-ChildItem -Path $RemoteParentPath -Recurse -Filter 'AppxManifest.xml' | Select-Object -ExpandProperty FullName

        foreach ($manifestFile in $RemoteManifestPaths)
        {
             $xml = [xml](Get-Content -Path $manifestFile -Raw)
             $xml.SelectSingleNode('//*[local-name()="DisplayName"]/text()').Value 
             LogWrite $xml
        } 

Any Ideas?

Errors now You cannot call a method on a null-valued expression. At line:6 char:5

Get-Content : Cannot find path 'H:\AppxManifest.xml' because it does not exist. At line:5 char:18

Managed to sort above errors

Adam
  • 19
  • 1
  • 8
  • 1
    Possible duplicate of [How to get Value from xml by PowerShell?](https://stackoverflow.com/questions/11344179/how-to-get-value-from-xml-by-powershell) – Paxz Dec 11 '18 at 14:42

1 Answers1

1

I assume you're looking to find all AppxManifest.xml files under the c:\ProgramData\App-V path on the remote computer (or in this case, the current computer using a UNC path to the file), then manipulate that XML file in some way; maybe to fetch the DisplayName value?

If so, try something like this:

[string]$ComputerName = $env:COMPUTERNAME
[string]$RemoteParentPath = Join-Path -Path "\\$ComputerName" -ChildPath 'c$\ProgramData\App-V'
[string[]]$RemoteManifestPaths = Get-ChildItem -Path $RemoteParentPath -Recurse -Filter 'AppxManifest.xml' | Select-Object -ExpandProperty FullName
foreach ($manifestFile in $RemoteManifestPaths) {
    $xml = [xml](Get-Content -Path $manifestFile -Raw)
    $xml.SelectSingleNode('//DisplayName/text()').Value
}  

I've broken it down into multiple lines to make it easier to understand what's going on / try out each piece. NB: I've not tested the code itself.

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • Keeps stating You cannot call a method on a null-valued expression. At line:6 char:5 Get-Content : Cannot find path 'H:\AppxManifest.xml' because it does not exist. At line:5 char:18 – Adam Dec 11 '18 at 14:55
  • Hmm, looks like I'd missed the opening brace for the for loop (now fixed), but that doesn't explain why it would be using your H drive... Are you amending this code before running it? – JohnLBevan Dec 11 '18 at 16:23
  • Ah sorry - just realised I was only returning file names, not paths (as converting to strings... amended) – JohnLBevan Dec 11 '18 at 16:26
  • I'm not sure either why it is pointing to my h drive the script isn't located their might be because I need to change directory from my h when I was working on something their earlier I'll check tomorrow, I notic d the brace was missing I'll check the code tomorrow to see if it runs – Adam Dec 11 '18 at 19:00
  • it brings back "version="1.0" encoding="utf-8" Package" which is the top line of the XML file do you know of a way to get DisplayName which would be line 10 in the XML file – Adam Dec 12 '18 at 07:08
  • Are you running exactly the code given above? `$xml.SelectSingleNode('//DisplayName/text()').Value` says to find the text value of the first DisplayName element found in the XML. – JohnLBevan Dec 12 '18 at 07:59
  • ps. If there's a schema involved, use `$xml.SelectSingleNode('//*[local-name()="DisplayName"]/text()').Value` (there are ways to load the name table with the appropriate namespaces and query with a prefixed name, but for now the above is enough). My guess is that you're just looking at the value of `$xml` itself, and you're seeing it formatted as a table, rather than the XML (i..e `$xml.OuterXml`. – JohnLBevan Dec 12 '18 at 08:02
  • I have put the newer code up it just keeps bringing back xml Package --- ------- version="1.0" encoding="utf-8" Package Remote_Take_Over_S1_0004 version="1.0" encoding="utf-8" Package Directories_S1_0004 version="1.0" encoding="utf-8" Package EARS_S1_0001 version="1.0" encoding="utf-8" Package NotepadPlusPlus_S1_0001 version="1.0" encoding="utf-8" Package – Adam Dec 12 '18 at 13:30
  • I just saw your edit `LogWrite $xml`. I don't know what `LogWrite` does, but I think you probably wanted to do `LogWrite $xml.SelectSingleNode('//DisplayName/text()').Value`; i.e. as that's the piece that pulls the display name value from the XML. – JohnLBevan Dec 12 '18 at 13:58
  • So I have a text box I write my outputs to so for example logwrite $CPU would write whatever $CPU has stored in the variable hopefully this makes sense – Adam Dec 12 '18 at 18:06
  • 1
    That brings back the text on its own, which is what i was looking for. once it tries to write it to the textbox using logwrite it shows blank. i guess ill investigate that now thank you – Adam Dec 13 '18 at 06:50