none
SharePoint Script / Remoting RRS feed

  • Question

  • Hi,

    I realise that I might have the incorrect forum but I wasn't sure which forum would be a better fit.

    I have been trying to create a script to query a SharePoint 2013 server to get a list of SharePoint groups that a user is a member of, please see below;

    Invoke-Command -ComputerName COMPNAME -Authentication Credssp -Credential "DOMAIN\sharepoint1" -ScriptBlock {Add-PSSnapin "Microsoft.SharePoint.PowerShell"; $user = get-SPUser -limit all -web http://COMPNAME | where {$_.loginname -like "*|sanderson\USERNAME"}; $SPGroups = get-spsite -limit all | select -ExpandProperty rootweb | select -ExpandProperty siteusers | where { $user.userlogin -eq $_.loginname } | select -ExpandProperty groups | select name, {$_.parentweb.url}; Write-Host $SPGroups}

    I know one liners are awful and a gimmick before you ask. When I run the command I'm not getting anything back, that includes not getting an error so I'm either getting nothing back or I'm not outputting it to my console properly. Any help is appreciated.

    Feel free to relocate me to a more appropriate forum for the question like I said earlier :)

    Tuesday, August 22, 2017 12:51 PM

Answers

  • What you have is not a one-liner but is a bunch of code all jammed onto one line.  This servers no purpose and makes the code impossible to understand or to debug.

    There is really no such thing as a "one-liner"  That term was used to refer to a "pipeline" which does not have to be written all in one line.

    Here is the correct way to format the script.

    $sb = { 
    	Add-PSSnapin Microsoft.SharePoint.PowerShell
    	$user = get-SPUser -limit all -web http://COMPNAME | 
    		Where-Object { $_.loginname -like "*|sanderson\USERNAME" }
    		$SPGroups = get-spsite -limit all | 
    			Select-Object -ExpandProperty rootweb | 
    			Select-Object -ExpandProperty siteusers | 
    			Where-Object { $user.userlogin -eq $_.loginname } | 
    			Select-Object -ExpandProperty groups | 
    			Select-Object name, { $_.parentweb.url }
    		Write-Host $SPGroups 
    }
    Invoke-Command -ScriptBlock $sb -ComputerName COMPNAME -Authentication Credssp -Credential DOMAIN\sharepoint1
    

    You can now see the obvious coding mistakes.  Try testing the code one  small piece at a time until you understand how it works.


    \_(ツ)_/

    Tuesday, August 22, 2017 1:54 PM