Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 无法将格式化的Get-ADOrganizationlUnit输出传递到脚本中的函数中_Powershell - Fatal编程技术网

Powershell 无法将格式化的Get-ADOrganizationlUnit输出传递到脚本中的函数中

Powershell 无法将格式化的Get-ADOrganizationlUnit输出传递到脚本中的函数中,powershell,Powershell,我无法将Get ADOrganizationalUnit的结果传递到脚本中的函数中 我将结果存储在一个变量中,该变量用于将cmdlet返回的OU的规范名称添加到表单上的下拉列表中 然后,我尝试在一个函数中使用相同的变量,调用该变量时,将根据所选的规范名称确定OU的可分辨性 由于变量是在表单加载时设置的,因此相关下拉列表将填充各种OU,因此我添加了一个write output$myVar,以确保在传递到函数之前没有发生任何奇怪的事情。我尝试使用$global:varName使$myVar全局化,并

我无法将
Get ADOrganizationalUnit
的结果传递到脚本中的函数中

我将结果存储在一个变量中,该变量用于将cmdlet返回的OU的规范名称添加到表单上的下拉列表中

然后,我尝试在一个函数中使用相同的变量,调用该变量时,将根据所选的规范名称确定OU的可分辨性

由于变量是在表单加载时设置的,因此相关下拉列表将填充各种OU,因此我添加了一个
write output$myVar
,以确保在传递到函数之前没有发生任何奇怪的事情。我尝试使用
$global:varName
使
$myVar
全局化,并且在调用函数时尝试传入变量:
myFunction$myVar$myVar1
。如果我在函数中使用
write output$myVar
,则没有输出,但我可以使用
write host$myVar
,它将返回一个字符串,其中只包含
$myVar
中所有OU的可分辨名称

我也在shell中直接测试了这一点,并且在将规范名称关联回OU的DN方面没有任何问题,但是我不知道在脚本中使用它时,我做了什么错误的操作,导致它无法工作

我正在使用它获取下拉列表和函数的OU数据:

$userOUs=Get-ADOrganizationalUnit-SearchBase$ouRoot-Filter*-Properties-CanonicalName |其中对象{$\.Name-like'*user*}
注意:使用
$userus
成功填充下拉列表

我用一个按钮调用该函数:

$myBtn.Add_单击({myFunction$userID$userus})
我试图将其传递给的函数:


函数myFunction($userID,$userOUs){
写入输出$userOUs#不返回任何内容
Write Host$userOUs#返回包含所有OU可分辨名称的字符串
$selectedOU=$OUList.SelectedItem
$targetOUCanonicalName=“$domainPrefix$selectedOU”#我从规范名称中删除域名以显示在下拉列表中,但将其添加回此处
$targetOu=$userOUs |其中对象{$\.CanonicalName-eq$targetCanonicalName}|选择-ExpandProperty DifferentizedName
获取ADUser-Identity$userID |移动ADObject-TargetPath$TargetTou
}
最终,目标是能够使用
$userus
变量根据在OU规范名称下拉列表中所做的选择来确定OU的DN。我想尝试让它更具动态性,而不必在switch语句中定义所有内容

我希望,一旦我得到了一个正确的方向,关于为什么变量没有按我需要的方式传递到函数中,我就能够做到这一点


编辑:我不打算使用
write host
write output
,因为脚本将有一个表单,我只是用它来尝试并弄清楚发生了什么。

我尝试了您的版本,它似乎工作得很好:

function myFunction($userID, $userOUs) {
    Write-Output $userOUs #returns nothing
    Write-Host $userOUs #returns the string containing all of the OUs' distinguished names

    #$selectedOU = $OUList.SelectedItem
    #$targetOUCanonicalName = "$domainPrefix$selectedOU" #I remove the domain name from the canonical name for display in the dropdown but add it back here
    #$targetOu = $userOUs | Where-Object {$_.CanonicalName -eq $targetCanonicalName} | select -ExpandProperty distinguishedName
   # Get-ADUser -Identity $userID | Move-ADObject -TargetPath $targetOU

}

$userOUs = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | Where-Object {$_.Name -like '*user*' }
$userOUs

myFunction -userID $UserID -userOUs $userOUs

$UserOus为我显示数据,当我执行上述函数时,我也会得到输出。我唯一想到的是这个问题在别的地方

我试过你的版本,它似乎很好用:

function myFunction($userID, $userOUs) {
    Write-Output $userOUs #returns nothing
    Write-Host $userOUs #returns the string containing all of the OUs' distinguished names

    #$selectedOU = $OUList.SelectedItem
    #$targetOUCanonicalName = "$domainPrefix$selectedOU" #I remove the domain name from the canonical name for display in the dropdown but add it back here
    #$targetOu = $userOUs | Where-Object {$_.CanonicalName -eq $targetCanonicalName} | select -ExpandProperty distinguishedName
   # Get-ADUser -Identity $userID | Move-ADObject -TargetPath $targetOU

}

$userOUs = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | Where-Object {$_.Name -like '*user*' }
$userOUs

myFunction -userID $UserID -userOUs $userOUs

$UserOus为我显示数据,当我执行上述函数时,我也会得到输出。我唯一想到的是这个问题在别的地方

您的函数引用了
$OUList
,我看不到在任何地方定义了它。我想首先测试一下,
$SelectedOU
是否输出您所期望的内容。
$OUList
是我填充下拉列表的地方。它只是在我的表单代码中,并在那里填充。我刚刚验证了调用函数时,
$selectedOU
是否返回下拉列表中的选定项。
$domainPrefix
是否在字符串末尾包含正斜杠?它确实包含。如果我在设置变量、函数的位置复制粘贴代码,然后从shell调用函数,
$targetOU
将正确返回,但作为脚本运行时则不会。我假设导致
write output
在函数中不返回任何内容的原因与我无法正确设置
$targetOU
的原因相同。如果
write host$userOUs
只返回可分辨名称列表,那么这将是有问题的
$userOUs
必须是AD OU对象,脚本的其余部分才能工作,因为您引用的属性将位于该类型的OU对象上。例如,
$UserOUs |其中{$\.canonicalname-eq“something”}
如果您只有一个DNs列表,将永远找不到任何内容。您的函数引用了
$OUList
,我看不到在任何地方定义了它。我想首先测试一下,
$SelectedOU
是否输出您所期望的内容。
$OUList
是我填充下拉列表的地方。它只是在我的表单代码中,并在那里填充。我刚刚验证了调用函数时,
$selectedOU
是否返回下拉列表中的选定项。
$domainPrefix
是否在字符串末尾包含正斜杠?它确实包含。如果我在设置变量、函数的位置复制粘贴代码,然后从shell调用函数,
$targetOU
将正确返回,但作为脚本运行时则不会。我假设导致
write output
在函数中不返回任何内容的原因与我无法正确设置
$targetOU
的原因相同。如果
write host$userOUs
只返回可分辨名称列表,那么这将是有问题的<代码>$userOUs必须是AD OU对象,脚本的其余部分才能工作,因为您正在引用