Powershell 从文本文件中检索变量 [英] Powershell retrieving a variable from a text file

查看:69
本文介绍了Powershell 从文本文件中检索变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法读取文本文件 C:\test.txt 并检索特定值?

Is there a way to read a text file C:\test.txt and retrieve a particular value?

ie 文件看起来像这样:

ie file looks like this:

serverName=serv8496
midasServer=serv8194

我想以某种方式从此文件中设置脚本中变量的值,例如:

I want to set the value of a variable in my script in some way from this file eg:

$MidasServer= (from file midasServer value)

我不会知道引用所在的行号.

I will not know the line number where the reference is.

有没有办法做到这一点?

Any way to do this?

推荐答案

是的,读取文件,拆分每一行,并将拆分结果赋值给Name和Value参数:

Yes, read the file, split each line and assign the split result to the Name and Value parameters:

Get-Content file.txt | Foreach-Object{
   $var = $_.Split('=')
   New-Variable -Name $var[0] -Value $var[1]
}

这篇关于Powershell 从文本文件中检索变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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