2016-11-07 134 views
1

我有這個腳本,但我正在尋找一個不使用txt文件的解決方案。你有一個想法,我怎麼能做到這一點Powershell電子郵件用戶

基本上我想通知用戶,他們必須重新啓動才能完成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 [email protected] -Subject "Mise à jours" -body $WarnMsg -smtpserver x.x.x.x -BodyAsHtml -Encoding $Enc} 

回答

1

試試這個:

[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 [email protected] -Subject "Mises à jour" -Body $WarnMsg -SmtpServer x.x.x.x -BodyAsHtml -Encoding $Enc 
} 

此外,尾隨不需要在PowerShell中;。它可用於將多個命令放在同一行上,但通常不易讀。

(je me suis permis de corriger les petites fautes defrançais^^)

+0

謝謝sodawillow –