Powershell 电子邮件用户
Powershell Email user
我有这个有效的脚本,但我正在寻找不使用 txt 文件的解决方案。你知道我该怎么做
基本上我想通知用户他们必须重新启动才能完成 Windows 更新的安装。
[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 不需要尾随 ;
。它可用于将多个命令放在同一行,但通常可读性差得多。
(je me suis permis de corriger les petites fautes de français ^^)
我有这个有效的脚本,但我正在寻找不使用 txt 文件的解决方案。你知道我该怎么做
基本上我想通知用户他们必须重新启动才能完成 Windows 更新的安装。
[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 不需要尾随 ;
。它可用于将多个命令放在同一行,但通常可读性差得多。
(je me suis permis de corriger les petites fautes de français ^^)