在 PowerShell 中有条件地指定开关参数

发布于 2024-12-11 00:18:29 字数 1574 浏览 0 评论 0原文

我正在编写一个 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 具有三个开关参数:

  1. AddToQuickLaunch
  2. AddToTopNav
  3. 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:

  1. AddToQuickLaunch
  2. AddToTopNav
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2024-12-18 00:18:29

计算之前的 $result$true$false):

$result = ...

并在之后使用它:

New-SPWeb `
-Url "$URL/$siteToCreate.url" `
-Name $siteToCreate.name `
-Template $webTemplate `
-Language $siteToCreate.language `
-AddToQuickLaunch:$result `
-AddToTopNav:$result `
-UseParentTopNav:$result

Calculate the $result ($true or $false) before:

$result = ...

And use it after:

New-SPWeb `
-Url "$URL/$siteToCreate.url" `
-Name $siteToCreate.name `
-Template $webTemplate `
-Language $siteToCreate.language `
-AddToQuickLaunch:$result `
-AddToTopNav:$result `
-UseParentTopNav:$result
梦一生花开无言 2024-12-18 00:18:29

还有一种方法。您可以像这样使用splatting:

$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

有关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:

$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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文