Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Powershell将循环的输出移动到阵列中_Powershell - Fatal编程技术网

Powershell将循环的输出移动到阵列中

Powershell将循环的输出移动到阵列中,powershell,Powershell,这似乎是一个简单的答案,但就我个人而言,我想不出来。我想做的是从一个文本文件中获取一堆电子邮件地址,然后去掉@后面的电子邮件地址(例如,如果电子邮件地址是adam@home.com我对亚当的比特感兴趣) 下面的脚本输出我想要的值,但是我希望将输出写入数组而不是控制台。这样做的最佳方式是什么 代码如下,谢谢 # Import the text file into a variable $import = Get-Content c:\\temp\test.txt # Variable to ho

这似乎是一个简单的答案,但就我个人而言,我想不出来。我想做的是从一个文本文件中获取一堆电子邮件地址,然后去掉@后面的电子邮件地址(例如,如果电子邮件地址是adam@home.com我对亚当的比特感兴趣)

下面的脚本输出我想要的值,但是我希望将输出写入数组而不是控制台。这样做的最佳方式是什么

代码如下,谢谢

# Import the text file into a variable 
$import = Get-Content c:\\temp\test.txt
# Variable to hold total number of emails
$totalEmails = $import.Count
#Variable to run loop
$start = 0
#Used as a separator to look at the first part of the email address
$separator = "@"
# Will increment by two to show the first part of the array in parts which     holds the text before the @ 
$even = 0
# Username after @
$userName


#Strip out the text before the @
do {

    $parts = $import.split($separator)
    $even+=2

    # This line here is whats not working, I would like to have the output of     each echo written into the array $userName 
    $userName = echo $parts[$even]


    $start++

} while ($totalEmails -gt $start)

你的电子邮件解析没有什么意义。这里有一句话来回答你的问题:

$Arr = @(Get-Content -Path 'C:\temp\test.txt') -replace '@.+'

这将替换整个脚本:
$array=Get Content“C:/temp/test.txt”|%{$| | Split String-Separator“@”| Select-First 1}
查看Hello,感谢您的回复!我试图运行此命令,但在splitstring语句中出现错误?确定吗?有什么错误?@Clijsters打赌它的
分割字符串:术语“分割字符串”不被识别为cmdlet、函数、脚本文件的名称,
没有分割字符串cmdlet。这就成功了,非常感谢!为了让我能理解我到底做了什么,“-replace'@.+'”语句就是把@后面的其余文本切掉?是的,它用空字符串替换@字符和它后面的每个字符“它实际上不适用于这种情况,因为正则表达式很简单,但是我很少有机会引用我最喜欢的一句话:“有些人在遇到问题时,会想“我知道,我会使用正则表达式。”现在他们有两个问题了。”-杰米Zawinski@MrI正则表达式转换为:literal@character,通配符1+。所以,@和所有内容直到行尾,通过省略第二个参数,它将被删除。