Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell syntax highlighting disrupted by single quotes in HTML #196

Open
mike-armand opened this issue Feb 4, 2020 · 0 comments
Open

Comments

@mike-armand
Copy link

mike-armand commented Feb 4, 2020

Environment

  • Editor and Version:
    VS Code
    Version: 1.41.1 (user setup)
    Commit: 26076a4de974ead31f97692a0d32f90d735645c0
    Date: 2019-12-18T14:58:56.166Z
    Electron: 6.1.5
    Chrome: 76.0.3809.146
    Node.js: 12.4.0
    V8: 7.6.303.31-electron.0
    OS: Windows_NT x64 10.0.17763

  • Your primary theme:
    Dark+ (default dark)
    (I did try out the PowerShell ISE theme, but it has same issue.)

Issue Description

We have a PowerShell script that sends out an HTML email, so the HTML is inline in the script. The issue is that there is text that includes an apostrophe. The syntax highlighter interprets this as a single quote and doesn't escape out of it until the next random apostrophe.

I've opened the same script in PowerShell ISE, and it handles it fine.

Screenshots

image
image

Expected Behavior

I would expect it to behave like PowerShell ISE.

Code Samples

function Get-ADUserPasswordExpirationDate {
	[cmdletBinding(SupportsShouldProcess = $true)]
	Param (
		[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, HelpMessage = "Identity of the Account")]
		[Object]$accountIdentity
	)
	PROCESS {
		Write-Verbose "Getting the user info for $accountIdentity"
		$accountObj = Get-ADUser $accountIdentity -properties PasswordExpired, PasswordNeverExpires, PasswordLastSet, name, mail
		# Make sure the password is not expired, and the account is not set to never expire
    Write-Verbose "verifying that the password is not expired, and the user is not set to PasswordNeverExpires"
    if (((!($accountObj.PasswordExpired)) -and (!($accountObj.PasswordNeverExpires))) -or ($PreviewUser)) {
    	Write-Verbose "Verifying if the date the password was last set is available"
    	$passwordSetDate = $accountObj.PasswordLastSet     	
      if ($passwordSetDate -ne $null) {
      	$maxPasswordAgeTimeSpan = $null
        # see if we're at Windows2008 domain functional level, which supports granular password policies
        Write-Verbose "Determining domain functional level"
        if ($global:dfl -ge 4) { # 2008 Domain functional level
          $accountFGPP = Get-ADUserResultantPasswordPolicy $accountObj
          if ($accountFGPP -ne $null) {
          	$maxPasswordAgeTimeSpan = $accountFGPP.MaxPasswordAge
					} else {
						$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
					}
				} else { # 2003 or ealier Domain Functional Level
					$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
				}				
				if ($maxPasswordAgeTimeSpan -eq $null -or $maxPasswordAgeTimeSpan.TotalMilliseconds -ne 0) {
					$DaysTillExpire = [math]::round(((New-TimeSpan -Start (Get-Date) -End ($passwordSetDate + $maxPasswordAgeTimeSpan)).TotalDays),0)
					if ($preview){$DaysTillExpire = 1}
					if ($DaysTillExpire -le $DaysToWarn){
						Write-Verbose "User should receive email"
						$PolicyDays = [math]::round((($maxPasswordAgeTimeSpan).TotalDays),0)
						if ($demo)	{Write-Host ("{0,-25}{1,-8}{2,-12}" -f $accountObj.name, $DaysTillExpire, $PolicyDays)}
            # start assembling email to user here
						$EmailName = $accountObj.name						
						$DateofExpiration = (Get-Date).AddDays($DaysTillExpire)
						$DateofExpiration = (Get-Date($DateofExpiration) -f $DateFormat)						

Write-Verbose "Assembling email message"						
[string]$emailbody = @"
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	</head>
<body>	
"@

if (!($NoImages)){
$emailbody += @"	
	<table id="email" border="0" cellspacing="0" cellpadding="0" width="655" align="center">
		<tr>
			<td align="left" valign="top" padding: 3px >
			</td>
		</tr>
"@						

if ($OurURL){		
$emailbody += @"
			<tr><td height="121" align="left" valign="bottom" bgcolor= "314280"></a></td></tr>
"@
}else{
$emailbody += @"	
			<tr><td height="121" align="left" valign="bottom" bgcolor= "314280"></td></tr>
"@
}

$emailbody += @"
		<tr>
			<td>
				<table id="body" border="0" cellspacing="0" cellpadding="8">
					<tr><br>
						<td width="1" align="left" valign="top" bgcolor="#a8a9ad"></td>
						<td></td>
						<td id="text" width="572" align="left" valign="top" style="font-size: 12px; color: #000000; line-height: 17px; font-family: Verdana, Arial, Helvetica, sans-serif">
"@
}
if ($DaysTillExpire -le 1){
	$emailbody += @"
		<div align='center'>
			<table border='0' cellspacing='0' cellpadding='8' style='width:510px; background-color: white; border: 0px;'>
				<tr>
"@
if (!($NoImages)){
$emailbody += @"
					<td align='right'></td>	
"@
}
$emailbody += @"
					<td style="font-family: verdana; background: #E12C10; text-align: center; padding: 0px; font-size: 9.0pt; color: white"><b>ALERT:</b> You must change your password today or you will be locked out!</td>		
"@
if (!($NoImages)){
$emailbody += @"
					<td align='left'></td>
"@
}
$emailbody += @"
				</tr>
			</table>
		</div>
"@
}

$emailbody += @"
			<p style="font-weight: bold">Hello $EmailName,</p>
			<p>Your $company password expires in <span style="background-color: red; color: white; font-weight: bold;">&nbsp;$DaysTillExpire&nbsp;</span> day(s), on $DateofExpiration.</p>
			<p>Please reset your password soon in order to avoid account lockout, which will temporarily suspend your access to Our systems. To reset your password, follow these quick steps:</p>
			<p><span style="font-weight: bold">Windows Users:</span></p>
			<ol>
				<li>Ensure you are connected to the network</li>
				<ul>
					<li>From within the office, connect to the network</li>
					<li>From outside of the office, connect to the VPN</li>		
				</ul>
				<li>Press Ctrl+Alt+Delete</li>
				<li>Click Change Password</li>
				<li>Enter your old password and a new password and click OK</li>
			</ol>
			<p><span style="font-weight: bold">Remote Users with no access to VPN:</span></p>
			<ol>
				<li>Browse to <a href="https://OurURL.com">OurURL.com</a></li>
				<li>Click on the drop down list in the top right - <span style="font-weight: bold">Welcome $EmailName</span></li>
				<li>Click Change Password</li>
				<li>Enter your username, old password, new password twice and click Change</li>
			</ol>
			<p><span style="font-weight: bold">*** Attention Smart Phone and iPad Users:</span> After resetting your password with the steps above, you will also need to update the your email password stored on all of your mobile devices.</p> 
			<p><span style="font-weight: bold">iPhone/iPad Users -- Reset your email Password:</span><br>Go to Settings &gt; Mail &gt; Exchange &gt; Account.</br></p>
			<p><span style="font-weight: bold">Mac Users:</span></p>
			<ol>
				<li>Login to your Mac with your active directory account</li>
				<li>Ensure you are connected to the network</li>
				<ul>
					<li>From within the office, connect to the network</li>
					<li>From outside of the office, connect to the VPN</li>
				</ul>
				<li>From the Apple Menu in the top left corner, select System Preferences</li>
				<li>Select Users and Groups</li>
				<li>Click the Change Password button</li>
			</ol>
			<p>Remember, if you do not change your password before it expires on $DateofExpiration, you will be locked out of all $company Computer Systems until an Administrator unlocks your account.</p>
			<p>You will continue to receive these emails until the password is changed or expires.  Please contact the service desk if you have trouble resetting your password - <a href="mailto:us@us.com"> Our Team</a>.</p>
            
			<p>Thank you,<br />
			$company <br />
			$HelpDeskPhone</p>
"@			
if ($accountFGPP -eq $null){ 
	$emailbody += @"
			<table style="background-color: #dedede; border: 1px solid black">
				<tr>
					<td style="font-size: 12px; color: #000000; line-height: 17px; font-family: Verdana, Arial, Helvetica, sans-serif"><b>$company Password Policy</b>
						<ul>
							<li>Your password must have a minimum of a $MinPasswordLength characters.</li>
							<li>You may not use a previous password.</li>
							<li>Your password must not contain parts of your first, last, or logon name.</li>
							<li>Your password must be changed every $PolicyDays days.</li>
"@							

if ($PasswordComplexity){
	Write-Verbose "Password complexity"
	$emailbody += @"
							<li>Your password requires a minimum of two of the following three categories:</li>
							<ul>
								<li>1 upper case character (A-Z)</li>
								<li>1 lower case character (a-z)</li>
								<li>1 numeric character (0-9)</li>								
							</ul>
"@
}
$emailbody += @"
							<li>You may not reuse any of your last $PasswordHistory passwords</li>
						</ul>
					</td>
				</tr>
			</table>
"@
}
if (!($NoImages)){
$emailbody += @"								
							</td>
							<td width="49" align="left" valign="top"></td>
							<td width="1" align="left" valign="top" bgcolor="#a8a9ad"></td>
						</tr>
					</table>
					<table id="footer" border="0" cellspacing="0" cellpadding="0" width="655">
						<tr>
							<td height="110" align="left" valign="bottom" bgcolor= "#6c88b6"></td>
						</tr>
					</table>
					<table border="0" cellspacing="0" cellpadding="0" width="655" align="center">
						<tr>
							<td align="left" valign="top"></td>
							<td align="middle" valign="top"><font face="Verdana" size="1" color="#000000"><p><b>This email was sent by Our Team's automated process.</b>
"@
}
if ($OurURL){
$emailbody += @"															
							
"@
}
if (!($NoImages)){
$emailbody += @"
								</p></font>
							</td>
							<td align="left" valign="top"></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
"@
}
$emailbody += @"
	</body>
</html>
"@
						if (!($demo)){
							$emailto = $accountObj.mail
							if ($emailto){
								Write-Verbose "Sending demo message to $emailto"
								Send-MailMessage -To $emailto -Subject "Your password expires in $DaysTillExpire day(s)" -Body $emailbody -From $EmailFrom -Priority High -BodyAsHtml
								$global:UsersNotified++
							}else{
								Write-Verbose "Can not email this user. Email address is blank"
							}
						}
					}
				}
			}
		}
	}
} # end function Get-ADUserPasswordExpirationDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant