Arrays 如何从Powershell中的值数组创建文本文件
我有一个文本文件“list.txt”,其中包含数百个URL的列表,我想使用“list.txt”中的每个值将这些URL连同一些常见的配置数据解析为单独的xml文件(配置文件),如下所示: list.txt包含:Arrays 如何从Powershell中的值数组创建文本文件,arrays,powershell,string-interpolation,variable-expansion,Arrays,Powershell,String Interpolation,Variable Expansion,我有一个文本文件“list.txt”,其中包含数百个URL的列表,我想使用“list.txt”中的每个值将这些URL连同一些常见的配置数据解析为单独的xml文件(配置文件),如下所示: list.txt包含: line_1 line_2 line_3 样板配置数据如下所示(以line_1为例): 这远远超出了我的新手Powershell技能。有人能帮忙吗?您正在寻找一种字符串模板方法,其中引用变量的字符串模板根据需要使用当前变量值进行实例化: # Define the XML file con
line_1
line_2
line_3
样板配置数据如下所示(以line_1
为例):
这远远超出了我的新手Powershell技能。有人能帮忙吗?您正在寻找一种字符串模板方法,其中引用变量的字符串模板根据需要使用当前变量值进行实例化:
# Define the XML file content as a *template* string literal
# with - unexpanded - references to variable ${line}
# (The {...}, though not strictly necessary here,
# clearly delineates the variable name.)
$template = @'
<code>
<?xml version="1.0"?>
<Website xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Url>${line}.mydomain.com</Url>
<Title>${line}</Title>
<Enabled>true</Enabled>
<PluginInName>Tumblr</PluginInName>
</Website>
'@
# Loop over all input lines.
Get-Content C:\temp\list.txt | ForEach-Object {
$line = $_ # store the line at hand in $line.
# Expand the template based on the current $line value.
$configFileContent = $ExecutionContext.InvokeCommand.ExpandString($template)
# Save the expanded template to an XML file.
$configFileContent | Set-Content -Encoding Utf8 "$line.xml"
}
可选阅读:选择
-f
和$ExecutionContext.InvokeCommand.ExpandString()
进行模板扩展:
向安斯加致敬,感谢他的帮助
使用-f
:
- 优点:
- 它在调用时明确了将填充哪些值
- 此外,在占位符中包含格式说明更容易(例如,
将数字的格式设置为2位小数){0:N2}
- 显式传递值允许在不同范围内轻松重用模板
- 此外,在占位符中包含格式说明更容易(例如,
- 如果意外传递的值太少或太多,默认情况下会发生错误
- 它在调用时明确了将填充哪些值
- 缺点:
占位符总是位置和抽象的;e、 例如,-f
简单地告诉您正在处理第三个占位符,但没有告诉您它的用途;在具有多个占位符的较大模板中,这可能会成为一个问题{2}
- 即使您传递了正确数量的值,它们的顺序也可能错误,这可能会导致微妙的错误
$ExecutionContext.InvokeCommand.ExpandString()
:
- 优点:
- 如果变量具有描述性名称,则模板将更具可读性,因为占位符(变量名称)将指示它们的用途
- 无需在调用时显式传递值-扩展只依赖于当前范围中可用的变量
- 缺点:
- 如果在多个函数(作用域)中使用模板,则需要确保在每个函数中设置模板中引用的变量
- 至少在默认情况下,
会悄悄地忽略模板中引用的不存在的变量,这可能是需要的,也可能不是需要的$ExecutionContext.InvokeCommand.ExpandString()
- 但是,您可以使用
或更高版本来报告错误;一般来说,使用Set StrictMode-Version 2
是一种很好的做法,不过请注意,它的效果很好,而且可以设置StrictMode
- 通常,您需要手动使模板与设置模板中引用变量的代码保持同步,以确保填写正确的值(例如,如果引用变量的名称更改,则模板字符串也必须更新)
- 但是,您可以使用
$FileName = "C:\temp\list.txt"
$FileOriginal = Get-Content $FileName
# create an empty array
Foreach ($Line in $FileOriginal)
{
$FileModified += $Line
if ($Line -match $pattern)
{
# Add Lines after the selected pattern
$FileModified += 'add text'
$FileModified += 'add second line text'
}
}
Set-Content $fileName $FileModified
# Define the XML file content as a *template* string literal
# with - unexpanded - references to variable ${line}
# (The {...}, though not strictly necessary here,
# clearly delineates the variable name.)
$template = @'
<code>
<?xml version="1.0"?>
<Website xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Url>${line}.mydomain.com</Url>
<Title>${line}</Title>
<Enabled>true</Enabled>
<PluginInName>Tumblr</PluginInName>
</Website>
'@
# Loop over all input lines.
Get-Content C:\temp\list.txt | ForEach-Object {
$line = $_ # store the line at hand in $line.
# Expand the template based on the current $line value.
$configFileContent = $ExecutionContext.InvokeCommand.ExpandString($template)
# Save the expanded template to an XML file.
$configFileContent | Set-Content -Encoding Utf8 "$line.xml"
}
# Define the XML file content as a *template* string literal
# with '{0}' as the placeholder for the line variable, to
# be instantiated via -f later.
$template = @'
<code>
<?xml version="1.0"?>
<Website xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Url>{0}.mydomain.com</Url>
<Title>{0}</Title>
<Enabled>true</Enabled>
<PluginInName>Tumblr</PluginInName>
</Website>
'@
# Loop over all input lines.
Get-Content C:\temp\list.txt | ForEach-Object {
# Expand the template based on the current $line value.
$configFileContent = $template -f $_
# Save the expanded template to an XML file.
$configFileContent | Set-Content -Encoding Utf8 "$line.xml"
}