{"id":2599,"date":"2015-02-05T21:53:00","date_gmt":"2015-02-05T21:53:00","guid":{"rendered":"https:\/\/www.edwardramos.com\/?p=2599"},"modified":"2015-02-05T21:53:00","modified_gmt":"2015-02-05T21:53:00","slug":"inventory-permissions-with-powershell","status":"publish","type":"post","link":"https:\/\/edwardramos.com\/index.php\/2015\/02\/05\/inventory-permissions-with-powershell\/","title":{"rendered":"Inventory Permissions with Powershell"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Here is a script that will inventory all users and permissions by site collection in a sharepoint farm to a csv file and\/or a SharePoint list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue\n#update these variables as needed\n$scriptname = \"siteusers\"\n$FilePath = \"d:\\dashboard\\eer-siteusers.csv\"\n$docliburl= \"http:\/\/eer\/spd\/IT\/systems\/ws\/dashboard\/Lists\/siteusers\";\n\nfunction eer-$scriptname() {\n    &#91;void]&#91;System.Reflection.Assembly]::LoadWithPartialName(\"Microsoft.SharePoint\")\n    $farm = &#91;Microsoft.SharePoint.Administration.SPFarm]::Local\n    foreach ($spService in $farm.Services) {\n        if (!($spService -is &#91;Microsoft.SharePoint.Administration.SPWebService])) {\n            continue;\n        }\n        foreach ($webApp in $spService.WebApplications) {\n            if ($webApp -is &#91;Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }\n\n            foreach ($site in $webApp.Sites) {\n\nforeach ($web in $site.AllWebs) {\nif (($web.permissions -ne $null) -and ($web.hasuniqueroleassignments -eq \"True\")) {\nforeach  ($group in $web.Groups) {\nforeach ($user in $group.users) { \n$data = @{\n                                \"loginname\" = $user.loginname\n\t\t\t\t\t\t\t\t\"WebUrl\" = $web.url\n\t\t\t\t\t\t\t\t\"GroupPermission\" = $group.permission\n\t\t\t\t\t\t\t\t\"UserGroup\" = $group\n\t\t\t\t\t\t\t\t\"SiteOwner\" = $web.description\n\t\t\t\t\t\t\t\t\"SiteName\" = $web.title\n\t\t\t\t\t\t\t   }\n                            New-Object PSObject -Property $data\n     }}\n    }\n\t}\n   }\n } \n $web.Dispose()\n}\n$site.Dispose()\n}    \t\n\n#eer-$scriptname | Out-GridView\neer-$scriptname | Export-Csv -Path $FilePath -NoTypeInformation\n\n$site=new-object Microsoft.SharePoint.SPSite($docliburl);\n$web=$site.openweb();\n$list=$web.GetList($docliburl);\n\n###delete old items\n\n$collListItems = $List.Items; \n$count = $collListItems.Count - 1 \nfor($intIndex = $count; $intIndex -gt -1; $intIndex--) \n{ \n        $collListItems.Delete($intIndex); \n} \n\n###\n\n$csv_file = Import-Csv $FilePath;\nforeach ($line in $csv_file)\n{ \nWrite-Output $line.Title;\n  $item = $list.Items.Add();\n  $item&#91;\"Site\"] = $line.WebURL;\n  $item&#91;\"Member\"] = $line.LoginName;\n  $item&#91;\"Perms\"] = $line.GroupPermission;\n  $item&#91;\"Group\"] = $line.UserGroup;\n  $item&#91;\"Site Owner\"] = $line.SiteOwner;\n  $item&#91;\"Title\"] = $line.SiteName;\n  $item.Update();\n}\n\n\n\n\nAdd-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue\n#update these variables as needed\n$scriptname = \"siteusers\"\n$FilePath = \"d:\\dashboard\\eer-siteusers.csv\"\n$docliburl= \"http:\/\/eer\/spd\/IT\/systems\/ws\/dashboard\/Lists\/siteusers\";\n\nfunction eer-$scriptname() {\n    &#91;void]&#91;System.Reflection.Assembly]::LoadWithPartialName(\"Microsoft.SharePoint\")\n    $farm = &#91;Microsoft.SharePoint.Administration.SPFarm]::Local\n    foreach ($spService in $farm.Services) {\n        if (!($spService -is &#91;Microsoft.SharePoint.Administration.SPWebService])) {\n            continue;\n        }\n        foreach ($webApp in $spService.WebApplications) {\n            if ($webApp -is &#91;Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }\n\n            foreach ($site in $webApp.Sites) {\n\nforeach ($web in $site.AllWebs) {\nif (($web.permissions -ne $null) -and ($web.hasuniqueroleassignments -eq \"True\")) {\n\nforeach ($user in $web.users) { \n$data = @{\n                                \"loginname\" = $user.loginname\n\t\t\t\t\t\t\t\t\"WebUrl\" = $web.url\n\t\t\t\t\t\t\t\t\"GroupPermission\" = $user.permission\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\"SiteOwner\" = $web.description\n\t\t\t\t\t\t\t\t\"SiteName\" = $web.title\n\t\t\t\t\t\t\t   }\n                            New-Object PSObject -Property $data\n     }\n    }\n\t}\n   }\n } \n $web.Dispose()\n}\n$site.Dispose()\n}    \t\n\n#eer-$scriptname | Out-GridView\neer-$scriptname | Export-Csv -Path $FilePath -NoTypeInformation\n\n$site=new-object Microsoft.SharePoint.SPSite($docliburl);\n$web=$site.openweb();\n$list=$web.GetList($docliburl);\n\n\n\n$csv_file = Import-Csv $FilePath;\nforeach ($line in $csv_file)\n{ \nWrite-Output $line.Title;\n  $item = $list.Items.Add();\n  $item&#91;\"Site\"] = $line.WebURL;\n  $item&#91;\"Member\"] = $line.LoginName;\n  $item&#91;\"Perms\"] = $line.GroupPermission;\n  $item&#91;\"Site Owner\"] = $line.SiteOwner;\n  $item&#91;\"Title\"] = $line.SiteName;\n  $item.Update();\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a script that will inventory all users and permissions by site collection in a sharepoint farm to a csv file and\/or a SharePoint list.<\/p>\n","protected":false},"author":1,"featured_media":2600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10],"tags":[70,86],"class_list":["post-2599","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-my-work","category-tech","tag-microsoft","tag-powershell"],"_links":{"self":[{"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/posts\/2599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/comments?post=2599"}],"version-history":[{"count":0,"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/posts\/2599\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/media\/2600"}],"wp:attachment":[{"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/media?parent=2599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/categories?post=2599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edwardramos.com\/index.php\/wp-json\/wp\/v2\/tags?post=2599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}