Bassie
Bassie

Reputation: 10390

Calling Script Remotely Does Not Work

I can run this script perfectly on my SharePoint server, and the user's profile picture gets updated:

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")

$siteurl = "http://SHAREPOINTSITE/"
try {
    $site = New-Object Microsoft.SharePoint.SPSite($siteurl)
} catch { 
    New-Item -ItemType File -Path C:\Users\admin\Desktop -Name ERROR1.txt -Value $_.Exception.Message -Force
}
try {
    $context = [Microsoft.Office.Server.ServerContext]::GetContext($site)
} catch { 
    New-Item -ItemType File -Path C:\Users\admin\Desktop -Name ERROR2.txt -Value $_.Exception.Message -Force
}

#This gets the User Profile Manager which is what we want to get hold of the users
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$user = "DOMAIN\user.name"

#Put it in a loop for iterating for all users
if ($upm.UserExists($user)) {
    try {
        $profile = $upm.GetUserProfile($user)
        $profile["PictureURL"].Value = "\\Sharepoint\C$\Users\admin\Desktop\1.jpg";
        $profile.Commit();
    } catch {
        New-Item -ItemType File -Path C:\Users\admin\Desktop -Name ERROR3.txt -Value $_.Exception.Message -Force
    }
}

New-Item -ItemType File -Path C:\Users\admin\Desktop -Name HELLO.txt -Force

$site.Dispose()

But when I run it from a remote PowerShell session, I am getting some weird errors:

ERROR1.txt

Exception calling ".ctor" with "1" argument(s): "The Web application at http://SHAREPOINTSITE/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application."

ERROR2.txt

Multiple ambiguous overloads found for "GetContext" and the argument count: "1".

I have checked all of the possibilities here, but still seeing this issue.

This is how I call the above script from the remote machine:

$spfarm = "DOMAIN\admin.username"
$spfarmpw = ConvertTo-SecureString "password123" -AsPlainText -Force

$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $spfarm,$spfarmpw
$session = New-PSSession SharePoint -Authentication Default -Credential $cred

Invoke-Command -Session $session -FilePath "\\SharePoint\C$\Users\admin\Desktop\testremote.ps1"

I have tried calling this in a few different ways (e.g. hosting the script on my machine or hosting it on the SharePoint server, as well as using relative paths to call the script), but I always see these errors.

Can anyone please help me understand why this doesn't work when calling it from a remote PC? The script is clearly being called (HELLO.txt always gets created), but the SharePoint profile picture never gets updated - even though that script definitely should work.

Any help or guidance is much appreciated


nslookup

nslookup SHAREPOINTSITE

Output

Server:    dc1.domain.co.uk
Address:   xx.xx.x.xx

Name:      sharepoint.domain.co.uk
Address:   yy.yy.y.yy
Aliases:   SHAREPOINTSITE.domain.co.uk 

Where yy.yy.y.yy is the correct IP (it's the same address I see when executing ping SHAREPOINTSITE)

Upvotes: 0

Views: 498

Answers (1)

David McEwing
David McEwing

Reputation: 3340

Try changing the Authentication method to CredSSP. This is required by the remote PowerShell so that it can pass the credentials on.

Upvotes: 1

Related Questions