append string to variable in for each loop

Welcome Forums General PowerShell Q&A append string to variable in for each loop

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #111037

    Participant
    Points: -1
    Rank: Member

    #Contents of foobar1.csv = vette_ionisio@foo.org
    #Entry in Azure AD = vette_ionisio_foo.org#EXT#@foosso.onmicrosoft.com

    Question: How can i add the phrase "#EXT#@foosso.onmicrosoft.com" to the UPN vette_ionisio@foo.org (spreadsheet contents)

    Connect-AzureAD

    Import-Csv D:\Powershell_temp\foobar1.csv | ForEach-Object {
    $ObjectId = (Get-AzureADUser -ObjectId '$_.Userprincipalname "#EXT#@wvusso.onmicrosoft.com"').ObjectId
    }

    Here is error message:

    Get-AzureADUser : Error occurred while executing GetUser
    Code: Request_BadRequest
    Message: The request URI is not valid. Since the segment 'Microsoft.DirectoryServices.User' refers to a collection, this must be the last segment in the request URI. All intermediate
    segments must refer to a single resource.
    HttpStatusCode: BadRequest
    HttpStatusDescription: Bad Request
    HttpResponseStatus: Completed
    At line:2 char:18

    Thank you for your input

    Norm

  • #111044

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    Variables can't be put in single quotes.
    You can d below.
    '$_.Userprincipalname "#EXT#@wvusso.onmicrosoft.com"' will not be the one you expect.

    Import-Csv D:\Powershell_temp\foobar1.csv | ForEach-Object {
    $ID = $_.Userprincipalname + '#EXT#@wvusso.onmicrosoft.com'
    $ObjectId = (Get-AzureADUser -ObjectId $ID).ObjectId
    }
    
    • #111047

      Participant
      Points: -1
      Rank: Member

      Hello Kvprasoon;

      Thank you for your response it worked!!

      Norm

  • #111055

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    Great, If you were not aware of this before, I would request you to read the documentation about quoting rules.

    About Quoting Rules

The topic ‘append string to variable in for each loop’ is closed to new replies.