我在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
我的基本xml文件如下所示:
<Deploy>
<SourceFolder>C:\FolderPath</SourceFolder>
<Deploy>
当我在PowerGUI工具中对其进行调试时,它工作正常,并且它会写出正确的输出。但是当我在Windows 7中的powershell控制台中运行相同的脚本时,结果是空行。我不明白为什么。
答案 0 :(得分:3)
您的脚本出现问题,在脚本开头声明[System.Xml.XmlDocument] $Config
时,您必须在$global:Config
函数中使用LoadConfig
。有关更多说明,请查看Get-Help about_Scopes
。
function LoadConfig
{
$configPath = Join-Path (Get-ScriptDirectory) Config.xml
$global:Config = [xml](gc $configPath)
}
为什么它在PowerGui中有效?由于您的会话中存在$config
,因此您最好配置PowerGui,如下图所示。