Running Powershell script with xml answer file

cwhitmore
cwhitmore Member Posts: 9
edited December 2023 in Scripts

I have a few Powershell scripts I use to setup Always on VPN, but two of them require an xml answer file. I've tried using a UNC path to the xml file when running the script, but no luck. Has anyone been successful in using an answer file with script in Atera? If so, what syntax did you use?

Comments

  • tanderson
    tanderson Member Posts: 271 ✭✭✭✭

    @cwhitmore

    Using an XML answer file in a PowerShell script typically involves parsing the XML file to extract data, which you can then use as part of your script's operations.

    Based on the information I found, here are some strategies for using XML files with PowerShell:

    Using Select-Xml Cmdlet:

    • The Select-Xml cmdlet can parse an XML file and select nodes using an XPath expression. Here's a basic example of how you might use Select-Xml to parse a file and get the InnerXML of specified nodes:

    Select-Xml -Path C:\Path\To\Your\file.xml -XPath '/XPath/To/Element' | ForEach-Object { $_.Node.InnerXML }

    • This command selects nodes from the XML file located at the given path, which match the XPath query, and then iterates over these nodes to extract the InnerXML

    Converting XML to Objects:

    • Another approach is to convert the XML file into a PowerShell object using the [xml] type accelerator, which allows you to work with the XML data as if it were a native PowerShell object. Here's how you might load an XML file and then access its data via properties:

    [xml]$xmlData = Get-Content -Path C:\Path\To\Your\file.xml
    $xmlData.YourRootElement.YourElement

    • This way, you can access the elements directly using the dot notation​.

    Looping Through XML Data:

    • If you need to iterate over a list of items within the XML, you can cast the XML to an object and then loop through the data. Here’s an example of looping through items and checking their status:

    [xml]$xmlData = Get-Content -Path C:\Path\To\Your\file.xml
    $xmlData.YourRootElement.YourChildElement | Where-Object Attribute -eq 'value' | ForEach-Object {

    # Your Logic Here

    }

    • This snippet shows how to loop through elements that have a specific attribute value and perform actions on each one​.

    When it comes to using these methods with, you should ensure that the PowerShell script can access the XML file at the specified path. If you're using a UNC path and encountering issues, it's possible there may be permission issues or the path may not be recognized within the script environment. Ensure the appropriate permissions to access the network location where the XML file is stored are being used.

    The syntax you use will largely depend on the structure of your XML file and what data you're trying to extract. For troubleshooting, you can test the script locally on your machine using an absolute path to the XML file to make sure the syntax and logic are correct before implementing.

  • cwhitmore
    cwhitmore Member Posts: 9

    @tanderson thanks for the detailed information. I was trying to access the xml files from a DFS share so as a workaround I copied the xml files down to the device's c:\tempPath folder which solved my problem.

  • lauri.kinnunen
    lauri.kinnunen Member Posts: 17 ✭✭✭

    We store required files to azure blob storage and clients can download from there.
    Or even from the publisher site. Using Blob you get around the VPN issue.

    Here is example:

    <# Give proper url to file you need
    Name of the file , this will be the name of file when saved on local machine.

    If file is downloaded do what you want, here we assume it is exe file so install it whit /S (silent for some)

    #>


    $FileURL = ""
    $FileName = ""
    $SoftwareName = ""

    if(!$(Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq $SoftwareName })){
    Invoke-WebRequest -Uri $FileURL -OutFile "$($env:TEMP)$FileName"

    If($?){
    msiexec /i "$($env:TEMP)\$FileName" /S
    }

    }

  • cwhitmore
    cwhitmore Member Posts: 9

    Thanks @lauri.kinnunen that's a great idea.

  • kim
    kim Member Posts: 113 ✭✭✭
    edited November 2023

    Thank you for sharing @lauri.kinnunen! Have you submitted it to the Shared Script Library? This is a great use case for it as you have variables that can be used to fit other Aterean Environments, Thank you for asking a question I didn't know I had the some one too @cwhitmore!