none
foreach not working RRS feed

  • Question

  • Hi all, Iam running the powershell script below, but I get an error. I can't figure out why it doesn't work, other than that the output of the earlier get in $users gives the wrong output format. I don't know how to get this right. 

    $users = get-msoluser -all | where-object {$_.validationstatus -eq 'error'} | select ObjectId
    foreach ($user in $users) {
    $errors = (Get-Msoluser -ObjectId $user).Errors
    $errors | foreach-object {"`nService: " + $_.ErrorDetail.Name.split("/")[0]; "Error Message:"+ $_.ErrorDetail.ObjectErrors.ErrorRecord.ErrorDescription}

    }

    Error:

    Get-MsolUser : Cannot bind parameter 'ObjectId'. Cannot convert the "@{ObjectId=009f1ee5-f9a4-4dee-b513-4487cdca043e
    }" value of type "Selected.Microsoft.Online.Administration.User" to type "System.Guid".
    At C:\temp\AdSyncError.ps1:3 char:36
    +     $errors = (Get-Msoluser -ObjectId $user).Errors
    +                                       ~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-MsolUser], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Online.Administration.Automation.GetUser

    You cannot call a method on a null-valued expression.
    At C:\temp\AdSyncError.ps1:4 char:28
    + ... ach-object {"`nService: " + $_.ErrorDetail.Name.split("/")[0]; "Error ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Monday, August 29, 2016 1:14 PM

Answers

  • Use $user.ObjectID inside the loop or add -ExpandProperty to your initial Select-Object.

    • Proposed as answer by Vincent Karunaidas Monday, August 29, 2016 1:57 PM
    • Marked as answer by Oekeloen Wednesday, August 31, 2016 9:47 AM
    Monday, August 29, 2016 1:49 PM

All replies