Search code examples
powershellsharepoint-2010

running a PowerShell command sp 2010


I am trying to create a site map with a PowerShell command from this example: http://blogs.msdn.com/b/opal/archive/2010/04/13/generate-sharepoint-2010-sitemap-with-windows-powershell.aspx

My actions: I copied the code into a file named New-SPSiteMap

I opened the PowerShell and wrote

New-SPSiteMap –Url http://centerportal –SavePath C:\inetpub\wwwroot\wss\VirtualDirectories\80\sitemap.xml

The error I get is:

The term 'New-SPSiteMap' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, 
or if a path was included, verify that the path is correct and try again.
At line:1 char:14
+ New-SPSiteMap <<<<  -Url http://mossdev2010  -SavePath C:\inetpub\wwwroot\wss\VirtualDirectories\80\sitemap.xml
+ CategoryInfo          : ObjectNotFound: (New-SPSiteMap:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Solution

  • In order to have the New-SPSiteMap function available you have to execute the script containing the function:

    & .\New-SPSiteMap.ps1
    New-SPSiteMap –Url http://centerportal –SavePath C:\inetpub\wwwroot\wss\VirtualDirectories\80\sitemap.xml
    

    Alliteratively, you could turn the PowerShell script into a "function" that is callable like this:

    .\New-SPSiteMap.ps1 -Url http://centerportal –SavePath C:\inetpub\wwwroot\wss\VirtualDirectories\80\sitemap.xml
    

    All you have to do is remove the function declaration function New-SPSiteMap:

    param($SavePath="C:\inetpub\wwwroot\wss\VirtualDirectories\80\SiteMap.xml", $Url="http://sharepoint")
    
    function New-Xml
    {
        param($RootTag="urlset",$ItemTag="url", $ChildItems="*", $SavePath="C:\SiteMap.xml")
    
        Begin {
            $xml="<?xml version=""1.0"" encoding=""UTF-8""?>
            <urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">"
        }
        Process {
            $xml += " <$ItemTag>"
            foreach ($child in $_){
            $Name = $child
            $xml += " <$ChildItems>$url/$child</$ChildItems>"
        }
            $xml += " </$ItemTag>"
        }
        End {
            $xml += "</$RootTag>"
            $xmltext=[xml]$xml
            $xmltext.Save($SavePath)
        }
    }
    
    $web = Get-SPWeb $url
    $list = $web.Lists | ForEach-Object -Process {$_.Items} | ForEach-Object -Process {$_.url.Replace(" ","%20")}
    
    # excludes directories you don’t want in sitemap. you can put multiple lines here:
    
    $list = $list | ? {$_ -notmatch "_catalogs"} 
    $list = $list | ? {$_ -notmatch "Reporting%20Templates"} 
    $list = $list | ? {$_ -notmatch "Reporting%20Metadata"} 
    $list | New-Xml -RootTag urlset -ItemTag url -ChildItems loc -SavePath $SavePath