【问题标题】:"The property cannot be found on this object. Verify that the property exists." Property definitely exists“在此对象上找不到该属性。验证该属性是否存在。”财产肯定存在
【发布时间】:2021-10-22 10:14:19
【问题描述】:

我无法弄清楚为什么我无法更改这些变量。如果我在调试模式下将它们输入控制台,那么它会打印这些值。我如何能够在第 24 行更改 allowedRequestors 变量也很奇怪,但不能更改其他任何变量。有谁知道为什么其他变量会发生这种情况?

$FilePath = "C:\Users\Desktop\TestScripts\testBulkAP.csv"

$headers = & $PSScriptRoot\GetToken.ps1

## preparing create Catalog data
$accesspacakgeRequest = '{"displayName":"","description":"sddsds","isHidden":false,"catalogId":"","accessPackageResourceRoleScopes":[],"accessPackageAssignmentPolicies":[{"displayName":"Initial Policy","description":"Initial Policy","durationInDays":365,"expirationDateTime":null,"canExtend":false,"requestApprovalSettings":null,"accessReviewSettings":null,"notificationSettings":null,"additionalInfo":null,"isDenyPolicy":false,"id":"","activeAssignmentCount":0,"accessPackageId":"00000000-0000-0000-0000-000000000000","accessPackageCatalog":null,"createdDateTime":null,"modifiedDateTime":null,"createdBy":"","modifiedBy":"","countOfUsersIncludedInPolicy":null,"requestorSettings":{"acceptRequests":true,"scopeType":"NoSubjects","allowedRequestors":[],"isOnBehalfAllowed":false},"questions":[]}]}'
$emlRequestUrl = "https://graph.microsoft.com/beta/identityGovernance/entitlementManagement/accessPackages"
$accesspacakgeRequestObject = ConvertFrom-Json -InputObject $accesspacakgeRequest

$Content = Import-Csv $FilePath

foreach($assignmentData in $Content) {
    $accesspacakgeRequestObject.catalogId = $assignmentData.catalogId
    $accesspacakgeRequestObject.displayName = $assignmentData.displayName
    $accesspacakgeRequestObject.description = $assignmentData.description
    $accesspacakgeRequestObject.accessPackageAssignmentPolicies.requestorSettings.scopeType = $assignmentData.scope 

    if ($assignmentData.scope -eq "SpecificDirectorySubjects") {
    $accesspacakgeRequestObject.accessPackageAssignmentPolicies.requestorSettings.allowedRequestors += New-Object -TypeName psobject -Property @{'@odata.type' = '#microsoft.graph.groupMembers'; 'id' = $assignmentData.groupId; 'description' = $assignmentData.groupName; 'isBackup' = 'false'}
    }

    $numApprovalStages = [int]$assignmentData.approvalStages

    if ($numApprovalStages -gt 0) {
    $accesspacakgeRequestObject.accessPackageAssignmentPolicies.requestApprovalSettings += New-Object -TypeName psobject -Property @{'approvalMode' = 'Serial'; 'isApprovalRequired' = 'true'; 'isApprovalRequiredForExtension' = 'false'; 'isRequestorJustificationRequired' = 'false'; 'approvalStages' = @()}

    for ($i=1;$i -le [int]$assignmentData.approvalStages; $i++)
    {
        $accesspacakgeRequestObject.accessPackageAssignmentPolicies.requestApprovalSettings.approvalStages += New-Object -TypeName psobject -Property @{'approvalStageTimeOutInDays' = '14'; 'primaryApprovers' = @(); escalationApprovers = @();'isEscalationEnabled' = 'false'; 'escalationTimeInMinutes' = '0'; 'isApproverJustificationRequired' = 'true'}
        $accesspacakgeRequestObject.accessPackageAssignmentPolicies.requestApprovalSettings.approvalStages.primaryApprovers += New-Object -TypeName psobject -Property @{'@odata.type' = '#microsoft.graph.singleUser'; "displayName" = ''; 'objectId' = Get-Variable -Name "assignmentData.approver$1" -ValueOnly; 'isBackup' = 'false'}
        
    }

    }
    
    
    
    $requestbody = $accesspacakgeRequestObject | ConvertTo-Json -Depth 10
    $response = Invoke-RestMethod $emlRequestUrl -Headers $headers -Method Post -Body $requestbody -UseBasicParsing -ErrorAction Continue
}

错误信息是:

The property 'requestApprovalSettings' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\Desktop\TestScripts\AddAccessPackageAndPolicyWITHAPPROVER.ps1:30 char:5
+     $accesspacakgeRequestObject.accessPackageAssignmentPolicies.reque ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
The property 'approvalStages' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\Desktop\TestScripts\AddAccessPackageAndPolicyWITHAPPROVER.ps1:34 char:9
+         $accesspacakgeRequestObject.accessPackageAssignmentPolicies.r ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
Get-Variable : Cannot find a variable with the name 'assignmentData.approver1'.
At C:\Users\Desktop\TestScripts\AddAccessPackageAndPolicyWITHAPPROVER.ps1:35 char:250
+ ... objectId' = Get-Variable -Name "assignmentData.approver$i" -ValueOnly ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (assignmentData.approver1:String) [Get-Variable], ItemNotFoundException
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
 
The property 'primaryApprovers' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\Desktop\TestScripts\AddAccessPackageAndPolicyWITHAPPROVER.ps1:35 char:250
+ ... objectId' = Get-Variable -Name "assignmentData.approver$i" -ValueOnly ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

【问题讨论】:

    标签: json powershell psobject


    【解决方案1】:

    .accessPackageAssignmentPolicies 属性包含一个数组[...]-包含在 JSON 输入中)。

    即使该数组恰好只包含 一个 元素,您仍然需要 按索引 访问它以便 设置 它(仅) 元素的属性;例如:

    # Note the `[0]`
    $accesspacakgeRequestObject.accessPackageAssignmentPolicies[0].requestorSettings.scopeType = $assignmentData.scope 
    

    请注意,获取属性确实严格要求此索引访问,因为有一个名为member enumeration的功能。

    这种可能令人惊讶的不对称性 - 要求设置上进行索引访问 - 但是,by design - 请参阅 this answer 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-24
      • 1970-01-01
      • 2021-04-24
      • 2022-01-24
      • 2022-10-04
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多