gpt4 book ai didi

arrays - Powershell 2 : Array size differs between return value and caller

转载 作者:行者123 更新时间:2023-12-04 23:29:26 24 4
gpt4 key购买 nike

几个小时以来,我一直在努力解决这个问题,而谷歌也无济于事。

我有一个在我的代码中调用的函数。调用如下所示:

$newData = @(CreateMailContactIfNeeded $entry)

被调用的函数应始终返回一个 2 元素数组。我确认这个
在被调用函数的 return 语句之前:
# ... (inside CreateMailContactIfNeeded) ...
$tmpArr = @("$contactString" , "external")
Write-Host "length is" $tmpArr.length
Write-Host "Contact string is ->"$contactString"<-"
return $tmpArr

长度始终为 2,并且联系人字符串始终为非空。示例输出:
length is 2
Contact string is -> GMSContact-33<-

(我无法去除字符串中的前导空格,所以我认为这是某种
内部powershell字符串格式或其他东西)

但是,当我回到调用函数时,有时但并非总是如此
我从返回值构建的数组长度为三:
if ($newData.length -eq 3) {
Write-Host "Weird - array length is 3..."
}

Write-Host "Array length now is "$newData.length
foreach ($tmpVar in $newData) {
Write-Host "Array entry is "$tmpVar
}

3-entry 数组的第一个元素是一个有效的字符串,但它是来自
前期处理:
Weird - array length is 3...
Array length now is 3
Array entry is Test User <-- INVALID
Array entry is GMSContact-33 <-- This should be first element
Array entry is external <-- This should be second element

有人可以向我解释可能发生了什么以及如何解决吗?我是否错误地进行了数组分配?鉴于上面的代码,“测试用户”怎么可能被插入到数组中?

编辑:下面 CreateMailContactIfNeeded 的全部内容...
function CreateMailContactIfNeeded {
param($CSVEntry)

$email = $CSVEntry.EMAIL_ADDR

Write-Host "--> Searching for $email"
Write-Host -n "----> In AD? "
# Return if this person already exists as a user in AD
if (IsInOurADDomain $CSVentry) {
Write-Host -f green "YES."
Write-Host "Returning "$email "= inhouse"
return @("$email" , "inhouse")
} else {
Write-Host -n -f gray "NO. "
}

$contactString = "GMSContact-" + $CSVEntry.MEMBER_ID
Write-Host "Contact string is now " $contactString

# Check if mail contact already exists. If not, create it.
Write-Host -n "In Contacts? "
$object = Get-MailContact $email 2> $null

if ($object -eq $null) {
$displayName = $CSVentry.FIRST_NAME + " " + $CSVentry.LAST_NAME

Write-Host -n -f gray "NO. "
Write-Host -n "Creating contact... "

$error.clear()
New-MailContact -Name $contactString `
-DisplayName $displayName `
-ExternalEmailAddress $email `
-OrganizationalUnit $global:OU_CONTACT_STRING
if ($error[0] -ne $null) {
Write-Host -f red "ERROR"
Write-Error $error[0]
return @("error","error")
}
# Derek says to do this to make it appear in alternate address book
Set-MailContact -identity $contactString -customattribute1 "AB2"
Write-Host -f green "SUCCESSFUL"
} else {
Write-Host -f green "YES"
}
Write-Host "Returning" $contactString "= external"
$tmpArr = @("$contactString" , "external")
Write-Host "length is" $tmpArr.length
Write-Host "Contact string is ->"$contactString"<-"
Write-Host "Contact string length is "$contactString.length
foreach ($tmp in $tmpArr) {
Write-Host "In function element is "$tmp
}
return $tmpArr
# return @("$contactString" , "external")
}

最佳答案

在 Poweshell 中,任何未被“捕获”的东西都会被有效地“返回”。

让我们举个例子:

function test(){

"starting processing"

#do some processing

write-host "starting different process"
$output = @("first",second")
return $output
}

当你做类似 $myoutput =test 的事情时,你会看到 $myoutput 实际上有三个元素 - first,second正如预期的那样,还有 starting processing .因为 starting processing未被捕获并“返回”到管道中。

只要在脚本中生成无效的“TEST USER”输出,请 try catch 输出或通过管道将其传送到 Out-Null或将其分配给 $null
找出额外元素从哪里“返回”的简单方法。

更改脚本,以便您不会在变量中捕获函数的输出。所以留下它为 CreateMailContactIfNeeded $entry
在运行脚本之前,运行 Set-PsDebug -trace 1 .现在,运行脚本 - 您应该能够看到 TEST USER正在被退回。按照上面的建议捕获它。

关于arrays - Powershell 2 : Array size differs between return value and caller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7325900/

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