我在PowerShell对象注释中找到一个包含一些数据的文件 :
@{ X = x ; Y = y }
我喜欢从文件上将它装入变量 。
我在PowerShell对象注释中找到一个包含一些数据的文件 :
@{ X = x ; Y = y }
我喜欢从文件上将它装入变量 。
(我整理一个反程序时发现)
PS> $content = ( Get-Content .foo.pson | Out-String )
PS> $data = ( Invoke-Expression $content )
Get-Content
返回文件中直线的数组; Out-String
用于将它们合并。
Invoke-Expression
然后运行脚本, 并捕捉结果。 这是可以注射攻击的, 但在我的特定情况下是肯定的 。
或者,如果你更喜欢你的PowerShell terse:
PS> $data = gc .foo.pson | Out-String | iex
(找不到较短的 Out-String
形式)
我曾经使用过“ 转换自 String- data ” 。 如果您想要使用此方法, 您需要改变您存储密钥/ 价值对的方式, 将每对密钥/ value 保存在自己的行内, 没有引用 :
#Contents of test.txt
X = x
Y = y
get-content . est.txt | ConvertFrom-StringData
Name Value
---- -----
X x
Y y
From- StringData 是一个内置的 cmdlet 。 我创建了相应的 turne To- StringData 函数, 可在此 < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
如 @Chad 所建议, 我使用 TranslateFrom- StringData 遇到麻烦 。 如果您这样做 :
$hash = get-content . est.txt | ConvertFrom-StringData
我发现我有一个物体阵列,而不是一个散列表。事实上,我似乎有一组散列表,每个表都有一个条目。
$hash.GetType()
看来您需要加入 滑动输入文件的每一行, 以确保它组成一个用于 转换自. s 的单字符串 。 使用 :
$hash = ((get-content . est.txt) -join `n ) | ConvertFrom-StringData
从Powershell5.0开始
Import-PowerShellDataFile
从.psd1 文件导入哪个值 。 所以, 您唯一要做的就是将您的文件重命名为 *. psd1
如果您可以给此文件扩展名 .ps1
, 例如 data.ps1
, 那么不能比此代码简单 :
$data = <path>data.ps1
这是一个更老的岗位, 但是,这有点扭曲了 你所接受的解决方案, 并且也许稍稍“安全”一点, 记住一些不可信的文件。
从您的笔记中, 您有一个含有使用 Powershell 语法的混音器的文件。 鉴于此限制, 您可以直接导入它 :
$HashPath = ".foo.pson"
# input file contents
$filecontent = Get-Content -Path $HashPath -Raw -ErrorAction Stop
# put the file in a script block
$scriptBlock = [scriptblock]::Create( $filecontent )
#check that the file contains no other Powershell commands
$scriptBlock.CheckRestrictedLanguage( $allowedCommands, $allowedVariables, $true )
#execute it to create the hashtable
$hashtable = ( & $scriptBlock )
$pictBlock. check RestratedLanguuage
上的注释, 您可以替换为
$scriptBlock.CheckRestrictedLanguage([string[]]@(), [string[]]@(), $false)
使用空字符串列表, 这样我们就不允许任何 Powershell 命令 。 导入杂列时, 这正是我们想要的 。 最后一个是 < code> allow EnvironmentVables code >, 因此我们限制此示例中的 < code>$false code > 。
旁注, Powershell 模块( psd1 文件) 只是一个散列, 这样这个概念可以帮助您同时在脚本块或其他东西中拉动 。
参考文献: < a href=>
SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...
I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.
I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks
Is it possible to detect that a specific user has an open session on a computer and to open a process in that session so that the application can be interacted with by the user using PowerShell ...
The following line of PowerShell works with IIS 6 installed: $service = New-Object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC") However, with IIS 7 it throws the following error ...
I am using Sandcastle Helpfile Builder to produce a helpfile (.chm). The project is a .shfbproj file, which is XML format, works with msbuild. I want to automatically update the Footer text that ...
I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...
I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...