Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - copy files with AD attributes in name??

Reply
 
Old 09-14-2007   #1 (permalink)
steveb


 
 

copy files with AD attributes in name??

I have a folder full of employee pictures.
The name format is from active directory, as
"sn, givenname.JPG"

Now I have a web app that can not access the first and last name to display the picture.
It only knows the SAMAccountName.

Does anyone have a clue where to start with a script to copy these files from
"sn, givenname.JPG" to "SAMAccountName.jpg" ??


--
--
Steven

http://www.glimasoutheast.org





My System SpecsSystem Spec
Old 09-14-2007   #2 (permalink)
Shay Levi


 
 

Re: copy files with AD attributes in name??

# use -recurse (on get-childitem) to traverse sub dirs

get-childitem *.jpg | foreach {

# parse jpg file name to extract sn and givenname
$regex = ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.jpg$').matches($_.name)

$sn = $regex[0].groups['sn'].value
$gn = $regex[0].groups['gn'].value

# cant test it right now, place here the code to
# search AD for users that match $sn and $gn, get its samaccountname
$samaccountname = "shaylevi" # testing

rename-item $_ -newname "$samaccountname.jpg"
}





Shay
http://scriptolog.blogspot.com


Quote:

> I have a folder full of employee pictures.
> The name format is from active directory, as
> "sn, givenname.JPG"
> Now I have a web app that can not access the first and last name to
> display the picture. It only knows the SAMAccountName.
>
> Does anyone have a clue where to start with a script to copy these
> files from "sn, givenname.JPG" to "SAMAccountName.jpg" ??
>
> http://www.glimasoutheast.org
>

My System SpecsSystem Spec
Old 09-15-2007   #3 (permalink)
Brandon Shell


 
 

Re: copy files with AD attributes in name??

Here is a function to find a AD user with sn and GivenName
function Find-ADUserwithSN{
param($sn,$givenName)
$root = [ADSI]"LDAP://192.168.0.10"
$filter = "(&(objectcategory=user)(sn=$sn))"
$props = "sn","givenName","sAMAccountName"
$ds = new-Object
system.DirectoryServices.DirectorySearcher($root,$filter,$props)
$ds.findall() | ?{$_.properties.givenname -match $givenName} |
%{$_.properties.samaccountname}
}

"Shay Levi" <no@xxxxxx> wrote in message
news:8766a94473e48c9c54a78b4b1a8@xxxxxx
Quote:

># use -recurse (on get-childitem) to traverse sub dirs
>
> get-childitem *.jpg | foreach {
>
> # parse jpg file name to extract sn and givenname
> $regex = ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.jpg$').matches($_.name)
>
> $sn = $regex[0].groups['sn'].value
> $gn = $regex[0].groups['gn'].value
>
> # cant test it right now, place here the code to # search AD
> for users that match $sn and $gn, get its samaccountname
> $samaccountname = "shaylevi" # testing
>
> rename-item $_ -newname "$samaccountname.jpg"
> }
>
>
>
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

>> I have a folder full of employee pictures.
>> The name format is from active directory, as
>> "sn, givenname.JPG"
>> Now I have a web app that can not access the first and last name to
>> display the picture. It only knows the SAMAccountName.
>>
>> Does anyone have a clue where to start with a script to copy these
>> files from "sn, givenname.JPG" to "SAMAccountName.jpg" ??
>>
>> http://www.glimasoutheast.org
>>
>
>
My System SpecsSystem Spec
Old 09-15-2007   #4 (permalink)
Brandon Shell


 
 

Re: copy files with AD attributes in name??

btw.. I did a match instead of an equal so it maybe possible to get more
than one return... I did that so you can determine the error case on your
own. You may want to write those to a file or what have you.

"Brandon Shell" <tshell.mask@xxxxxx> wrote in message
news:%23kz9W309HHA.600@xxxxxx
Quote:

> Here is a function to find a AD user with sn and GivenName
> function Find-ADUserwithSN{
> param($sn,$givenName)
> $root = [ADSI]"LDAP://192.168.0.10"
> $filter = "(&(objectcategory=user)(sn=$sn))"
> $props = "sn","givenName","sAMAccountName"
> $ds = new-Object
> system.DirectoryServices.DirectorySearcher($root,$filter,$props)
> $ds.findall() | ?{$_.properties.givenname -match $givenName} |
> %{$_.properties.samaccountname}
> }
>
> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a94473e48c9c54a78b4b1a8@xxxxxx
Quote:

>># use -recurse (on get-childitem) to traverse sub dirs
>>
>> get-childitem *.jpg | foreach {
>>
>> # parse jpg file name to extract sn and givenname
>> $regex = ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.jpg$').matches($_.name)
>>
>> $sn = $regex[0].groups['sn'].value
>> $gn = $regex[0].groups['gn'].value
>>
>> # cant test it right now, place here the code to # search AD
>> for users that match $sn and $gn, get its samaccountname
>> $samaccountname = "shaylevi" # testing
>>
>> rename-item $_ -newname "$samaccountname.jpg"
>> }
>>
>>
>>
>>
>>
>> Shay
>> http://scriptolog.blogspot.com
>>
>>
>>
Quote:

>>> I have a folder full of employee pictures.
>>> The name format is from active directory, as
>>> "sn, givenname.JPG"
>>> Now I have a web app that can not access the first and last name to
>>> display the picture. It only knows the SAMAccountName.
>>>
>>> Does anyone have a clue where to start with a script to copy these
>>> files from "sn, givenname.JPG" to "SAMAccountName.jpg" ??
>>>
>>> http://www.glimasoutheast.org
>>>
>>
>>
>
My System SpecsSystem Spec
Old 09-17-2007   #5 (permalink)
steveb


 
 

Re: copy files with AD attributes in name??

Thanks for the regex.

I cheaped out and just used the following:
-----------------
# use -recurse (on get-childitem) to traverse sub dirs

get-childitem \\tg16\EmployeePhotos\@all\*.jpg | foreach {
$sn = ""
$gn = ""

# parse jpg file name to extract sn and givenname
$regex = ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.[Jj][Pp][Gg]$').matches($_.name)

$sn = $regex[0].groups['sn'].value
$gn = $regex[0].groups['gn'].value

copy-item $_ "\\tg16\EmployeePhotos\@firstlast\$gn$sn.jpg"
}
-----------------------

It was sufficient, since our AD login naming convention is "FirstName""LastName".

The more I use PowerShell, the more I like it.
It seems much more intuitive to me than VBS, of course, my Unix background has a little to do with it.

--
--
Steven

http://www.glimasoutheast.org



"Shay Levi" <no@xxxxxx> wrote in message news:8766a94473e48c9c54a78b4b1a8@xxxxxx
Quote:

># use -recurse (on get-childitem) to traverse sub dirs
>
> get-childitem *.jpg | foreach {
>
> # parse jpg file name to extract sn and givenname
> $regex = ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.jpg$').matches($_.name)
>
> $sn = $regex[0].groups['sn'].value
> $gn = $regex[0].groups['gn'].value
>
> # cant test it right now, place here the code to # search AD for users that match $sn and $gn, get its samaccountname
> $samaccountname = "shaylevi" # testing
>
> rename-item $_ -newname "$samaccountname.jpg"
> }
>
>
>
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

>> I have a folder full of employee pictures.
>> The name format is from active directory, as
>> "sn, givenname.JPG"
>> Now I have a web app that can not access the first and last name to
>> display the picture. It only knows the SAMAccountName.
>>
>> Does anyone have a clue where to start with a script to copy these
>> files from "sn, givenname.JPG" to "SAMAccountName.jpg" ??
>>
>> http://www.glimasoutheast.org
>>
>
>

My System SpecsSystem Spec
Old 09-17-2007   #6 (permalink)
Shay Levi


 
 

Re: copy files with AD attributes in name??

BTW, the regex is not case sensitive so "jpg" is much more understanable

Shay
http://scriptolog.blogspot.com


Quote:

> Thanks for the regex.
>
> I cheaped out and just used the following:
> -----------------
> # use -recurse (on get-childitem) to traverse sub dirs
> get-childitem \\tg16\EmployeePhotos\@all\*.jpg | foreach {
> $sn = ""
> $gn = ""
> # parse jpg file name to extract sn and givenname
> $regex =
> ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.[Jj][Pp][Gg]$').matches($_.name
> )
> $sn = $regex[0].groups['sn'].value
> $gn = $regex[0].groups['gn'].value
> copy-item $_ "\\tg16\EmployeePhotos\@firstlast\$gn$sn.jpg"
> }
> -----------------------
> It was sufficient, since our AD login naming convention is
> "FirstName""LastName".
>
> The more I use PowerShell, the more I like it.
> It seems much more intuitive to me than VBS, of course, my Unix
> background has a little to do with it.
> http://www.glimasoutheast.org
>
> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a94473e48c9c54a78b4b1a8@xxxxxx
>
Quote:

>> # use -recurse (on get-childitem) to traverse sub dirs
>>
>> get-childitem *.jpg | foreach {
>>
>> # parse jpg file name to extract sn and givenname
>> $regex =
>> ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.jpg$').matches($_.name)
>> $sn = $regex[0].groups['sn'].value
>> $gn = $regex[0].groups['gn'].value
>> # cant test it right now, place here the code to # search AD for
>> users that match $sn and $gn, get its samaccountname
>>
>> $samaccountname = "shaylevi" # testing
>>
>> rename-item $_ -newname "$samaccountname.jpg"
>> }
>> Shay
>> http://scriptolog.blogspot.com
Quote:

>>> I have a folder full of employee pictures.
>>> The name format is from active directory, as
>>> "sn, givenname.JPG"
>>> Now I have a web app that can not access the first and last name to
>>> display the picture. It only knows the SAMAccountName.
>>> Does anyone have a clue where to start with a script to copy these
>>> files from "sn, givenname.JPG" to "SAMAccountName.jpg" ??
>>>
>>> http://www.glimasoutheast.org
>>>

My System SpecsSystem Spec
Old 09-17-2007   #7 (permalink)
Shay Levi


 
 

Re: copy files with AD attributes in name??

Sorry, I was wrong. It is case sensetive.

Shay
http://scriptolog.blogspot.com


Quote:

> BTW, the regex is not case sensitive so "jpg" is much more
> understanable
>
> Shay
> http://scriptolog.blogspot.com
Quote:

>> Thanks for the regex.
>>
>> I cheaped out and just used the following:
>> -----------------
>> # use -recurse (on get-childitem) to traverse sub dirs
>> get-childitem \\tg16\EmployeePhotos\@all\*.jpg | foreach {
>> $sn = ""
>> $gn = ""
>> # parse jpg file name to extract sn and givenname
>> $regex =
>> ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.[Jj][Pp][Gg]$').matches($_.nam
>> e
>> )
>> $sn = $regex[0].groups['sn'].value
>> $gn = $regex[0].groups['gn'].value
>> copy-item $_ "\\tg16\EmployeePhotos\@firstlast\$gn$sn.jpg"
>> }
>> -----------------------
>> It was sufficient, since our AD login naming convention is
>> "FirstName""LastName".
>> The more I use PowerShell, the more I like it.
>> It seems much more intuitive to me than VBS, of course, my Unix
>> background has a little to do with it.
>> http://www.glimasoutheast.org
>> "Shay Levi" <no@xxxxxx> wrote in message
>> news:8766a94473e48c9c54a78b4b1a8@xxxxxx
Quote:

>>> # use -recurse (on get-childitem) to traverse sub dirs
>>>
>>> get-childitem *.jpg | foreach {
>>>
>>> # parse jpg file name to extract sn and givenname
>>> $regex =
>>> ([regex]'^(?<sn>[^,]*),\s(?<gn>[^.]*)\.jpg$').matches($_.name)
>>> $sn = $regex[0].groups['sn'].value
>>> $gn = $regex[0].groups['gn'].value
>>> # cant test it right now, place here the code to # search AD for
>>> users that match $sn and $gn, get its samaccountname
>>> $samaccountname = "shaylevi" # testing
>>>
>>> rename-item $_ -newname "$samaccountname.jpg"
>>> }
>>> Shay
>>> http://scriptolog.blogspot.com
>>>> I have a folder full of employee pictures.
>>>> The name format is from active directory, as
>>>> "sn, givenname.JPG"
>>>> Now I have a web app that can not access the first and last name to
>>>> display the picture. It only knows the SAMAccountName.
>>>> Does anyone have a clue where to start with a script to copy these
>>>> files from "sn, givenname.JPG" to "SAMAccountName.jpg" ??
>>>> http://www.glimasoutheast.org
>>>>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
'Recent Files' on Start Menu has lost special attributes Vista installation & setup
Files attributes Read Only in Program files Vista file management
Searching for files by attributes Vista file management
copy-item changing files attributes on network copy failures PowerShell
Searching, sorting, and excluding files by attributes Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46