_template XML Editing Template Powershell
Log In or Register to download the BES file, and more.

0 Votes

Versioning - This is the latest version.

1_template XML Editing Template Powershell11/12/2018 10:45:08 AM
2_template XML Editing Template Powershell11/12/2018 10:49:01 AM

Description

Sample template fixlet to modify an XML file. 

This Fixlet demonstrates opening an XML file with PowerShell, and replacing a value in the XML content.

Originally written to allow removing an NVidia driver without rebooting.  NVidia, in their admin-unfriendliness, decided that uninstalling a driver should immediately trigger a reboot without providing any options to delay the reboot or to warn the user that a reboot is going to occur.  There are no command-line parameters available to modify this behavior, but we can skip the reboot by modifying a value in their nv.xml configuration file. 

This fixlet, being an example of the PowerShell usage, generates on-the-fly an XML file similar to NVidia's format, and then uses PowerShell to replace one of the attribute values in that file.


Property Details

ID25582
StatusBeta - Preliminary testing ready for more
Title_template XML Editing Template Powershell
DomainBESC
SourceInternal
Source Release Date8/23/2018 12:00:00 AM
KeywordsXML, Powershell, Replace
Added by on 11/12/2018 10:49:01 AM
Last Modified by on 11/12/2018 10:49:01 AM
Counters 482 Views / 0 Downloads
User Rating 1 star 2 star 3 star 4 star 5 star * Average over 0 ratings. ** Log In or Register to add your rating.

Relevance

Used in 3 fixlets   * Results in a true/false
Show indented relevance
windows of operating system AND (if exists property "in proxy agent context" then not in proxy agent context else true)

Actions

Action 1 (default)

Action Link Click here to deploy this action.
Script Type BigFix Action Script
action uses wow64 redirection false

// build the powershell script that we will use to modify an XML file
createfile until EOF_EOF_EOF_EOF
<#

.Synopsis
This is a Powershell script to search an XML file for a given node, and replace an attribute value on any matching node(s)

.DESCRIPTION
$MyInvocation.ScriptName -file -xpath -name -value [-out ] [-showdetail]

.EXAMPLE
powershell -ExecutionPolicy Bypass -File $MyInvocation.ScriptName -file "C:\temp\nv.xml" -xpath "//*[@uninstallReboot='true']" -name uninstallReboot -value false -out "c:\temp\nv-output.xml" -showdetail

.NOTES
Originally written to change brain-dead NVidia driver uninstallers to not force a reboot without warning on uninstall. The NVidia XML config files needed to be updated to remove the forced reboot.



#>



param (
    [Parameter(Mandatory=$true)][string]$file,
    [string]$out=$file,
    [Parameter(Mandatory=$true)][string]$xpath,
    [Parameter(Mandatory=$true)][string]$name,
    [Parameter(Mandatory=$true)][string]$value,
    [switch]$showdetail=$false
    
)
if ($showdetail) {{
    write-host "Parameters:"
    write-host "file: $file"
    write-host "Selection Search: $select"
    write-host "Node to change: $name"
    write-host "New value to set: $value"
    write-host "Output File: $out"
}
#$path=$file
[xml]$xml=(Get-Content $file)
[System.Xml.XmlElement] $root = $xml.get_DocumentElement()
$nodes=$root.SelectNodes($xpath)
foreach ($node in $nodes) {{
if ($showdetail) {{ write-host "Found node " $node.OuterXml }
    $node.$name=$value
    }
if ($showdetail) {{ write-host "Saving output to " $out }
$xml.save($out)
EOF_EOF_EOF_EOF

delete updatexml.ps1
move __createfile updatexml.ps1


// Create a sample XML file to update, will change "uninstallReboot" to "false"
delete __createfile
createfile until EOF_EOF_EOF_EOF
="1.0" encoding="UTF-8"?>
="Graphics Driver" disposition="critical" versionText="391.74" timestamp="2018-06-05T02:19:48.000" name="Display.Driver" version="391.74">

    ="0" fixedProgress="0" phase="installAllDrivers" action="uninstall" when="uninstall" uninstallReboot="true">
="C:\Program Files\NVIDIA Corporation\Installer2\Display.Driver.{{2F1420AC-B99E-4088-ACE3-ACD203A2B05E}\*.inf" />
="PCI\VEN_10DE" />



EOF_EOF_EOF_EOF
delete sample.xml
move __createfile sample.xml

// Now, use powershell to modify an attribute within the sample file and output it to a new XML
waithidden powershell.exe -ExecutionPolicy Bypass -File updatexml.ps1 -file sample.xml -xpath "//*[@uninstallReboot='true']" -name uninstallReboot -value false -out sample-output.xml
Success Criteria

This action will be considered successful when the applicability relevance evaluates to false.


Sharing

Social Media:
Share this page on Yammer

Comments

Log In or Register to leave comments!
JasonWalker -
BigFix.me does not display the embedded XML correctly, but downloading the .BES file does give the correct content; don't copy/paste the action script from the website display.