Forum Discussion
How to update PowerShell script to include extensionattribute2 to $stream.WriteLine
- Jun 10, 2022
That makes sense.
The variable being used, $strName, is used in many places, some of which are filenames.
The problem then is that extensionAttribute2 contains characters like "/" which is what's causing many or all of those errors (I didn't read every single one.)
That's unfortunate, but we can work around that.
Firstly, remove the lines I have you before and put the script back to the way it was - just so we know we're working from a reliable starting point.
Step 1: Create a new variable just for the name including the pronouns.
About eight lines down, you will see these two lines:
$strName = $objUser.DisplayName $strTitle = $objUser.TitleChange these two lines to look like this:
$strName = $objUser.DisplayName if ($objUser.Properties.Contains("extensionAttribute2") -and -not [string]::IsNullOrWhiteSpace($objUser.extensionAttribute2[0])) { $strNameWithPronouns = "$($objUser.DisplayName) $($objUser.extensionAttribute2[0])"; } else { $strNameWithPronouns = $objUser.DisplayName; } $strTitle = $objUser.TitleStep 2: Use the new $strNameWithPronouns variable further down to set the Name.
Further down - around line 45, you will see this line:
$stream.WriteLine("<Table border =`'0`'><tr><td><B><SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT-FAMILY: `'Calibri`'`">" + $strName + "</B></SPAN></td></tr>")Change it to this, to make use of the new variable, "$strNameWithPronouns":
$stream.WriteLine("<Table border =`'0`'><tr><td><B><SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT-FAMILY: `'Calibri`'`">" + $strNameWithPronouns + "</B></SPAN></td></tr>")Cheers,
Lain
You'd want to update the original line about eight lines down from this:
$strName = $objUser.DisplayName
To this (a safe option that behaves even if extensionAttribute2 hasn't been set):
if ($objUser.Properties.Contains("extensionAttribute2") -and -not [string]::IsNullOrWhiteSpace($objUser.extensionAttribute2[0]))
{
$strName = "$($objUser.DisplayName) $($objUser.extensionAttribute2[0])";
}
else
{
$strName = $objUser.DisplayName;
}
Cheers,
Lain
When I add the if statement to the eighth line of the script, the below errors are returned.
mkdir : An item with the specified name C:\Users\boldenc\AppData\Roaming\Microsoft\signatures\ already exists.
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:33 char:1
+ mkdir $FolderLocation
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\Users\bolden...oft\signatures\:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
Remove-ItemProperty : Cannot find path 'HKCU:\Software\Microsoft\Office\15.0\Outlook\Setup\' because it does not exist.
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:37 char:1
+ Remove-ItemProperty -Path HKCU:\Software\Microsoft\Office\15.0\Outloo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKCU:\Software\...\Outlook\Setup\:String) [Remove-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand
New-ItemProperty : Cannot find path 'HKCU:\Software\Microsoft\Office\15.0\Common\MailSettings' because it does not exist.
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:38 char:1
+ New-ItemProperty -Path HKCU:\Software\Microsoft\Office\15.0\Common\Ma ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKCU:\Software\...on\MailSettings:String) [New-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand
New-ItemProperty : Cannot find path 'HKCU:\Software\Microsoft\Office\15.0\Common\MailSettings' because it does not exist.
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:39 char:1
+ New-ItemProperty -Path HKCU:\Software\Microsoft\Office\15.0\Common\Ma ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKCU:\Software\...on\MailSettings:String) [New-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand
Cannot convert value "C:\Users\boldenc\AppData\Roaming\\Microsoft\\signatures\\\\Christina Bolden (She/Her/Hers).htm" to type "System.IO.StreamWriter". Error: "Could not find a part of the path
'C:\Users\boldenc\AppData\Roaming\Microsoft\signatures\Christina Bolden (She\Her\Hers).htm'."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:43 char:1
+ $stream = [System.IO.StreamWriter] "$FolderLocation\\$strName.htm"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastConstructorException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:44 char:1
+ $stream.WriteLine("<!DOCTYPE HTML PUBLIC `"-//W3C//DTD HTML 4.0 Trans ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:45 char:1
+ $stream.WriteLine("<HTML><HEAD><TITLE>Signature</TITLE>")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:46 char:1
+ $stream.WriteLine("<META http-equiv=Content-Type content=`"text/html; ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:47 char:1
+ $stream.WriteLine("<BODY>")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:49 char:1
+ $stream.WriteLine("<Table border =`'0`'><tr><td><B><SPAN style=`"FONT ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:51 char:1
+ $stream.WriteLine("<SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:53 char:1
+ $stream.WriteLine("<SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:55 char:1
+ $stream.WriteLine("<SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:57 char:1
+ $stream.WriteLine("<SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:59 char:1
+ $stream.WriteLine("<SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:61 char:1
+ $stream.WriteLine("<SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:65 char:1
+ $stream.WriteLine("</BODY>")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:66 char:1
+ $stream.WriteLine("</HTML>")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:67 char:1
+ $stream.WriteLine("</DIV>")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Cannot convert value "C:\Users\boldenc\AppData\Roaming\\Microsoft\\signatures\\\\Christina Bolden (She/Her/Hers).txt" to type "System.IO.StreamWriter". Error: "Could not find a part of the path
'C:\Users\boldenc\AppData\Roaming\Microsoft\signatures\Christina Bolden (She\Her\Hers).txt'."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:71 char:1
+ $stream = [System.IO.StreamWriter] "$FolderLocation\\$strName.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastConstructorException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:72 char:1
+ $stream.WriteLine("$strName")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:74 char:1
+ $stream.WriteLine("$strTitle $strCompany | $strDepartment ")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:76 char:1
+ $stream.WriteLine("National Endowment for the Arts")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:78 char:1
+ $stream.WriteLine("400 7th Street SW | Washington DC 20506")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:80 char:1
+ $stream.WriteLine("$strEmail | 202-682-$strPhone (p) | $strFax")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:84 char:1
+ $stream.WriteLine("Learn more about the arts in your community at ar ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
Exception calling "WriteLine" with "1" argument(s): "Cannot write to a closed TextWriter."
At C:\Users\boldenc\OneDrive - National Endowment for the Arts\boldenc\Documents\Scripts - email signature_Login\nea_email_sig_with_logo_Pronouns.ps1:85 char:1
+ $stream.WriteLine("https://www.arts.gov/sites/default/files/2018-%20H ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
- LainRobertsonJun 10, 2022Silver Contributor
That makes sense.
The variable being used, $strName, is used in many places, some of which are filenames.
The problem then is that extensionAttribute2 contains characters like "/" which is what's causing many or all of those errors (I didn't read every single one.)
That's unfortunate, but we can work around that.
Firstly, remove the lines I have you before and put the script back to the way it was - just so we know we're working from a reliable starting point.
Step 1: Create a new variable just for the name including the pronouns.
About eight lines down, you will see these two lines:
$strName = $objUser.DisplayName $strTitle = $objUser.TitleChange these two lines to look like this:
$strName = $objUser.DisplayName if ($objUser.Properties.Contains("extensionAttribute2") -and -not [string]::IsNullOrWhiteSpace($objUser.extensionAttribute2[0])) { $strNameWithPronouns = "$($objUser.DisplayName) $($objUser.extensionAttribute2[0])"; } else { $strNameWithPronouns = $objUser.DisplayName; } $strTitle = $objUser.TitleStep 2: Use the new $strNameWithPronouns variable further down to set the Name.
Further down - around line 45, you will see this line:
$stream.WriteLine("<Table border =`'0`'><tr><td><B><SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT-FAMILY: `'Calibri`'`">" + $strName + "</B></SPAN></td></tr>")Change it to this, to make use of the new variable, "$strNameWithPronouns":
$stream.WriteLine("<Table border =`'0`'><tr><td><B><SPAN style=`"FONT-SIZE: 11pt; COLOR: black; FONT-FAMILY: `'Calibri`'`">" + $strNameWithPronouns + "</B></SPAN></td></tr>")Cheers,
Lain
- Christina_B5749Jun 10, 2022Copper ContributorThat worked! Thank you much.