Powershell path to config file from script

Discussion in 'Scripting' started by sebus, Mar 6, 2019.

  1. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,356
    2,026
    210
    I have a PS scipt that does have internal reference to an xml config

    $xmlFilePath = "Config_Device.xml"

    I need to execute it from System, so using NsudoC in a batch

    Code:
    %~dp0NSudoC.exe -U:S -P:E C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file %~dp0\somefile.ps1
    But running it I get error from PS that the Config_Device.xml cannot be found

    Any ideas?

    sebus
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,226
    84,914
    340
  3. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    534
    926
    30
    $MyInvocation.MyCommand.Definition can still be used but generally it's used for PowerShell 2 and below. In PowerShell 3 the automatic variable $PSScriptRoot was incorporated and is always set to the current scipt/module directory.

    If using a true UTF-8 formatted XML config file, and not just adding .xml as the extension to a regular .txt file, you will want to use the xml parameter to define the variable:
    Code:
    [XML]$XMLFile =  Get-Content -Path -XMLFilePath
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...