从文件加载 PowerShell 哈希表? [英] Loading a PowerShell hashtable from a file?

查看:58
本文介绍了从文件加载 PowerShell 哈希表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 PowerShell 对象表示法中的一些数据的文件:

I've got a file containing some data in PowerShell Object Notation:

@{ X = 'x'; Y = 'y' }

我想将其加载到文件中的变量中.

I'd like to load this into a variable from the file.

推荐答案

(我在整理复制品时想出来的)

PS> $content = ( Get-Content .\foo.pson | Out-String )
PS> $data = ( Invoke-Expression $content )

Get-Content 返回一个包含文件中行的数组;Out-String 用于将它们连接在一起.

Get-Content returns an array with the lines in the file; the Out-String is used to join them together.

Invoke-Expression 然后运行脚本,并捕获结果.这对注入攻击是开放的,但在我的特定情况下是可以的.

Invoke-Expression then runs the script, and the result is captured. This is open to injection attacks, but that's OK in my specific case.

或者,如果您更喜欢 PowerShell 简洁:

Or, if you prefer your PowerShell terse:

PS> $data = gc .\foo.pson | Out-String | iex

(我找不到更短的Out-String)

这篇关于从文件加载 PowerShell 哈希表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆