7

I am an nube to Powershell, so I think I have a pretty basic question here....

How do I determine if a SPWeb exists? I want to perform some operations on a site based on a URL passed in as a parameter. However, there are instances where the site may not exist. I have tried something like

$test = Get-SPWeb($siteToGet)
if ($test -eq  $null ) {
    Write-Host Site $siteToGet  does not exist
}
else {
    Write-Host Site $siteToGet  DOES exist
}

where $siteToGet is not a valid site. However, I get an error message on the Get-SPWeb saying in red saying the site does not exist. The rest of the code then runs. Is this okay? I would prefer to log a message instead of display the error message from Get-SPWeb. It also causes more error messages as my script continues to run, but by checking for $null I can at least make sure I am not executing further code.

Any ideas how to check for the existence of a site using PowerShell and SharePoint 2010?

Thanks

John Ptacek
  • 228
  • 1
  • 2
  • 7

2 Answers2

9

Rob, if you check for a site that doesnt exist you will get errors with your approach.

Get-SPWeb "dasdasdasg" # this will throw a non-terminating error

To get past that, you can use several approaches. One is adding the common parameter -ErrorAction (alias is EA) and ErrorVariable (EV).

An example is actually when loading the SharePoint module:

Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue # -EA 0 works also

Use EA carefully, as this swallows your exceptions. Unless you know exactly what exception you are handling, use EA in combination with EV:

$site = Get-SPWeb $siteUrl -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection

if ($err) { #do something with error like Write-Error/Write-Warning or maybe just create a new web with New-SPWeb } else { #do something else }

Note that you can also add to error variable using +err and iterate error array.

Also note the AssignmentCollection. You should read up on that when you use object that need to be disposed, like SPSite and SPWeb:

 Get-Help Start-SPAssignment -full

Another option is using the Trap construct or try/catch/finally (PS V2 only)

More reading: -ErrorAction and -ErrorVariable

Trap in powershell

Glorfindel
  • 1,050
  • 1
  • 11
  • 17
Anders Rask
  • 17,949
  • 3
  • 38
  • 71
  • Thansk Anders, this is what I was looking for. Is the err flag something that is available for all cmdlets? – John Ptacek Aug 10 '10 at 11:18
  • 1
    yes along with Confirm, WhatIf and other. See more by typing "get-help about_commonparameters" – Anders Rask Aug 10 '10 at 12:42
  • Anders-I tried my code on a site that didn't exist and I got no errors. I got the expected "site does not exist". Wonder if something is different between our environments??? – Rob Wilson Aug 10 '10 at 13:48
  • Nevermind. I see what I did wrong. Thanks for clearing it up, Anders. – Rob Wilson Aug 10 '10 at 16:13
1

When I tried your code I also got a red error, but it seemed to indicate that it couldn't find the Get-SPWeb command -- which in turn caused the test to equal null. I wonder if by chance you didn't have the SharePoint snap-in loaded. The first block of code below ensures that the snap-in is loaded.

The PowerShell ISE works great for testing this script interactively if you aren't using it already. When I execute the following script, all of the tests show that the site or sub-site exists:


# check to ensure Microsoft.SharePoint.PowerShell is loaded if not using the SharePoint Management Shell 
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) {
    Write-Host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell" 
}

# check for the existence of a site collection 
$test = Get-SPWeb("http://pcrob7") 
if ($test -eq  $null ) {
    Write-Host Site Collection $siteToGet does not exist 
} 
else {
    Write-Host Site $siteToGet DOES exist 
}

# check for existence of a site 
$SiteUrl = "http://pcrob7" 
$targetUrl = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl} 
if ($targetUrl -ne $null) {
    Write-Host "Site" $SiteUrl "exists!" } else{
    Write-Host "Site" $SiteUrl "does not exist." 
}

# check for existence of a sub-site 
$SiteUrl = "http://pcrob7/robsubsite" 
if ($SiteUrl -ne $null) {
    Write-Host "Sub-site" $SiteUrl "exists!" 
} 
else {
    Write-Host "Sub-site" $SiteUrl "does not exist." 
}

REVISED CODE BELOW PER ANDERS' OBSERVATIONS: I revised this for my own sake and for the sake of anyone who would like to see a complete solution. Thanks for the tips, Anders!

# check to ensure Microsoft.SharePoint.PowerShell is loaded if not using the SharePoint Management Shell
Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue # -EA 0 works also

$siteURL="http://pcrob7/robsubsite"  # works for sites and site collections
$site = Get-SPWeb $siteUrl -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection

if ($err)
{
   # Write-Error/Write-Warning or maybe just create a new web with New-SPWeb
   Write-Host Site Collection $siteToGet does not exist

}
else
{
   Write-Host Site $siteToGet DOES exist
}
Rob Wilson
  • 4,180
  • 2
  • 24
  • 30