gpt4 book ai didi

arrays - 传递字符串数组和foreach循环不起作用:没有数组,只有一个字符串?

转载 作者:行者123 更新时间:2023-12-03 00:51:11 28 4
gpt4 key购买 nike

我有一个传递给函数的ntp-server数组来循环通过它。这是发生了什么:

  $srvA = @(
'0.at.pool.ntp.org',
'1.at.pool.ntp.org',
'2.at.pool.ntp.org',
'3.at.pool.ntp.org'
)

Function Get-NtpTime {
param($srvList)
$srvList
$nSrv = $srvList.Length
foreach ( $Server in $srvList ) {
$nSrv--
Write-Host $Server $nSrv
}
}

Get-NtpTime $srvA
0.at.pool.ntp.org 1.at.pool.ntp.org 2.at.pool.ntp.org 3.at.pool.ntp.org
0.at.pool.ntp.org 1.at.pool.ntp.org 2.at.pool.ntp.org 3.at.pool.ntp.org 70

如您所见,$ srvList似乎是一个字符串而不是字符串数组,并且
$ Server不是单个服务器,而是全部,长度是70,而不是4。
数组的定义似乎不正确,但是为什么在哪里以及如何使用?
(我尝试过1行数组的版本-没什么区别)

最佳答案

您应该将$srvList参数键入为数组。

function Get-NtpTime
{
param(
[string[]]
$srvList
)

# ...snip...
}

关于arrays - 传递字符串数组和foreach循环不起作用:没有数组,只有一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25630616/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com