Author Posts

September 23, 2014 at 4:11 am

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
}

}

September 23, 2014 at 5:41 am

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
    }
}

September 23, 2014 at 7:47 am

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

September 23, 2014 at 7:54 am

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.

September 23, 2014 at 7:54 am

I am running the code that you posted

September 23, 2014 at 7:56 am

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.

September 23, 2014 at 7:58 am

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

September 23, 2014 at 8:00 am

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.

September 23, 2014 at 8:06 am

That worked! Thanks