PowerShell:为变量分配多个值,但后来只调用一半?

PowerShell: assign multiple values to variable, but later call only half?

编辑使问题更简单

我基本上只是试图调用具有多个值的变量的一部分。

示例:

我有这个变量

$a = "alocf", "arbmi", "ausbt", "auspg", "bmicw", "cidci", "cltcr", "cwnfl", "dkdsd", "dttsd", "elpca", "elyea", "grrgv", "grrym", "hbgno", "hopkn", "houra", "hourm", "housa", "irvbp", "jeftb", "lnhmd", "mcnbn", "mkcrd", "mkeav", "msnav", "msndd", "mspav", "myoca", "omacw", "omaha", "phxbh", "pitns", "rbkca", "rkcir", "rkswy", "rstev", "rstss", "rvrgr", "scfwi", "spiwx", "stlhb", "scvnm", "tmbpw", "tpafa", "wakny"

如您所见,它有多个值,最终我尝试调用该变量,但只调用可用值的前半部分。

有什么办法吗?


原题

所以我正在为我的工作重写一个脚本,该脚本基本上应该根据人数为特定 URL 打开 x 个 Google Chrome 选项卡使用它以及他们正在做的部分。综上所述,他们 运行 脚本,它询问有多少人正在使用它。假设有 5 个人,那么他们有 5 个选项可供选择,这 5 个选项将 46 个 URL 分配给彼此。

我的问题是我想制作它以使其或多或少地成为未来的证明。看,在我现在的 5 个人中,我有 4 个 9 部分和 1 个 10 部分。但是,如果他们更新 URLs 并且突然有 47,我需要更改几乎整个事情,调整每个部分中有多少属性。

我的计划是,基本上我有一个变量。每个 URL 设置为 46 个不同的参考词。我希望能够使用 if 语句,例如:

#If statement for if 2 people are using script.
if ($b -eq "2") {

#Variable set equal to the total number of URL reference words($a.count) divided by 2, for if there are 2 people.  
$c = $a.count / 2

#Variable that subtracts the prior whole number of the previous variable, in case it is an odd number, from the total amount of URL reference words, to get the total number of the second half.
$d = $a.count - [Math]::Floor($c)

#Prompt to ask which section they're doing.
$e = Read-Host "Are you doing section 1 or section 2? >"

    #If statement for if they do the first half
    if ($e -eq "1") { }

}

显然还没有完成,但这是这部分脚本的一般设置。我想基本上说,如果他们选择第 1 部分,取 $c 等于的数字,并从具有所有其他变量的变量中仅提取该数量的 URLs。

过去几天我四处寻找,但显然我找错了地方,或者需要提高我的 google 搜索技巧哈哈。

总结:如果一个变量有多个值,有没有办法只调用该变量的一部分?

非常感谢任何帮助,谢谢。

如果我没理解错的话,你想要完成的是动态地将数组拆分成块,如果我的假设是正确的,你可以使用这个助手 function:

function ChunkArray {
    param([int] $Chunks)

    [ref] $ref = 0
    $toProcess = @($input)
    $groupSize = [math]::Ceiling($toProcess.Count / $Chunks)
    $tag = ''
    while($Chunks) {
        $Chunks = $Chunks / 10
        $tag += 0
    }
    $toProcess | Group-Object {
        "Chunk{0:$tag}" -f [math]::Floor($ref.Value++ / $groupSize)
    }
}

此函数可以使用 automatic variable $input 将您的数组拆分/分组到传递给 -Chunks 参数的参数中 例如,使用问题中的数组 $a

PS /> $chunks = $a | ChunkArray -Chunks 8
PS /> $chunks | Format-Table

Count Name         Group
----- ----         -----
    6 Chunk00      {alocf, arbmi, ausbt, auspg…}
    6 Chunk01      {cltcr, cwnfl, dkdsd, dttsd…}
    6 Chunk02      {grrgv, grrym, hbgno, hopkn…}
    6 Chunk03      {housa, irvbp, jeftb, lnhmd…}
    6 Chunk04      {mkeav, msnav, msndd, mspav…}
    6 Chunk05      {omaha, phxbh, pitns, rbkca…}
    6 Chunk06      {rstev, rstss, rvrgr, scfwi…}
    4 Chunk07      {scvnm, tmbpw, tpafa, wakny}

# Inspecting the 4th chunk:
PS /> $chunks[3].Group

housa
irvbp
jeftb
lnhmd
mcnbn
mkcrd