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
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.Title
Change 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.Title
Step 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