2014-10-17 52 views
0

我正在使用PowerShell脚本来搜索远程计算机上的某个文件,并将它们保存到xx.txt文件中,并在扫描正常时发送带有附加文件的电子邮件,如果不是这样只是发送错误信息的电子邮件。 (我有70台电脑的域名)同时处理2个列表

现在我要做的是用用户名加入计算机名称。 (我还是很新的用PowerShell脚本)

这是脚本:

$people = Get-Content "D:\Script\people.txt" # list of people. One user per line. 
$computers = Get-Content "D:\Script\list.txt" # list of computers is stored in list.txt file. One coputer name per line 
$day = (get-date).DayOfWeek # day of week 
$time = get-date -Format "HH:mm:ss" # time 
$date = get-date -Format "dd.MM.yyyy" # date 

foreach ($a in $people){}  

foreach ($i in $computers) 

    {$i + $a + "`n" + 
       "=================================================" 
       "|   " + $time + " - " + $day + " - " + $date+"  |"   # 14:57:54 - Friday - 17.10.2014 
       "=================================================" 
       "|  Testing connection on computer $i |" 
       "=================================================" 

       $test_connect = Test-Connection -ComputerName $i -Count 2 -ErrorAction SilentlyContinue # Ping computer with 2 counts and don't dispay errors 

      if ($test_connect -ne $null) # if ping OK, continue with searching for files 
       { 
        "=================================================" 
        "|   Testing connection PASSED   |" 
        "|  Scan started on $i at $a    |" 
        "=================================================" 

        $RemoteSB = $executioncontext.invokecommand.NewScriptBlock("get-childitem c:\*,d:\* -include *.xls,*.xlsx,*.mobi,*.avi,*.mp3,*.mp4,*.csv,*.aif,*.iff,*.mid,*.ra,*.wav,*.wma,*.mov,*.mpg,*.rm,*.wmv,*.epub -exclude *.xlm -recurse") 
        invoke-command -computername $i -scriptblock $RemoteSB -ErrorAction SilentlyContinue > X:\$i.txt 

       $smtpTo = "[email protected]" 
       "=================================================" 
       "|    Search done on $i    |" 
       "|   Continuing to next computer   |" 
       "|  email report sent to $smtpTo  |" 
       "|   !!!! SCAN COMPLETE !!!    |" 
       "=================================================" 
       "        " 
       $file = "X:\$i.txt" # file name list of computers and their destination PATH$ 
       $smtpServer = "smtp.gmail.com" # enter your SMTP server 
       $smtpFrom = "[email protected]" # send email FROM address 
       $smtpTo = "[email protected]" # send email TO address 
       $messageSubject = "Scan Done for computer $i on $date" # SUBJECT line 
       $messagebody = "Scan for computer $i is done on $day $date at $time, continuing to next one. This computer belongs to $a." #email BODY text 
       Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Subject $messageSubject -Body $messagebody -attachment $file # Sendmail command 

       } 

      else # if PING fails send email with error 
       { 
        "=================================================" 
        "|    !!!! ERROR !!!     |" 
        "=================================================" 
        "=================================================" 
        "|  Testing connection on $i FAILED  |" 
        "=================================================" 
        " " 

       $smtpServer = "smtp.gmail.com" # enter your SMTP server 
       $smtpFrom = "[email protected]" # send email FROM address 
       $smtpTo = "[email protected]" # send email TO address 
       $messageSubjectError = "!!!ERROR!!! Scan for computer $i failed" # SUBJECT line 
       $messagebodyError = "Error Scan for computer $i at $day $date on $time, continuing to next one." # email BODY text 
       Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Subject $messageSubjectError -Body $messagebodyError # Sendmail command 

       } 
     } 

脚本是可以正常使用,如果我不把第一的foreach($在$人一){} 我觉得($ a中的$ people){XXXXXXXXX}或类似的东西缺失,但我仍然不明白。

如果有人可以帮助,我将不胜感激。

问候

的Neven Gotovac

UPDATE:

试着用下面的答案,但它没有工作,或者我已经把它错了行。 脚本的输出如下所示。如果你检查你会看到那个名字“augustin”总是重复。相反,它应该像计算机名称一样改变。

PS C:\Users\Administrator> D:\Script\run3.ps1 
    c04 - augustin 
    ================================================= 
    |   17:44:29 - Friday - 17.10.2014  | 
    ================================================= 
    |  Testing connection on computer c04 | 
    ================================================= 
    ================================================= 
    |    !!!! ERROR !!!     | 
    ================================================= 
    ================================================= 
    |Testing connection on c4 for augustin FAILED | 
    ================================================= 

    C37 - augustin 
    ================================================= 
    |   17:44:29 - Friday - 17.10.2014  | 
    ================================================= 
    |  Testing connection on computer C37  | 
    ================================================= 
    ================================================= 
    |    !!!! ERROR !!!     | 
    ================================================= 
    ================================================= 
    |Testing connection on C37 for augustin FAILED | 
    ================================================= 

    C51 - augustin 
    ================================================= 
    |   17:44:29 - Friday - 17.10.2014  | 
    ================================================= 
    |  Testing connection on computer C51  | 
    ================================================= 
    ================================================= 
    |    !!!! ERROR !!!     | 
    ================================================= 
    ================================================= 
    |Testing connection on C51 for augustin FAILED | 
    ================================================= 

@马特 我已经把括号像你说的,但它仍然重复的用户名:(是不是一个问题,如果用户名在中间firstname.lastname点在people.txt文件

+0

是否应该有人与计算机之间的关系?就像马特说的,你现在的代码列出了每一台电脑的每个人。 – 2014-10-17 16:20:56

+0

是的,它假设是关系。例如:c01 - augustin – 2014-10-17 16:27:56

回答

3

我还没有如果这条线

foreach ($a in $people){} 

虽然这样的工作分配给它是空的表达{}。你需要在大括号移动到脚本的末尾检查所有的代码,但一个问题,我看到的。

foreach ($a in $people){}  
foreach ($i in $computers) 

    {$i + $a + "`n" + 
       "=================================================" 
       "|   " + $time + " - " + $day + " - " + $date+"  |"   
       "=================================================" 
       #truncated to save space    
    } 

应改用

foreach ($a in $people){  
foreach ($i in $computers) 

    {$i + $a + "`n" + 
       "=================================================" 
       "|   " + $time + " - " + $day + " - " + $date+"  |"   
       "=================================================" 
       #truncated to save space    
    } 
} 

你有听到对子级处理每个人$a每台计算机的逻辑$i

从问题编辑更新

我认为你需要用相应的用户检查每台计算机,是否正确?如果是这种情况,您可以通过单个索引循环进行循环。有一个更好的方法来做到这一点,但这将与您当前的代码很好地融合。

For($index=0;$index -lt $people.Count; $index++){ 
    $a = $people[$index] 
    $i = $computers[$index] 

    $i + $a + "`n" + 
    #... Process Stuff Here 

} 

使用它代替当前代码中的两个for循环。

+0

当脚本到达时,它会更改用户名(检查所有计算机) – 2014-10-17 16:06:59

+0

代码正在工作,因为我已经记录了它,现在从你更新,我想我看到了你想要的内容。你想合并文件的内容吗?也就是说'$ people'的第一行中的用户匹配'$ computers'中的第一行。你似乎想要检查所有计算机上的所有用户。两个文本文件的长度是否相同? – Matt 2014-10-17 16:17:40

+0

是的,我想合并。正如你所说的那样。是的,他们是相同的长度。 – 2014-10-17 16:23:18