在PowerShell中

有条件指定开关参数我正在写使用SharePoint 2010 新的SPWeb cmdlet创建的XML文件中定义的网站PowerShell脚本:在PowerShell中

<?xml version="1.0" encoding="utf-8" ?> 

<siteStructure>

<site>

<url>Corporate</url>

<name>Corporate</name>

<language>1033</language>

<addToQuickLaunch>0</addToQuickLaunch>

<addToTopNav>0</addToTopNav>

<useParentTopNav>1</useParentTopNav>

</site>

</siteStructure>

的新的SPWeb cmdlet的有三个开关参数:

  1. AddToQuickLaunch
  2. AddToTopNav
  3. UseParentTopNav

我无法弄清楚如何在Xml中的匹配值为0或$ true时设置开关参数。

这几乎就像调用New-SPWeb设置三个开关参数时需要某种内联if语法。

$siteStructureData = [xml](Get-Content .\Data-SiteStructure.xml) 

# find the template to use

$webTemplate = Get-SPWebTemplate | Where {$_.Title -like $templateName }

if ($webTemplate)

{

# iterate through and create sites

foreach ($siteToCreate in $siteStructureData.siteStructure.site)

{

"Creating site $webtitle at $URL/$webUrl"

# http://technet.microsoft.com/en-us/library/ff607579.aspx

New-SPWeb `

-Url "$URL/$siteToCreate.url" `

-Name $siteToCreate.name `

-Template $webTemplate `

-Language $siteToCreate.language

# set -AddToQuickLaunch

# set -AddToTopNav

# set -UseParentTopNav

}

}

else

{

throw ("Could not find template $templateName");

}

非常感谢!

回答:

计算前$result$true$false):

$result = ... 

而且使用后:

New-SPWeb ` 

-Url "$URL/$siteToCreate.url" `

-Name $siteToCreate.name `

-Template $webTemplate `

-Language $siteToCreate.language `

-AddToQuickLaunch:$result `

-AddToTopNav:$result `

-UseParentTopNav:$result

回答:

还有一个办法。您可以使用泼洒这样的:

$params = @{ 

Url = "$URL/$siteToCreate.url"

Name = $siteToCreate.name

Template = $webTemplate

Language = $siteToCreate.language }

# if needed, add the keys to the hashtable

if ($result) {

$params.AddToQuickLaunch = $true

$params.AddToTopNav = $true

$params.UseParentTopNav = $true

}

# now you just pass the parameters:

New-SPWeb @params

更多泼洒:

What does the "@" symbol do in Powershell? http://blogs.msdn.com/b/powershell/archive/2009/01/02/how-and-why-to-use-splatting-passing-switch-parameters.aspx

以上是 在PowerShell中 的全部内容, 来源链接: utcz.com/qa/263553.html

回到顶部