Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从Powershell中的值数组创建文本文件_Arrays_Powershell_String Interpolation_Variable Expansion - Fatal编程技术网

Arrays 如何从Powershell中的值数组创建文本文件

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

我有一个文本文件“list.txt”,其中包含数百个URL的列表,我想使用“list.txt”中的每个值将这些URL连同一些常见的配置数据解析为单独的xml文件(配置文件),如下所示:

list.txt包含:

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

  • 优点:

    • 它在调用时明确了将填充哪些值

      • 此外,在占位符中包含格式说明更容易(例如,
        {0:N2}
        将数字的格式设置为2位小数)

      • 显式传递值允许在不同范围内轻松重用模板

    • 如果意外传递的值太少或太多,默认情况下会发生错误

  • 缺点:

  • -f
    占位符总是位置和抽象的;e、 例如,
    {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"
}