2014-10-17 57 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