0

I am getting an error when trying to run a powershell script. I copied this script from a site. It should take fields from the user information list which is hidden and populate a contact list that you have created an advance. I am not a powershell user so I copied the script as is and tried to run it from powershell by typing .\CreateEmployeeDirectory

I am getting the following error:

At C:\Users\me\CreateEmployeeDirectory.ps1:19 char:162
+ ... ist $webUrl } |
+                    ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : EmptyPipeElement

Here is the script:

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
function Enumerate-SPUserProfiles($webUrl, $listName)
{
    $x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $x= [System.Reflection.Assembly]::LoadWithPartialName("microsoft.sharepoint.portal")
    $x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
    $x= [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
    $sites = Get-SPSite
    $context = [Microsoft.Office.Server.ServerContext]::GetContext($sites[0])
    $profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

    $web = Get-SPWeb $webUrl
    $list = $web.Lists[$listName]

    # gets all the user profiles, those that have domain\username as DisplayName are excluded
    $profileManager.GetEnumerator() | ?{$_.DisplayName -notlike '*\*' -and $_.ProfileType -eq "User" } | foreach-object { Update-PhoneBook $_ $web $list $webUrl } |
}

function Update-PhoneBook($user, $web, $list, $webUrl)
{
    if($user.Item("FirstName") -ne "" -and $user.Item("FirstName") -ne $null)
    {
        $spQuery = new-object Microsoft.SharePoint.SPQuery
        $userName = $user.Item("UserName")
        $camlQuery = "<Where><Eq><FieldRef Name='UserName' /><Value Type='Text'>$userName</Value></Eq></Where>";
        $spQuery.Query = $camlQuery
        $spQuery.RowLimit = 1
        $spListItemCollection = $list.GetItems($spQuery)
        $domain = (gwmi WIN32_ComputerSystem).Domain

        if($spListItemCollection.Count -ge 1)
        {
            Write-Host "Updating $username..."
            Update-Contact $spListItemCollection[0] $user $domain $webUrl
        }
        else
        {
            Write-Host "Adding $username..."
            $contact = $list.items.add();
            Update-Contact $contact $user $domain $webUrl
        }
    }
}

function Update-Contact($contact, $user, $domain, $webUrl)
{
    $lastName = $user.Item("LastName");
    $contact["Title"] = "$lastName";

    $firstName = $user.Item("FirstName");
    $contact["FirstName"] = "$firstName";

    $workEmail = $user.Item("WorkEmail");
    $contact["Email"] = "$workEmail";

    $workPhone = $user.Item("WorkPhone");
    $contact["WorkPhone"] = "$workPhone";

    $CellPhone = $user.Item("CellPhone");
    $contact["CellPhone"] = "$cellPhone";

    $office = $user.Item("Office");
    $contact["Office"] = "$office";

    $department = $user.Item("Department");
    $contact["Department"] = "$department";

    $userName = $user.Item("UserName");
    $contact["UserName"] = "$userName";

    $contact.update()
}
1

According to the error message, this line is wrong:

$profileManager.GetEnumerator() | ?{$_.DisplayName -notlike '*\*' -and $_.ProfileType -eq "User" } | foreach-object { Update-PhoneBook $_ $web $list $webUrl } |

Please try replacing it with this one here (note that the last |-character is now gone):

$profileManager.GetEnumerator() | ?{$_.DisplayName -notlike '*\*' -and $_.ProfileType -eq "User" } | foreach-object { Update-PhoneBook $_ $web $list $webUrl }

  • Thanks Daniel that allowed it to run but now I have a different issue. I was expecting it to ask for the two values that are in the Enumerator but when I run it it just executes without asking for input. Should I be including the input value some way when I issue the run command of .\CreateEmployeeDirectory. Also I got this message once it did finish: The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered. Should I be concerned with that message? – MikeZ Sep 27 '16 at 15:45
  • Without rewriting the script, you will have to call the function directly ( e.g. 'Enumerate-SPUserProfiles "your_site_url" "List_Name"'). Yes, you should be concerned about that message. It can have a few reasons, though, so you might want to ask about it in a separate question. – Daniel Stölzner Sep 27 '16 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.