Replacing Text in a foreach loop

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of Juli Juli 2 years, 2 months ago.

  • Author
    Posts
  • #19016
    Profile photo of Juli
    Juli
    Participant

    I have a script that I am building that will get the contents of a directory into a var, then populate another var with the information from the first var but modified. I am not getting any errors, but it's not modifying the information the way I think it should. Not sure what I have wrong here.

    The contents of the initial var look like this:

    Name
    ——-
    jdoe.jpg
    hdoe.jpg
    jhancock.jpg
    gwashington.jpg

    Here is the script that I am using. In the foreach the $Identity var is never getting populated so it can't process the command string that comes after.

    $exchange = new-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://atlexmbx2/powershell -Authentication kerberos
    import-PSSession $exchange
    param(
    [Parameter(Mandatory=$True)]
    [string]$Path,
    [string]$Photo,
    [string]$Identity,
    [string]$PhotoPath = $Path + "\" + $Photo

    )

    BEGIN
    {
    $Photo = Get-Item $Path\*.jpg | Select-Object Name

    }

    PROCESS{

    foreach($Name in $Photo)
    {
    $Identity = $Name.Photo -replace "@{Name=" -replace ".jpg}"

    Import-RecipientDataProperty -Identity $Identity -Picture -FileData ([Byte[]]$(Get-Content -Path $PhotoPath -Encoding Byte -ReadCount 0))

    $Identity
    }

    }

  • #19019
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    There are a few problems here:

    [ul]
    [li] The param block must be the first non-comment line of a script or function; you've got New-PSSession and Import-PSSession commands that would need to be moved into the Begin block. [/li]
    [li] You've defined several variables as parameters in the param block, but they don't appear to be actually used that way. You overwrite $Photo and $Identity inside the function without ever checking to see what values were passed in, and you appear to be making the assumption that $PhotoPath is automatically updated based on the values of $Path and $Photo each time $Photo is changed. (This is not the case; you would need to assign a new value to $PhotoPath each time.) [/li]
    [li] You're trying to access a .Photo property on the objects in your $Photo array, but that property doesn't exist (which is why you're seeing blank values for $Identity.) [/li]
    [/ul]

    Here's a revision of your code, which is what I think you intended it to do:

    param(
        [Parameter(Mandatory=$True)]
        [string]$Path
    )
    
    BEGIN
    {
        $Photo = Get-Item $Path\*.jpg
    
        $exchange = new-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://atlexmbx2/powershell -Authentication kerberos
        import-PSSession $exchange
    }
    
    PROCESS{
    
        foreach($File in $Photo)
        {
            $Identity = [System.IO.Path]::GetFileNameWithoutExtension($File.FullName)
            Import-RecipientDataProperty -Identity $Identity -Picture -FileData ([Byte[]]$(Get-Content -Path $File.FullName -Encoding Byte -ReadCount 0))
            $Identity
        }
    }
    
    • #19027
      Profile photo of Juli
      Juli
      Participant

      I am still not getting anything for the $Identity. I can see the items in the $Photo var, but $Identity is not being populated.

  • #19028
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Are you running the code that I posted, or has it been changed? I don't see how you can be getting a blank $Identity variable from the code in my last post.

  • #19029
    Profile photo of Juli
    Juli
    Participant

    I am running the code that you posted

  • #19030
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Then I'm not sure what's going on. You'll need to do some debugging (either by stepping through the code, or by adding some Write-Debug / Write-Verbose / etc statements.) See what the values of $File, $File.FullName, and $Identity are at the time that Import-RecipientDataProperty is about to be called.

  • #19031
    Profile photo of Juli
    Juli
    Participant

    This is the error message I get with your code: And I didn't retype it, I copied it.

    At line:10 char:84
    + ... -ConnectionUri http://atlex ...
    + ~
    The '<' operator is reserved for future use. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : RedirectionNotSupported

  • #19032
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Heh, I didn't even notice the forum software was doing that. It's translating the URL into an HTML element. It doesn't look that way when I edit the post.

    Just change that parameter back to its original value of: -ConnectionUri http://atlexmbx2/powershell , without all the HTML bits around it.

  • #19034
    Profile photo of Juli
    Juli
    Participant

    That worked! Thanks

You must be logged in to reply to this topic.