使用active directory命令的Powershell在函数外工作,但在函数内不工作



我正在PowerShell中编写一个函数来自动化active directory。当独立执行命令时,它工作得很好,但当我制作函数时,它不工作。

Function addMember($parent, $child){
$parentObjectID = Get-AzureADGroup -Filter "DisplayName eq '$parent'" -ErrorAction Continue | select -ExpandProperty ObjectId
$childObjectID = Get-AzureADGroup -Filter "DisplayName eq '$child'" -ErrorAction Continue | select -ExpandProperty ObjectId
Add-AzureADGroupMember -ObjectId $parentObjectID -RefObjectId $childObjectID
}

addMember('Main', 'Child')
Get-AzureADGroup : Error occurred while executing GetGroups 
Code: Request_UnsupportedQuery
Message: Unsupported or invalid query filter clause specified for property 'displayName' of resource 'Group'.
RequestId: 0ab4dc0e-0492-427c-8c86-df6cb7d4e7b6
DateTimeStamp: Tue, 06 Jul 2021 08:07:17 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:3 char:22
+ ... dObjectID = Get-AzureADGroup -Filter "DisplayName eq '$child'" -Error ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-AzureADGroup], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetGroup

Add-AzureADGroupMember : Cannot bind argument to parameter 'ObjectId' because it is null.
At line:5 char:38
+     Add-AzureADGroupMember -ObjectId $parentObjectID -RefObjectId $ch ...
+                                      ~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Add-AzureADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.AddGroupMember

这是一个变量的问题吗?

我通过调用函数解决了这个问题

addMember"Main"Child">

最新更新