我有一个foreach循环并使用write-host cmdlet写入控制台.我现在希望将这些行写入一个变量,该变量将存储循环的所有结果.什么是cmdlet /语法?
Kei*_*ill 13
这有几种方法可以做到这一点.将行放在一个字符串中:
$lines = ''
for ($i=0; $i -lt 10; $i++)
{
$lines += "The current value of i is $i`n"
}
$lines
Run Code Online (Sandbox Code Playgroud)
或者作为一个字符串数组,其中每一行是数组中的不同元素:
$lines = @()
for ($i=0; $i -lt 10; $i++)
{
$lines += "The current value of i is $i"
}
$lines
Run Code Online (Sandbox Code Playgroud)