Executing Scripts on SharePoint Management Shell

 
Good afternoon everyone,

I'm looking for a script to apply permissions to folders inside Sharepoint, but I'm not even able to run tests because powershell does not run some scripts, the main error is when I execute any command like this: "$ site = new-object Microsoft.SharePoint.SPSite (" https://contoso.sharepoint.com/ ")", Shell brings me:
new-object: Can not find the type [Microsoft.SharePoint.SPSite]: Make sure the assembly containing the type is loaded.
 
I can't import modules, and can't add Snapin, when I run: "Add-PSSnapin Microsoft.SharePoint.PowerShell", Shell brings me:
 
Add-PSSnapin: No snap-ins were registered for Windows PowerShell version 5.
 
That is, I can not do anything on account of not loading some assembly modules.
 
Could someone give me a help regarding this please?
 
Thanks in advance.
  • Your request is a bit incomplete as you do not say, where you are trying to do this from. Meaning are you doing this from your own workstation or directly on the SharePoint server, if PowerShell Remoting is enabled and what your permissions are.

    PowerShell scripts will always run if...

    1 - PowerShell / PowerShell Remoting are enabled and the ExecutionPolicy is properly set.
    2 - You have the permissions to run PowerShell scripts locally and / or remotely
    3 - You have all the modules loaded, locally or remotely either via directly install or explicit or implicit PowerShell remoting.

    Otherwise you can only use command interactively.

    You can only use ...

    Add-PSSnapin -Name *SharePoint* # the shortcut way of using the snap-in

    ...If you are directly on the SharePoint server physically using a Remote Desktop Session or in an Explicit / Implicit PowerShell Remoting session.

    NOTE:
    You also must be an administrator to use PowerShell Remoting or use custom configuration for a non-administrator.

    # Explicit PowerShell Remoting Session

    PS C:\Scripts> Enter-PSSession -ComputerName 'sp01'
    [sp01]: PS C:\Users\administrator.contoso\Documents> Add-PSSnapin -Name *SharePoint*
    [sp01]: PS C:\Users\administrator.contoso\Documents> Get-Command

    CommandType Name ModuleName
    ----------- ---- ----------
    ...
    Cmdlet Update-SPAppCatalogConfiguration Microsoft.SharePoint.PowerShell
    Cmdlet Update-SPAppInstance Microsoft.SharePoint.PowerShell
    Cmdlet Update-SPDistributedCacheSize Microsoft.SharePoint.PowerShell

    ...

    When you are done exit the session

    [sp01]: PS C:\Users\administrator.contoso\Documents> Remove-PSSnapin -Name *SharePoint*
    [sp01]: PS C:\Users\administrator.contoso\Documents> Exit-PSSession

    The same is true of Lync/Skype and Exchange. Well, You could install the UCMA+OCS on your local workstation tp get the lynch / Skype cmdlets as well as you can install the Exchange EMC on you local workstation, as long as you have IIS installed.

    If you just want to use the cmdlets from Exchange, SharePoint, Lync/Skype from local workstation and your have PowerShell Remoting enabled on the Exchange, Lync/Skype, SharePoint servers.

    Details of Implicit Remoting

    'blogs.technet.microsoft.com/heyscriptingguy/2013/09/08/remoting-the-implicit-way'
    'technet.microsoft.com/en-us/library/ff720181.aspx'

    How to enable Remote PowerShell for SharePoint 2013 for Non-Administrators
    'blogs.technet.microsoft.com/anneste/2014/09/17/how-to-enable-remote-powershell-for-sharepoint-2013-for-non-administrators'


    For MS Cloud services, see...

    Connect to Exchange Online PowerShell
    'technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx'

    Office 365 PowerShell for SharePoint Online
    'technet.microsoft.com/en-us/library/fp161362.aspx'
    'blogs.technet.microsoft.com/.../manage-office-365-sharepoint-with-modules'

    Connecting to Lync/Skype for Business Online by using Windows PowerShell
    'technet.microsoft.com/en-us/library/dn362795(v=ocs.15).aspx'

    If you are in the PowerShell ISE, you have to remember to click the 'Refresh' button on the 'Commands' tab to see the added cmdlets. You can use Implicit Remote session with module import to current editor PowerShell ISE, PowerShell Console Host or VSCode.

    Understand the security ramifications of some remote sessions, and specifically SharePoint.

    'powershellmagazine.com/2014/03/06/accidental-sabotage-beware-of-credssp'
    'blogs.technet.microsoft.com/heyscriptingguy/2012/11/14/enable-powershell-second-hop-functionality-with-credssp'
    'msdn.microsoft.com/en-us/library/ee309365(v=vs.85).aspx'
    'blogs.msdn.microsoft.com/besidethepoint/2010/05/26/powershell-remoting-for-sharepoint'

    Do this first to enable SharePoint remoting
    Again see the above and limit CredSSP only to the servers that you ned it for

    SharePoint PowerShell Remoting requires the below. Tailor to your risk needs.
    #>

    # On the SharePoint Server
    Enable-PSRemoting -Force Enable-WSManCredSSP -Role Server -Force
    Enable-WSManCredSSP -Role Server -Force


    # On your management
    # Client (this has to be done once per unique managment client)
    Enable-WSManCredSSP -Role Client -DelegateComputer * -Force
    Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentials -Name WSMan -Value WSMAN/*
    Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentialsDomain -Name WSMan -Value WSMAN/*
  • In reply to postanote:

    I'm using Sharepoint Online (Office 365), I don't have SharePoint Server).
    I read yours link, but I couldn't resolve the problem yet :(
  • In reply to Daniel Felix:

    Understood.
    But as per the links. Since, you are on SPO, then there is stuff you have to download and install first.

    Microsoft Online Services Sign-In Assistant for IT Professionals RTW
    'microsoft.com/en-us/download/details.aspx?id=28177'

    Connect to SharePoint Online PowerShell
    'technet.microsoft.com/en-us/library/fp161372.aspx'

    Install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. 
    'go.microsoft.com/fwlink/p/?LinkId=255251'
    You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.

    To open the SharePoint Online Management Shell command prompt, from the Start screen, type sharepoint, and then click SharePoint Online Management Shell.

    To connect with a user name and password

    1. Fill in the values for the $adminUPN and $orgName variables (replacing all the text between the quotes, including the < and > characters), and then run the following commands at the SharePoint Online Management Shell command prompt:

    $adminUPN="<the full email address of a SharePoint Online global administrator account, example: jdoe@contosotoycompany.onmicrosoft.com>"
    $orgName="<name of your Office 365 organization, example: contosotoycompany>"
    $userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."
    Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential

    2. When prompted with the Windows PowerShell credential request dialog box, type the password for the SharePoint Online global administrator account.

    Introduction to the SharePoint Online Management Shell
    'support.office.com/en-us/article/Introduction-to-the-SharePoint-Online-Management-Shell-C16941C3-19B4-4710-8056-34C034493429'

  • In reply to postanote:

    I already did everything that you said, but I'm still with the problem. So, I figure out that this kind of command that I'm using is not properly for SPO, now my problem is... How can I do it (apply permissions in existing folders with existing security groups).
  • In reply to Daniel Felix:

    I do not have a SPO site set up with anything configured to test against in my current O365 instance. Though of course SPO is there by default.

    Yet, remember, as I stated in my earlier post. You cannot use Add-PSSnapin unless you are physically on the SharePoint server. The same is true of Exchange, Lync / Skype.

    For MSOLine, there is no server for you to land on, only services. So, there is no Add-PSSnapin option.


    ///////// SharePoint Online PowerShell Environment Setup /////////

    as an Administrator on your workstation.

    Install the MS Online Sign in Assistance.
    Install the SPO Module.

    In the PowerShell ISE... Load the cmdlets

    Import-Module -Name 'MSOnline'
    Import-Module -Name 'Microsoft.Online.SharePoint.PowerShell' -DisableNameChecking

    Connect to SPO

    Connect-MsolService -Credential 'YourMsoCreds'
    Connect-SPOService -Url 'YourMsoSpoAdminUrl' -Credential 'YourMsoCreds'

    Click the Refresh button on the PowerShell ISE Commands tab.

    Get-Command -Module 'Microsoft.Online.SharePoint.PowerShell'
    Get-Command -Name '*SharePoint*

    ///////// SharePoint Online PowerShell Environment Setup /////////

    This is the online details of the cmdlets for SPO.

    'technet.microsoft.com/en-us/library/fp161388.aspx'
    'technet.microsoft.com/en-us/library/fp161364.aspx'

    To see some of this in action, you can visit the MS Channal9 site and view the video on the topic here:
    'channel9.msdn.com' the Video Channel.

    Just search for...

    Learn best practices for managing and administering SharePoint Online and OneDrive for Business

    Or just search for...

    SharePoint Online PowerShell
    'channel9.msdn.com/Search?term=sharepoint%20online%20powershell#pubDate=year&ch9Search&lang-en=en'

    Or just search for...

    SharePoint Online PowerShell
    'channel9.msdn.com/Search?term=sharepoint%20online%20powershell#pubDate=year&ch9Search&lang-en=en'

    The speaker shows how to connect / manage SPO with PoSH starting at time marker 18:57.
    The speaker, provides more points regarding such efforts in his blog.
    Learn Best Practices for Managing and Administering SharePoint Online and OneDrive for Business Ignite Session Links and Resources
    'blogs.technet.microsoft.com/cbortlik/2016/09/14/learn-best-practices-for-managing-and-administering-sharepoint-online-and-onedrive-for-business-ignite-session-links-and-resources'




    As for your new specific query. There is a script on the MS PowerShell Gallery you can tweak for your use case.


    SharePoint online PowerShell script to create folders and assign permissions
    'gallery.technet.microsoft.com/office/SharePoint-online-57f24eca'

    There is also a module for SharePontOnline in the PowerShell Galler you can leverage.
    'powershellgallery.com/packages/SharePointPnPPowerShellOnline/2.4.1605.0'

    To do this to donwload, install and load up for use.

    Using the PowerShell ISE or PowerShell Console host, execute, the below to review the module
    (if you are on PoSH v5 or higher, or PoSH v4 with PowerShellGet installed)



    //////// SharePointPnPPowerShellOnline Setup ////////

    Find-Module -Name SharePointPnPPowerShellOnline | Format-List

    If ((Test-Path -Path $profile) -eq $false )
    {New-item –Type file –Force $profile}
    Else {Write-Warning -Message "User $env:USERNAME PowerShell profile already exists."}

    Save-Module -Name SharePointPnPPowerShellOnline -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules" -Force
    Install-Module -Name SharePointPnPPowerShellOnline -Force
    Import-Module -Name SharePointPnPPowerShellOnline

    Click the Refresh button on the PowerShell ISE Commands tab.

    //////// SharePointPnPPowerShellOnline Setup ////////



    Take a look at the SharePointPnPPowerShellOnline Get and Set permissions cmdlets.

    If you want to go deeper, you can also to look to PowerShell + CSOM (SharePoint Client Object model.)
    'dev.office.com/sharepoint/docs/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code'
  • In reply to postanote:

    One more reinforcement, I meant to state.

    As you will see from the available SPO cmdlets (the MS links previously mentioned), they are not as complete as SP Server proper. That will change over time.

    So, that is the reason I point you to the CSOM (SharePoint Client Object model). So, the moment you try to do with SPO what you can do with SP proper, you have to get creative and really understand and how to use CSOM.

    Also of note: to do anything substantial in SPO, you have to be a SPO Global Admin.