运行PowerShell命令sp 2010

时间:2011-12-08 10:38:37

标签: powershell sharepoint-2010

我正在尝试使用此示例中的PowerShell命令创建站点地图: http://blogs.msdn.com/b/opal/archive/2010/04/13/generate-sharepoint-2010-sitemap-with-windows-powershell.aspx

我的行动:我将代码复制到名为New-SPSiteMap

的文件中

我打开了PowerShell并写了

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

我得到的错误是:

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

1 个答案:

答案 0 :(得分:2)

为了使New-SPSiteMap函数可用,您必须执行包含函数的脚本:

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

Alliteratively,您可以将PowerShell脚本转换为可以像这样调用的“函数”:

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

您所要做的就是删除函数声明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