如何将变量存储到磁盘以便下次运行 PowerShell 脚本时可以使用它们? [英] How to store variables to disk so you can use them next time the PowerShell script runs?
问题描述
我想将变量存储到磁盘或注册表,以便下次运行预定脚本时可以使用它?最好是一个或两个...
I would like to store a variable to disk or registry so I can use it the next time I run the scheduled script? Preferably an one-liner or two...
干杯,罗伊
推荐答案
$foo | Export-CliXml foo.xml
然后
$foo = Import-CliXml foo.xml
请注意,如果 $foo 代表一个活动对象,那么当您恢复它时,您只会获得它的属性.然而,类型信息或多或少地保留了下来.例如,如果您保存了一个 System.Diagnostics.Process 对象,那么当您对其重新水合时,您将拥有一个 Deserialzed.System.Diagnostics.Process 对象.
Note that if $foo represents a live object, when you restore it, you are only going to get its properties. However the type information is more-or-less preserved. For example if you save out a System.Diagnostics.Process object, when you rehydrate it you will have a Deserialzed.System.Diagnostics.Process object.
顺便说一句,如果您需要存储/检索多个变量,您可以这样做:
BTW if you need to store/retrieve multiple variables, you can do that like so:
Get-Variable bla* | Export-Clixml vars.xml
Import-Clixml .\vars.xml | %{ Set-Variable $_.Name $_.Value }
这篇关于如何将变量存储到磁盘以便下次运行 PowerShell 脚本时可以使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!