[英]Powershell script working with xml produces different output in PowerGUI and Powershell console
我在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控制台中运行相同的脚本时,结果是空行。 我不知道为什么。
您的脚本遇到了麻烦,因为在脚本开始时声明[System.Xml.XmlDocument] $Config
,您必须在LoadConfig
函数中使用$global:Config
。 有关更多说明, Get-Help about_Scopes
。
function LoadConfig
{
$configPath = Join-Path (Get-ScriptDirectory) Config.xml
$global:Config = [xml](gc $configPath)
}
为什么在PowerGui中可以使用? 因为$config
存在于您的会话中,所以最好配置PowerGui,如下图所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.