首页 > 解决方案 > 使用 Powershell 将多行对象转换为多个对象

问题描述

我有一个变量$c

$c返回

apples
oranges
bananas

如何制作这些单独的对象?

$fruit = $C |  foreach { $_ -like 'oranges'}

if($fruit)
{
  write-host "Success" -ForegroundColor Green
}
else
{
  write-host "Failed" -ForegroundColor red
}

除非我使用通配符,否则这不起作用?

我试图得到一个完全匹配的检查每一行。

任何帮助表示赞赏。我坚持这个。

标签: powershell

解决方案


使用-split运算符

$multiLineString = @'
apples
oranges
bananas
'@

$arrayOfStrings = $multiLineString -split '\r?\n' 

\r?\n要么匹配CR LF,要么单独匹配,LF因此它适用于输入字符串中的 UNIX 和 Windows 样式的行分隔符。


推荐阅读