I have a following script in PS:
[System.Xml.XmlDocument] $Config;
function Get-ScriptDirectory
{
Split-Path $script:MyInvocation.MyCommand.Path
}
function LoadConfig
{
$configPath = Join-Path (Get-ScriptDirectory) Config.xml
$Config = [xml](gc $configPath)
}
function WriteData
{
$sourceFolderPath = $Config.Deploy.SourceFolder
Write-Host $sourceFolderPath
}
LoadConfig
WriteData
My basic xml file looks like:
<Deploy>
<SourceFolder>C:\FolderPath</SourceFolder>
<Deploy>
When I debuging it in PowerGUI tool it works fine and it writes the correct output. But when I run the same script in powershell console in Windows 7, the result is empty line. I have no idea why.
Your script has a trouble, as you declare
[System.Xml.XmlDocument] $Configat the begining of your script, you have to use$global:ConfiginLoadConfigfunction. For more explanations have look toGet-Help about_Scopes.Why does it work in PowerGui ? Because
$configexist in your session, you’d better configure PowerGui like I show in the following picture.