Powershell电子邮件用户
我有这个脚本,但我正在寻找一个解决方案,不使用txt文件。你知道吗我怎么能做到 基本上,我想通知用户,他们必须重新启动才能完成Windows更新的安装Powershell电子邮件用户,powershell,windows-update,Powershell,Windows Update,我有这个脚本,但我正在寻找一个解决方案,不使用txt文件。你知道吗我怎么能做到 基本上,我想通知用户,他们必须重新启动才能完成Windows更新的安装 [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administrat ion") | out-null if (!$wsus) { $wsus = [Microsoft.UpdateServices.Administration.Admin
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administrat ion") | out-null
if (!$wsus) {
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
}
$computerScope = new-object Microsoft.UpdateServices.Administration.ComputerTargetScope;
$computerScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::InstalledPendingReboot;
$updateScope = new-object Microsoft.UpdateServices.Administration.UpdateScope;
$updateScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::InstalledPendingReboot;
$computers = $wsus.GetComputerTargets($computerScope);
$Usernames = foreach ($Computer in $computers) {
$Error.Clear()
(get-wmiobject Win32_ComputerSystem -ComputerName $Computer.FullDomainName - ErrorAction SilentlyContinue).UserName.Split("\")[1]
}
$Emailadress = ForEach ($Username in $Usernames) {
Get-ADUser -Identity $Username -Properties EmailAddress | select EmailAddress } $Emailadress | Out-File C:\Myscript\email.txt
$Emails = Get-Content C:\Myscript\email.txt
ForEach ($Email in $Emails) {
$WarnMsg = "
<p style='font-family:arial'>Bonjour,</p>
<p style='font-family:arial'>Votre ordinateur dois être redémaré pour finir l'installation de mise à jours,</p>
<p style='font-family:arial'>Merci.</p>"
$Enc = New-Object System.Text.utf8encoding
send-mailmessage -to $Email -from noreply@noreply.com -Subject "Mise à jours" -body $WarnMsg -smtpserver x.x.x.x -BodyAsHtml -Encoding $Enc}
试试这个:
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | Out-Null
if (!$wsus) {
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
}
$computerScope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
$computerScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::InstalledPendingReboot
$updateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$updateScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::InstalledPendingReboot
$computers = $wsus.GetComputerTargets($computerScope)
$Usernames = foreach ($Computer in $computers) {
$Error.Clear()
(Get-WmiObject Win32_ComputerSystem -ComputerName $Computer.FullDomainName -ErrorAction SilentlyContinue).UserName.Split("\")[1]
}
#this does not change, no need to put it in the foreach loop
$WarnMsg = "<p style='font-family:arial'>Bonjour,</p>
<p style='font-family:arial'>Votre ordinateur doit être redémarré pour finir l'installation de mises à jour,</p>
<p style='font-family:arial'>Merci.</p>"
$Enc = New-Object System.Text.utf8encoding
#for each user
foreach($Username in $Usernames) {
#get email address
$emailAddress = Get-ADUser -Identity $Username -Properties EmailAddress | Select-Object -ExpandProperty EmailAddress
#use it to send a mail
Send-MailMessage -To $emailAddress -From noreply@noreply.com -Subject "Mises à jour" -Body $WarnMsg -SmtpServer x.x.x.x -BodyAsHtml -Encoding $Enc
}
还有,拖尾;PowerShell中不需要。它可以用于将多个命令放在同一行上,但通常可读性较差
我是法国小城镇的居民^^