none
add items to a list with remote powershell

    Question

  • Hi,

    I'm trying to add an item to a calendar list remotely from powershell. I can do it fine directly from the server by just removing the first 5 lines in the code. The userinput in get-credential has the correct permissions however when I analyze the code it seems that the $list variable is null and therefore it doesnt work. However any ideas on how I can fix this?

    Thanks

    $c = Get-Credential \
    $session = New-PSSession <Sharepointserver>-Authentication CredSSP -Credential $c
    
    #Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
    Invoke-Command -Session $session -ScriptBlock{Add-PsSnapin Microsoft.SharePoint.PowerShell} 
    
    
    # Variables that we are going to use for list editing
    $webURL = "http://sharepointserver/sites/somesite"
    $listName = "Somecalendar" 
    
    #Get the SPWeb object and save it to a variable
    
    $web = Invoke-Command -Session $session -ScriptBlock{Get-SPWeb $webURL}  
    
    #Get the SPList object to retrieve the "Somecalendar"
    $list = $web.Lists[$listName]
    
    #Create a new item
    $newItem = $list.Items.Add() 
    
    #Add properties to this list item
    $newItem["Title"] = "My first appointment" 
    $newItem["Description"] = "This is a multiple lines of text field." 
    
    #Update the object so it gets saved to the list
    $newItem.Update()


    • Edited by Jan Knudsen Friday, January 04, 2013 2:02 PM
    Friday, January 04, 2013 2:00 PM

All replies

  • Anyone?

    I've just tried to make it more "simple" with a few screenshots. The first one works locally from the server:

    The second is from my remote computer and here I dont get any values back when I ask for $web.title or $list.title. The username/password I use is the same both places.

    Thanks

    Monday, January 07, 2013 12:44 PM
  • Anyone?

    I've just tried to make it more "simple" with a few screenshots. The first one works locally from the server:

    The second is from my remote computer and here I dont get any values back when I ask for $web.title or $list.title. The username/password I use is the same both places.

    Thanks

    Monday, January 07, 2013 12:44 PM
  • Did you configure your server properly for remote access? Here's some things I run to get my server setup for it:

    Enable-PsRemoting -Confirm:$false
    Enable-WSmanCredSSP -Role Server -Force
    
    Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024
    Set-Item WSMan:\localhost\shell\MaxShellsPerUser 2
    Set-Item WSMan:\localhost\shell\MaxConcurrentUsers 2
    
    Register-PSSessionConfiguration `
        -Name "SharePoint" `
        -StartupScript "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\CONFIG\POWERSHELL\Registration\sharepoint.ps1" `
        -Force `
        -ThreadOptions ReuseThread
    Set-PSSessionConfiguration -Name "SharePoint" -ShowSecurityDescriptorUI -Force

    You also need to make sure your client is setup to support CredSSP:

    Enable-PsRemoting -Confirm:$false
    Enable-WSmanCredSSP -Role Client -DelegateComputer * -Force

    When you call Enter-PSSession provide the -ConfigurationName parameter with "SharePoint" (or whatever you used for the Register-PSSessionConfiguration cmdlet) - this is necessary to make sure that the snapin and threading model is set properly when working with SharePoint (it also saves you from having to explicitly load the snapin every time you connect).


    Gary Lapointe, Blog: http://blog.falchionconsulting.com/, Twitter: http://twitter.com/glapointe Aptillon Inc.: http://www.aptillon.com

    Thursday, January 29, 2015 10:22 PM
  • Hi Gary,

    Thank you for providing me with this useful information about how to enable ps. Tried to follow your procedure and it does enable psremoting correctly however the result is still the same as in my last screenshot = no values returned.

    Eventhough my post is 2 years old I'm still interested in learning what needs to be configured differently.

    Thank you

    Friday, January 30, 2015 9:15 AM
  • That's funny, I didn't even notice your post was 2 years old, stumbled on it while looking for something else and saw it was unanswered and totally overlooked the date :)

    It's very odd that it's not working for you as I've configured dozens of environments like this and have never had an issue like what you describe. I'd be curious to know if any odd ULS entries are logged when you call Get-SPWeb (I'm guessing that the $web variable itself is actually coming back $null). Feels almost like some odd permissions issue (like the credentials aren't being passed correctly or something) but that doesn't really make sense either as you'd get some sort of error returned. I wonder if you tried the same steps with a different farm if you get the same results.


    Gary Lapointe, Blog: http://blog.falchionconsulting.com/, Twitter: http://twitter.com/glapointe Aptillon Inc.: http://www.aptillon.com

    Saturday, January 31, 2015 5:51 PM
  • I have the same issue with remote PowerShell and a list.  The list always is null.   Runs great when on the SharePoint server but does not from another server. 

    I have not yet found a work around

    $list=$web.Lists["Phone Log"]

    or

    $list=$web.GetList["Phone Log"]

    Return a blank from the remote server.

     
    Sunday, March 29, 2015 3:54 PM