1

I have an interesting issue that I am hoping the crowd can help with. A Powershell script that I am running on my jump box is completes without error. When I run it on the production server, it errors out with

At D:\Scripts\Groups\MS_Student_Group_Update.ps1:9 char:265
+ ... ountName, @{name=â€
+                                                                  ~
You must provide a value expression following the '-join' operator.

Both are MSServer 2016.

Here is the script

Function writeLog {
    Param ([string]$logstring)
    Add-content $Logfile -value $logstring
}

$date = get-date -f MMddyyyy
$Logfile = 'D:\Powershell\Logs\googleEmailGroup_AD_MS' + $date + '.log'

$ldapSearcher  = get-aduser -ldapfilter '(&(objectCategory=person)(objectClass=user)(description=*middle*)(cn=S*)(!userAccountControl:1.2.840.113556.1.4.803:=2))' -properties MemberOf |select name,SamAccountName, @{name=”MemberOf”;expression={$_.memberof -join “;”}}

#$test = $ldapSearcher | select -first 20

foreach($user in $ldapSearcher) {
    $ErrorOccurred = $false
    if($user.MemberOf -notlike "FCPS MS*") {
        $user_grp = 'FCPS MS Students All'
        try {
            Add-adgroupmember -Identity $user_grp -Members $user.SamAccountName
        }
        catch {
            writeLog "Failed to add $($user.SamAccountName) GoogleSyncGroup."
        }
        if(!$ErrorOccurred) {
            writeLog "Added_to_Google_Group: $($user.samaccountname)"
        }
    } }
  • acknowledging RSAT being installed/enabled, seems that the issue is that there is no return on the property of `memberof`. A simple `if` statement should fix that by providing an alternate return if null. `if ($_.memberof) { ... } else { "no members" }`. – Abraham Zinala Aug 01 '22 at 15:10
  • 1
    Could be a problem with the “-char not being interpreted as "-char. That might depend on your PowerShell version and language settings – An-dir Aug 01 '22 at 15:39
  • Since the problem occurs with a _string literal_ in your _source code_, the likeliest explanation is that your script file is misinterpreted by the Windows PowerShell engine, which happens if the script is saved as UTF-8 _without a BOM_. Try saving your script as UTF-8 _with BOM_; see the linked duplicate for details. – mklement0 Aug 01 '22 at 15:55
  • Specifically, you're using `“` (LEFT DOUBLE QUOTATION MARK, [`U+201C`](http://www.fileformat.info/info/unicode/char/201c)) instead of the usual ASCII-range double quote, and the former's UTF-8 encoding (3 bytes) is being misinterpreted as ANSI encoding. – mklement0 Aug 01 '22 at 15:57

0 Answers0