【问题标题】:Powershell: Save file to standing directoryPowershell:将文件保存到常设目录
【发布时间】:2012-04-11 10:03:22
【问题描述】:

我正在尝试将 XML 文件从 powershell 脚本保存到常设目录,但经过一些在线阅读后,似乎可能性有限。任何人都知道一些保存到“常设目录”的代码,而不是保存到“完整路径”的代码:

-此代码在生成 XML 并将一些变量保存到 XML 后运行。

$template | out-File C:\Scripts\XML\info.xml
$systemroot = [System.Environment]::SystemDirectory
$xml = New-Object xml
$xml.Load("C:\Scripts\XML\info.xml")

-此代码在脚本的最后运行,在 XML 之后以及向 XML 添加信息的一些命令之后。

$xml.Save("/info.xml")
Get-Content /info.xml

感谢所有帮助!

【问题讨论】:

  • 只有我不知道“常设目录”是什么意思吗?你能发布一些例子吗?谢谢
  • 我的意思是我希望将 XML 保存到与脚本相同的位置,相同的文件。

标签: xml powershell directory


【解决方案1】:

您可以通过这种方式找到脚本的路径:

$ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition

然后您可以在脚本中使用$ScriptPath 作为路径来加载或保存 xml 文件:

$template | out-File $ScriptPath\info.xml  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2022-01-16
    • 2011-07-19
    • 2021-04-10
    相关资源
    最近更新 更多