我正在开发powershell脚本,它搜索远程计算机上的某些文件并将它们管道(保存)到xx.txt文件,如果扫描没问题就发送附带文件的电子邮件,如果不是,它只会发送电子邮件错误信息。 (我在域中有70台计算机)
现在我要做的是将计算机名称与用户名联系起来。 (我对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 = "example@example.com"
"================================================="
"| 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 = "example@example.com" # send email FROM address
$smtpTo = "example@example.com" # 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 = "example@example.com" # send email FROM address
$smtpTo = "example@example.com" # 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 in $ people){}脚本工作正常 我认为($ a in $ people){XXXXXXXXX}或类似的东西都缺失了但是我仍然不明白。
如果有人可以提供帮助,我将不仅仅是欣赏。
问候
Neven Gotovac
更新:
尝试下面的答案,但它没有奏效,或者我把它放错了路线。 脚本的输出看起来像这样。如果你检查你会看到“奥古斯丁”这个名字总是重复。相反,它应该像计算机名称一样进行更改。
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 |
=================================================
@马特 我按你说的方式放了括号,但它仍然重复用户名:(如果用户名在people.txt文件中的firstname.lastname中有点,那么这是一个问题
答案 0 :(得分: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循环。