【问题标题】:How do I parse through multiple inputs?如何解析多个输入?
【发布时间】:2020-01-07 13:48:39
【问题描述】:

早上好!所以我有一个 PowerShell 服务器构建脚本,它执行以下操作:

询问您是否要将 AD 组添加到您正在构建的服务器上的管理员,或者您是否想在不同的服务器上搜索管理员中的组和用户并使用其中之一。

  1. 如果您想将 AD 组添加到管理员,它会询问您什么 AD 组并将其保存在变量中 ($OSAdministrators)
  2. 如果要在服务器中搜索组和用户,请输入 服务器,它搜索并显示所有组的结果和 管理员中的用户。然后它会要求您输入哪个组 你想使用,并将其保存在同一个变量中 ($OSAdministrators)。

#2 的示例代码:

$OSAdministratorsSearchHost = Read-Host "Enter the hostname of the server to search for Administrators groups"

function Get-LocalAdmin {
    $admins = Gwmi win32_groupuser –Computer $OSAdministratorsSearchHost
    $admins = $admins |? {$_.GroupComponent –like '*"Administrators"'}
    $admins |% {
        $_.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $nul
        $matches[1].trim('"') + “\” + $matches[2].trim('"')
        }
    }
Get-LocalAdmin

$OSAdministrators = Read-Host "Enter the name of the AD group from the list above to add to Administrators on the new server; press Enter to skip"

如果您只想添加 1 个组,这非常有用。问题是有时您可能有几个组要添加到服务器,我不知道如何处理。例如,对于上面的#2,我希望它是这样的:

$OSAdministrators = Read-Host "Enter the name(s) of the AD group(s) from the list above to add to Administrators on the new server. If entering multiple, separate them with a comma (e.g. "Server Group 1,Server Group 2")"

但我不确定如何拆分“服务器组 1”和“服务器组 2”并稍后在我的代码中使用它,它实际上将组添加到您正在构建的服务器上的管理员:

$DomainName = "[where the domain FQDN would be]"
$AdminGroup = [ADSI]"WinNT://$HostName/Administrators,group"
$Group = [ADSI]"WinNT://$DomainName/$OSAdministrators,group"
$AdminGroup.Add($Group.Path)

我尝试过在线搜索,但我搜索的方式没有找到适合这个特定用例的任何内容,或者解决方案对于我正在尝试做的事情似乎过于复杂(我说的是 30仅用于解析输入的代码行)。我想我只是想念一种更简单的方法。

任何方向将不胜感激。谢谢!

【问题讨论】:

  • 你看过Out-GridView了吗?这将为您提供一个可供选择的列表 - 并且可以允许选择多个项目并将其传递到下一阶段。

标签: powershell active-directory windows-server


【解决方案1】:

如果您可以添加一些关于如何输入输入的控件,您可以通过, 拆分输入。然后循环处理拆分项。

$OSAdministrators = Read-Host "Enter the name(s) of the AD group(s) from the list above to add to Administrators on the new server. If entering multiple, separate them with a comma (e.g. "Server Group 1,Server Group 2")"

$OSAdmins = ($OSAdministrators -split ",").Trim()
foreach ($OSAdministrator in $OSAdmins) {

  $DomainName = "[where the domain FQDN would be]"
  $AdminGroup = [ADSI]"WinNT://$HostName/Administrators,group"
  $Group = [ADSI]"WinNT://$DomainName/$OSAdministrator,group"
  $AdminGroup.Add($Group.Path)
}

在您当前的循环迭代中,您可以在任何需要的地方重复使用$OSAdministrator。在循环之外,您可以访问$OSAdmins 数组中的元素。

【讨论】:

  • 效果很好,谢谢!不知道为什么我还没有遇到“分裂”,但这很完美,很好地解析了那些。
【解决方案2】:

如果你可以在那个时候使用 GUI,这可能会满足你的需要......

function Get-LocalAdminList ($ComputerName) {
    (Get-CimInstance -ClassName Win32_GroupUser –Computer $ComputerName |
        Where-Object {
            $_.GroupComponent -match 'administrators'
            }).PartComponent.Name
    }

$ChosenItemList = Get-LocalAdminList -ComputerName $env:COMPUTERNAME |
    Out-GridView -OutputMode Multiple -Title 'Please select the desired Local Admin members & click OK'

$ChosenItemList

当我选择一个项目时,我会在列表中获得该项目。有两个...我得到两个字符串。 [咧嘴一笑]

【讨论】:

  • 不错!我也要试试这个。让它们可选择将比每次输入组更乏味。我相信我的队友会很感激的。 :)
  • 一个问题 - 如果我选择两个,现在两者都是 $ChosenItemList 变量的一部分吗?如果我改用 $ChosenItemLists,我可以这样做:foreach ($ChosenItemList in $ChosenItemLists).........
  • @Nate - 是的!返回的项目都存储在输出分配给的任何地方。因此,如果函数发出 1 个项目,您会在 $Var 中获得一个项目。如果 func 返回两个或多个项目,您将获得所有项目的集合。 ///// 你可能想要强制 $ChosenItemList 始终是一个数组...可以这样做 >>> [array]$ChosenItemList =
  • @Nate - 非常欢迎你......很高兴帮助一点! [咧嘴]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多