在 PowerShell 中有条件地指定开关参数
我正在编写一个 PowerShell 脚本,该脚本使用 SharePoint 2010 New-SPWeb cmdlet 创建由 Xml 文件定义的网站:
<?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>
New-SPWeb cmdlet 具有三个开关参数:
- AddToQuickLaunch
- AddToTopNav
- UseParentTopNav
我遇到了问题弄清楚如何仅在 Xml 中的匹配值为 0 或 $true 时设置 switch 参数。
这几乎就像我在调用 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");
}
非常感谢!
I'm writing a PowerShell script that uses the SharePoint 2010 New-SPWeb cmdlet to create sites defined by an Xml file:
<?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>
The New-SPWeb cmdlet has three switch parameters:
- AddToQuickLaunch
- AddToTopNav
- UseParentTopNav
I'm having trouble figuring out how to only set the switch parameter when the matching value in the Xml is 0 or $true.
It's almost like I need some sort of inline-if syntax when calling New-SPWeb to set the three switch parameters.
$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");
}
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计算之前的
$result
($true
或$false
):并在之后使用它:
Calculate the
$result
($true
or$false
) before:And use it after:
还有一种方法。您可以像这样使用splatting:
有关splatting的更多信息:
“@”符号是什么在 Powershell 中做什么?
http://blogs.msdn.com/b/powershell/archive/2009/01/02/how-and-why-to-use-splatting-passing-switch-parameters.aspx
There is one more way. You can use splatting like this:
More about splatting:
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