2016-11-07 5 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

このお試しください:;はPowerShellで必要とされていない末尾、また

[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 
} 

を。これは、同じ行に複数のコマンドを置くのに使うことができますが、通常は読みにくいです。

(JE私suisのPERMISデcorrigerレプチット・デ・フランス語を^^ fautes)

+0

はsodawillowありがとう –

関連する問題