header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Use returned data for a variable
Last Post 19 Feb 2008 03:27 AM by JasonH. 11 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JasonHUser is Offline
New Member
New Member
Posts:9

--
12 Feb 2008 11:17 PM  

I'm new to Powershell (and scripting in general) so I hope this is an easy question to answer for someone with some experience. However because my question involves the use of a non-standard Powershell cmdlet, this may not be that easy.

I'm attempting to write a Powershell script that will query an OpenLDAP directory for informatin on a user and return the value of the mail attribute from OpenLDAP and insert that value into a corresponding Windows AD user object's WindowsEmailAddress attribute.

I came across /n Software's NetCmdLet package and am using the get-ldap command to get the mail attribute from OpenLDAP for users.  The results for a query for a user are:

objectClass                : {top, person, organizationalPerson, inetOrgPerson...}
cn                         : {User Name}
departmentNumber           : {Dept}
displayName                : {User Name}
employeeType               : {Employee}
facsimileTelephoneNumber   : {+### ### ####}
givenName                  : {User}
mail                       : {nobody@gmail.com}
o                          : {Your Organization Name Here}

I'd really like to get the nobody@gmail.com information, sans curly braces, to input into a variable so I can put that into the WindowsEmailAddress attribute for the user object in AD. 

Any idea how to parse the results of that query and get just the mail attribute? 

Thanks a lot!

Jason

 

smurawskiUser is Offline
New Member
New Member
Posts:46

--
12 Feb 2008 11:56 PM  
Jason,

Depending on how the rest of your script looks, you could do it a couple of ways, but I think this is easiest.

This is how I would start:
$MyResult of .mail -replace "{|}" #This will remove both of the curly braces
Steven Murawski
Co-Host - Mind of Root (www.mindofroot.com)
Host - PowerShell Basics (powershell-basics.com)
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
13 Feb 2008 01:19 AM  
It is entirely possible that the results returned are an array and that's how powershell is displaying that. Very easy to test.

PS> $ldapquery = get-ldap
PS> $ldapquery.mail[0]

The [0] is array notation for returning the first item. This will most likely be your email address.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
13 Feb 2008 01:20 AM  
ARGH. That is $ldapquery.mail [ 0 ]
left-bracket
0
right-bracket
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
JasonHUser is Offline
New Member
New Member
Posts:9

--
13 Feb 2008 02:55 PM  

Thank you both for the quick replies!  I'll give the suggestions a try and reply again when I've made some progress. Given my scripting skills that could still be a little while but this is awesome to have a lead on this.  Thanks!

JasonHUser is Offline
New Member
New Member
Posts:9

--
14 Feb 2008 05:56 PM  

Thanks again for the replies!  Halr9000, your suggestion that the ldapquery was returning an array for the mail attribute was correct and very helpful.  I've got the script setup in a very basic way that works if I pass an argument of username when I run the script but I'd like to run it against an OU where a number of mailbox-enabled users exist so that we can change the default Exchange smtp address for all the users in the OU.  (Exchange Email Address Policy is not an option for us.)  So I'm trying to come up with a for-each loop to do this but as I've stated before, my programming skills are close to null. 
Any suggestions?  The scripts are below:

This works for a single user when the username is passed as an argument when the script is run:

$user = $args[ 0 ]
$ldapquery = get-ldap -server "ldap.contoso.com" -dn "ou=people,dc=contoso,dc=com" -search "employeeNumber=$user" -attr
$ldapquery.mail[ 0 ]
Set-Mailbox -Identity $user -PrimarySmtpAddress $ldapquery.mail[ 0 ]

This is my attempt at a for-each loop (which does not work):

foreach ($x in Get-Mailbox -OrganizationalUnit "ou=employees,dc=ad,dc=contoso,dc=com")  {
    $ldapquery = get-ldap -server "ldap.contoso.com" -dn "ou=people,dc=contoso,dc=com" -search "employeeNumber=$x.Name" -attr
    $ldapquery.mail[ 0 ]
Set-Mailbox -Identity $user -PrimarySmtpAddress $ldapquery.mail[ 0 ]
}

The error returned for each user object in the OU is Cannot index into a null array.  +     $ldapquery.mail[ 0 <<<< ]

(Meaning if there are four user objects in the OU, I get four of the errors.)  I'm still reading and learning with Powershell so I'm trying loads of things trying to figure out for-each loops....

Thanks a lot!  
Jason

kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
14 Feb 2008 07:16 PM  
I apoligise in advance if I'm not being helpfull. I also don't have the Get-Mailbox CMDlet, but I'll try a suggestion using another CMDlet instead.

Try moving your Get-Mail statement up on line like so.

$GottenMailbox = Get-Mailbox -OrganizationalUnit "ou=employees,dc=ad,dc=contoso,dc=com"
foreach ($Get in $GottenMailbox) { #do your stuff here}

It works for me using Get-Process as shown below.


$Y=Get-process
foreach ($A in $Y)
    {$A.cpu | out-host}
My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
15 Feb 2008 04:30 PM  
Sometimes I find when I am running a command that I am expecting to see returned a list of objects when in fact ony one item is returned. In this case there is no array of objects its just one item. For debugging purposes if you break your complex commands into simpler parts you can then examine these parts easier when things go wrong.

For instance you could try $GottenMailbox | gm, or $GottenMailbox[0] | gm, and $GottenMailbox[1] | gm, etc. etc. etc.
My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
15 Feb 2008 04:34 PM  
My apologies once again. I'm still getting use to these code tags.

$GottenMailbox | gm 
$GottenMailbox[0] | gm 
$GottenMailbox[1] | gm

etc. etc. etc.
My blog: http://blogs.powershellcentral.com/kscriss/
JasonHUser is Offline
New Member
New Member
Posts:9

--
15 Feb 2008 09:40 PM  

Kscriss, thanks for the replies! Much appreciated. I had to look up the 'gm' alias you suggested to find it's short for get-member.  That's a good tip to pipe through that to see the results.  I was also using a write-host $variable command after some of the lines to make sure I was getting the results I expected. 

I worked around a bit more and figured out the for-each loop syntax a bit better and the code below, while still rough, is doing the trick for me. Now to just add some code at the start to ask the script executer for the OU name rather than having to hard code it into the script....

# Get mailbox information for all users in the OU. Store results in $user
$user = Get-Mailbox -OrganizationalUnit "ou=finance,dc=ad,dc=contoso,dc=com"

# For-each loop to first get only username from get-mailbox command above and then put that in $uname
foreach ($mail in $user) {
$uname = $mail.Name

# Write usernames to screen so we see who we're dealing with
Write-host $uname

# Query OpenLdap for username to retrieve mail attribute. Store mail in $smtp (really to prevent mail address from being displayed on screen
$ldapquery = get-ldap -server "ldap.contoso.com" -dn "ou=people,dc=contoso,dc=com" -search "employeeNumber=$uname" -attr
$smtp = $ldapquery.mail[ 0 ]

# Show in console the e-mail address
Write-host $smtp

# Finally, update the Exchange user default SMTP address with mail address from OpenLDAP
Set-Mailbox -Identity $uname -PrimarySmtpAddress $smtp
}

 p.s. How do I make text appear in the code text box like Kscriss has done?  I'm using the "code" style in the editor but it seems to do nothing.

Thanks!       Jason

kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
18 Feb 2008 03:32 PM  
Its a tag. I'm not very good at it yet. It goes like this.

less-than-sign Code greater-than-sign
Type your stuff here
less-than-sign / Code greater-than-sign

If you do it right it should look like this.

Type your stuff here


This method seems to only work for me in the Quick Reply.
My blog: http://blogs.powershellcentral.com/kscriss/
JasonHUser is Offline
New Member
New Member
Posts:9

--
19 Feb 2008 03:27 AM  
Thanks kscriss! I'll try it out here in the quick reply:

This is code. Thanks.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer