Powershell从文本文件中检索变量

时间:2022-03-20 13:32:04

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

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

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?

有什么办法吗?

2 个解决方案

#1


16  

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]
}

#2


5  

If that is exactly how your file appears i.e. a list of key value pairs denoted with a equals sign then you should have a look at ConvertFrom-StringData which

如果这正是您的文件显示的方式,即用等号表示的键值对列表,那么您应该看看ConvertFrom-StringData

converts a string that contains one or more key and value pairs into a hash table. Because each key/value pair must be on a separate line, here-strings are often used as the input format.

将包含一个或多个键和值对的字符串转换为哈希表。因为每个键/值对必须在一个单独的行上,所以here-strings通常用作输入格式。

So if a text file contained just the data in your example you could do this to create a hashtable

因此,如果文本文件仅包含示例中的数据,则可以执行此操作以创建哈希表

$Path = "C:\temp\test.txt"
$values = Get-Content $Path | Out-String | ConvertFrom-StringData
$values.midasServer

Where the $values.midasServer would have the value serv8194. No need to know where the properties are in respect to the file. Your input file can also have varying leading and trailing space around the equals sign which will give the exact same result.

$ values.midasServer的值为serv8194。无需知道属性相对于文件的位置。您的输入文件也可以在等号周围具有不同的前导和尾随空格,这将给出完全相同的结果。

Depending on your use case you can take that one step farther and create a custom object from that hashtable

根据您的使用情况,您可以更进一步,并从该哈希表中创建自定义对象

New-Object -TypeName pscustomobject -Property $values

If you have at least PowerShell v3 or higher you can simplify the process (assuming you want a custom psobject)

如果您至少拥有PowerShell v3或更高版本,则可以简化该过程(假设您需要自定义的psobject)

$values = [pscustomobject](Get-Content $Path -Raw | ConvertFrom-StringData)
$values.midasServer

#1


16  

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]
}

#2


5  

If that is exactly how your file appears i.e. a list of key value pairs denoted with a equals sign then you should have a look at ConvertFrom-StringData which

如果这正是您的文件显示的方式,即用等号表示的键值对列表,那么您应该看看ConvertFrom-StringData

converts a string that contains one or more key and value pairs into a hash table. Because each key/value pair must be on a separate line, here-strings are often used as the input format.

将包含一个或多个键和值对的字符串转换为哈希表。因为每个键/值对必须在一个单独的行上,所以here-strings通常用作输入格式。

So if a text file contained just the data in your example you could do this to create a hashtable

因此,如果文本文件仅包含示例中的数据,则可以执行此操作以创建哈希表

$Path = "C:\temp\test.txt"
$values = Get-Content $Path | Out-String | ConvertFrom-StringData
$values.midasServer

Where the $values.midasServer would have the value serv8194. No need to know where the properties are in respect to the file. Your input file can also have varying leading and trailing space around the equals sign which will give the exact same result.

$ values.midasServer的值为serv8194。无需知道属性相对于文件的位置。您的输入文件也可以在等号周围具有不同的前导和尾随空格,这将给出完全相同的结果。

Depending on your use case you can take that one step farther and create a custom object from that hashtable

根据您的使用情况,您可以更进一步,并从该哈希表中创建自定义对象

New-Object -TypeName pscustomobject -Property $values

If you have at least PowerShell v3 or higher you can simplify the process (assuming you want a custom psobject)

如果您至少拥有PowerShell v3或更高版本,则可以简化该过程(假设您需要自定义的psobject)

$values = [pscustomobject](Get-Content $Path -Raw | ConvertFrom-StringData)
$values.midasServer