2012-06-18 2 views
0

암호가 < = 10 일 후에 만료되는 사용자의 전자 메일을 생성하는 powershell 스크립트가 있습니다. 전자 메일은 HTML로 포맷되어 있지만주의를 끌기 위해 한 문장의 글꼴 색을 빨간색으로 변경하고 싶습니다. 그러나 html 코드 주위의 인용 부호에 문제가 있습니다. 큰 따옴표를 사용하면 powershell은 리터럴 메시지를 출력하고 작은 따옴표가있는 오류 메시지를 표시합니다. PowerShell 전자 메일에서 여러 글꼴 색을 사용하는 방법이 있습니까?Powershell 전자 메일의 HTML 글꼴 색상 설정

다음은 현재 사용중인 코드입니다. PowerShell의 스크립트에서 처음 시도한 내용을 추가 할 것입니다. 따라서 필자는 먼 길을 가고 있다면 입력에 대해 열어두고 있습니다.

# Import ActiveDirectory module for Powershell V2 AD cmdlets 
    import-module activedirectory 
    # Uncomment the following line to include optional cmdlets included with Exchange 2010 schema changes. No such cmdlets are included in this script 
    # add-pssnapin microsoft.exchange.management.powershell.e2010 

    #Import the maximum password age from Active Directory GPO policy from domain 
    $maxdays=(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays 
    $date = date 

    # Simple HTML Message to format body of email. Body is broken up into four parts for appearance and for easy function insertion into message. 
    $body1 += "<html><body><br> Your network password will expire in " 
    $body2 += " day(s).</body><html>" 
    $body3 += "<html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password." 
    $body3 += "<br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website." 
    $body3 += "<br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309" 
    $body3 += "<br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also." 
    $body3 += "<br><br>Thank you," 
    $body3 += "<br> IS Department" 
    $body3 += "<br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/>" 
    $body3 += "<br><br><hr>" 
    $body3 += "From <b> IS Department</b>" 
    $body3 += "<br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is" 
    $body3 += "<br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this" 
    $body3 += "<br>communication, or any part thereof, is strictly prohibited and may be unlawful. The IT Department thanks you for your cooperation.<br>" 
    $body4 += "<br><hr><br></body></html>" 

    # Combine body segments into string for display 
    $bod1y=$body1 | out-string 
    $body2=$body2 | out-string 
    $body3=$body3 | out-string 
    $body4=$body4 | out-string 

    #Gather ADusers which are enabled, password is set not set to never expire and all properties of user object. *Note Extension Attributes will not show up unless they are populated. 
    (Get-ADUser -filter {(Enabled -eq "True") -and (PasswordNeverExpires -eq "False")} -properties *) | Sort-Object pwdLastSet | 

    #Loop to validate password age of each account and generate email. Emails to non-domain addresses are generated based on extensionattribute1 and extensionattribute2. 
    #Active Directory is pre-populated with the user address as extensionattribute1 and domain information in extensionattribute2. For example, johndoe = extensionattribute1 
    # gmail.com = extensionattribute2. 
    foreach-object { 
    $lastset=Get-Date([System.DateTime]::FromFileTimeUtc($_.pwdLastSet)) 
    $expires=$lastset.AddDays($maxdays).ToShortDateString() 
    $daystoexpire=[math]::round((New-TimeSpan -Start $(Get-Date) -End $expires).TotalDays) 
    $samname=$_.samaccountname 
    $firstname=$_.GivenName 
    $lastname=$_.SN 
    $extensionattribute1=$_.extensionattribute1 
    $extensionattribute2=$_.extensionattribute2 
    $recipient="[email protected]$extensionattribute2" 
       if (($daystoexpire -ge 1) -and ($daystoexpire -le 10)) { 
       $ThereAreExpiring=$true 

       $email = @{ 
       to = "$recipient" 
       from = '[email protected]' 
       subject = "$firstname $lastname your network password will expire in $daystoexpire day(s)" 
       body = "$firstname $lastname" + " $body1" + "$daystoexpire" + "$body2" + "$body3" + "$date" + "$body4" 
       smtpserver = 'smtp.server.org' 
       # attachments = "p:\documents\citrix\citrix_password_reset.doc" 
      } 
     Send-MailMessage @email -BodyAsHTML 
     } 

은}`

답변

0

당신은 파워 쉘의 "여기에 문자열"기능 봤어? technet article that discusses the feature이 있습니다. 물건들을위한 템플릿 인 문자열에 항상 사용합니다.

저는 이러한 템플릿에서 {0} 같은 C# 스타일 자리 표시자를 사용하고 싶습니다. 이것은 날짜와 통화의 공상적인 형식을 허용합니다. (필자의 예에서 '멋진'날짜 형식을 사용하고 있습니다.)

템플릿을 플레이스 홀더와 함께 사용하면 특정 순서로 문자열을 연결하거나 $ firstname과 같은 항목을 기억할 필요가 없습니다. 그러한 연계 안에서 가야한다. 그것은 또한 더 쉽게 국제화되기로되어 있지만, 한번도 해본 적이 없습니다.

여기 빠른 예제가 있습니다. 루프를 루핑 로직에 통합해야합니다.

# first, stick the template into a variable for later use. Use a "here string" for ease of formatting and editting. 
$bodyTemplate = @" 
{0} {1} 
<html><body><br> Your network password will expire in {2} day(s).</body><html> 
<html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password. 
<br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website. 
<br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309 
<br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also. 
<br><br>Thank you, 
<br> IS Department 
<br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/> 
<br><br><hr> 
From <b> IS Department</b> 
<br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is 
<br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this 
communication, or any part thereof, is strictly prohibited and may be unlawful. The IT Department thanks you for your cooperation.<br> 
{3:D} 
<br><hr><br></body></html> 
"@ 

# Now, loop through your users, calculate $DaysUntilExpiry, test the value and build the email to be sent 
# I'm just making up some dumb values here 
$daystoexpire = 42 # or whatever 
$firstname = "George" 
$lastname = "Washington" 
$date = date 

# using the template, stick the appropriate values into place and store that in a variable for convenience 
$body = $bodyTemplate -f $firstname, $lastname, $daystoexpire, $date 

# do whatever we want with $body 
write-host $body 
+0

나를 가리켜 주셔서 감사합니다. 그것은 약간 조정과 함께 일했습니다. 이중 따옴표와 # 문장을 조합하여 문장의 나머지 부분을 주석 처리했지만 코드를 으로 변경했습니다. C# 자리 표시 자 사용 팁에도 감사드립니다. 그것은 앞으로의 스크립트에서 매우 유용 할 것입니다. – Posher

관련 문제