0

I have a PowerShell script below:

    Add-PSSnapin Microsoft.SharePoint.PowerShell

    function Get-DocInventory([string]$siteUrl) {

    $web = Get-SPWeb "http://contoso.com/sites/Test10"

    foreach ($list in $web.Lists) {

    if($excludeLists -notcontains $list.Title){

    foreach ($item in $list.Items) {

    foreach($version in $item.Versions){



    $personField = $item.Fields.GetField("Author");

    $authorObject = $personField.GetFieldValue($item["Author"]);

    $authorName = $authorObject.LookupValue;



    $userField = $version.Fields.GetField("Editor");

    $editorObject = $userField.GetFieldValue($version["Editor"]);

    $editorName = $editorObject.LookupValue;



    $localOffset = +5;

    $modified = $version["Modified"] -as [datetime];

    if($modified.IsDaylightSavingTime()){$localOffset += 1;}

    $modifiedLocal = $modified.addHours(-$localOffset);



    $data = @{

    "Version" = $version.VersionLabel

    "List Name" = $list.Title

    "Created By" = $authorName

    "Created Date" = $item["Created"]

    "Modified By" = $editorName

    "Modified Date" = ($modifiedLocal -as
    [datetime]).DateTime

    "Item Name" = $item.Name

    }

    New-Object PSObject -Property $data | Select "List Name", "Item Name",
    "Version", "Created By", "Created Date", "Modified By", "Modified Date"

    }

    }

    $web.Dispose();

    }

    }

    }
Get-DocInventory | Expport-Csv -NoTypeInformation -Path C:\TestOutput.csv

My problem is that when I run the script I get an error

"Cannot find an SPSite object that contains the following Id or Url: http://contoso.com/sites/Test10".

It doesn't make sense because when I use a different Url like http://contoso/sites/Departments/FRP the script works.

I have tried these variations:

  • $web = Get-SPWebApplication "http://contoso.com/sites/Test10"
  • $web = Get-SPSite "http://contoso.com/sites/Test10"
  • $web = Get-SPWeb "http://contoso.com/sites/Test10"

But I still get the same error mentioned previously. Can someone tell me what is wrong with the script?

3
  • Are you sure the urls are working
    – Unnie
    Commented Nov 10, 2015 at 19:52
  • contoso.com/sites/Test10 is a SPSite, not a SPWeb, contoso/sites/Departments/FRP is a SPWeb Commented Nov 10, 2015 at 19:53
  • Yes the Urls are working. In regards to what you mentioned Eric, if that was an SPSite then it should have worked if I used Get-SPSite which is in one of my variations right?
    – Nina G
    Commented Nov 10, 2015 at 20:21

2 Answers 2

0

/sites/test10 is both an SPSite and a SPWeb but not a WebApplication.

 $site =get-spsite http://contesso.com/sites/test10
  $web = $site.rootweb
  .........code here
  $site.dispose()

If get-spsite does not work in powershell but the url is successful for you in the browser it will be a permissions issue

1
  • I did what you recommended and I am still getting the same error. As for permissions I have Farm Admin and should pose no problems.
    – Nina G
    Commented Nov 10, 2015 at 22:35
0

The issue is that http://contoso.com/sites/Test10 is a site collection. You'd need to do something like this (untested, from memory):

function Get-DocInventory([string]$siteUrl) {
  $site = Get-SPSite $siteUrl
  $web = $site.RootWeb

  foreach ($list in $web.Lists) {
...
3
  • My question is, how can the script point to contoso.com/sites/Test10? In other words how will I know which site, site collection, web app does it point to if I'm not specifying a Url? Please advise.
    – Nina G
    Commented Nov 10, 2015 at 20:23
  • You have to be specific in what you want to pass into your function. If you want it to be a web and you are passing in a site, then you need logic to account for it. Commented Nov 10, 2015 at 20:42
  • I do understand that but in your previous statement you mentioned that the Url was an SPSite and not an SPWeb. When I used Get-SPSite it still returned the same error is what I am getting at. Was that what you were referring to?
    – Nina G
    Commented Nov 10, 2015 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.