powershell script reading parameters from txt

醉酒当歌 提交于 2019-12-05 22:51:40

When you import the file with Import-Csv cmdlet, you get objects of type PSCustomObject back.

The splatting operator (@) expects a hashtable, not a PSCustomObject.


PowerShell 3.0+

To import the parameters from the txt file, you could use ConvertFrom-StringData cmdlet to return them as hashtables instead:

Get-Content -Path .\test.txt -ReadCount 2 | ForEach-Object {
    $Splat = ConvertFrom-StringData $($_ -join [Environment]::NewLine)
    .\paramtest.ps1 @Splat
}

And format your text file like this:

text.txt

param=foo
param2=c:\\temp
param=bar
param2=c:\\temp
param=foobar
param2=c:\\temp

PowerShell 2.0

If you are working with PowerShell 2.0, or if you need to retain the csv format, you can do the work yourself by refencing the values from PSCustomObject into a new hashtable and splat that:

Import-Csv .\test.txt |ForEach-Object {
    $Splat = @{}
    $_.psobject.Properties |ForEach-Object { $Splat[$_.Name] = $_.Value }
    .\paramtest.ps1 @Splat
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!