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 - Help Translating "NamesTranlsate" from VBS to Powershell

Reply
 
Old 11-30-2007   #1 (permalink)
Mark Holland


 
 

Help Translating "NamesTranlsate" from VBS to Powershell

Hello,

I am trying to create a script in Powershell as apposed to my standard
VBScript. One of the tasks I need to complete is to convert a sAMLoginName to
a distinguished name. In VB I used the following function:

Function GetLdapDN(sAMLoginName)

vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"

Set oTrans = CreateObject("NameTranslate")
oTrans.Init 3, vDNSDomain
oTrans.Set 1, vDNSDomain
vNetBIOSDomain = oTrans.Get(3)
oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
On Error Resume next
oTrans.Set 3, vNetBIOSDomain & sAMLoginName
GetLdapDN = oTrans.Get(1)

End Function

So I make an attempt to convert it to VB but it doesn't work - here's my
attempt...

Function GetLdapDN([string]$vNTName){

$vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"

$oTrans = New-Object -ComObject NameTranslate
$oTrans.Init 3,$vDNSDomain
$oTrans.Set 1, $vDNSDomain
$vNetBIOSDomain = $oTrans.Get(3)
$oTrans.Init 1, $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length - 1))
$oTrans.Set 3, $vNetBIOSDomain + $vNTName

$GetLdapDN = $oTrans.Get(1)

}

All I get is "Unexpected token '3' in expression or statement." Any ideas
would be gratefully recieved!!!

Many Thanks

Mark Holland


My System SpecsSystem Spec
Old 11-30-2007   #2 (permalink)
Jeff


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

On Nov 30, 4:51 pm, Mark Holland
<MarkHoll...@xxxxxx> wrote:
Quote:

> Hello,
>
> I am trying to create a script in Powershell as apposed to my standard
> VBScript. One of the tasks I need to complete is to convert a sAMLoginName to
> a distinguished name. In VB I used the following function:
>
> Function GetLdapDN(sAMLoginName)
>
> vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>
> Set oTrans = CreateObject("NameTranslate")
> oTrans.Init 3, vDNSDomain
> oTrans.Set 1, vDNSDomain
> vNetBIOSDomain = oTrans.Get(3)
> oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
> On Error Resume next
> oTrans.Set 3, vNetBIOSDomain & sAMLoginName
> GetLdapDN = oTrans.Get(1)
>
> End Function
>
> So I make an attempt to convert it to VB but it doesn't work - here's my
> attempt...
>
> Function GetLdapDN([string]$vNTName){
>
> $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>
> $oTrans = New-Object -ComObject NameTranslate
> $oTrans.Init 3,$vDNSDomain
> $oTrans.Set 1, $vDNSDomain
> $vNetBIOSDomain = $oTrans.Get(3)
> $oTrans.Init 1, $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length - 1))
> $oTrans.Set 3, $vNetBIOSDomain + $vNTName
>
> $GetLdapDN = $oTrans.Get(1)
>
> }
>
> All I get is "Unexpected token '3' in expression or statement." Any ideas
> would be gratefully recieved!!!
>
> Many Thanks
>
> Mark Holland
Mark,

Try using parentheses when you call NameTranslate methods:

$oTrans.Init( 3,$vDNSDomain )
$oTrans.Set( 1, $vDNSDomain )

Jeff
My System SpecsSystem Spec
Old 11-30-2007   #3 (permalink)
Mark Holland


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

Hi Jeff,

I tried that already but get alot of "Method invocation failed
because[System.__ComObject] doesn't contain a method named 'Init'." plus
various other errors....



"Jeff" wrote:
Quote:

> On Nov 30, 4:51 pm, Mark Holland
> <MarkHoll...@xxxxxx> wrote:
Quote:

> > Hello,
> >
> > I am trying to create a script in Powershell as apposed to my standard
> > VBScript. One of the tasks I need to complete is to convert a sAMLoginName to
> > a distinguished name. In VB I used the following function:
> >
> > Function GetLdapDN(sAMLoginName)
> >
> > vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
> >
> > Set oTrans = CreateObject("NameTranslate")
> > oTrans.Init 3, vDNSDomain
> > oTrans.Set 1, vDNSDomain
> > vNetBIOSDomain = oTrans.Get(3)
> > oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
> > On Error Resume next
> > oTrans.Set 3, vNetBIOSDomain & sAMLoginName
> > GetLdapDN = oTrans.Get(1)
> >
> > End Function
> >
> > So I make an attempt to convert it to VB but it doesn't work - here's my
> > attempt...
> >
> > Function GetLdapDN([string]$vNTName){
> >
> > $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
> >
> > $oTrans = New-Object -ComObject NameTranslate
> > $oTrans.Init 3,$vDNSDomain
> > $oTrans.Set 1, $vDNSDomain
> > $vNetBIOSDomain = $oTrans.Get(3)
> > $oTrans.Init 1, $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length - 1))
> > $oTrans.Set 3, $vNetBIOSDomain + $vNTName
> >
> > $GetLdapDN = $oTrans.Get(1)
> >
> > }
> >
> > All I get is "Unexpected token '3' in expression or statement." Any ideas
> > would be gratefully recieved!!!
> >
> > Many Thanks
> >
> > Mark Holland
>
> Mark,
>
> Try using parentheses when you call NameTranslate methods:
>
> $oTrans.Init( 3,$vDNSDomain )
> $oTrans.Set( 1, $vDNSDomain )
>
> Jeff
>
My System SpecsSystem Spec
Old 11-30-2007   #4 (permalink)
Brandon Shell [MVP]


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

Two things...

1) Try using psbase
$oTrans.psbase.Init( 3,$vDNSDomain )
$oTrans.psbase.Set( 1, $vDNSDomain )

2) There is much easier way to accomplish your goal (cant test these, but
they should work.)
(new-object
System.DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")).distinguishedname
or with Quest CMDLets
Get-QADUser $logon | %{$_.distinguishedname}

"Jeff" <jeff.hillman@xxxxxx> wrote in message
news:909225c8-311d-44ca-b87a-895cd7e29e02@xxxxxx
Quote:

> On Nov 30, 4:51 pm, Mark Holland
> <MarkHoll...@xxxxxx> wrote:
Quote:

>> Hello,
>>
>> I am trying to create a script in Powershell as apposed to my standard
>> VBScript. One of the tasks I need to complete is to convert a
>> sAMLoginName to
>> a distinguished name. In VB I used the following function:
>>
>> Function GetLdapDN(sAMLoginName)
>>
>> vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>>
>> Set oTrans = CreateObject("NameTranslate")
>> oTrans.Init 3, vDNSDomain
>> oTrans.Set 1, vDNSDomain
>> vNetBIOSDomain = oTrans.Get(3)
>> oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
>> On Error Resume next
>> oTrans.Set 3, vNetBIOSDomain & sAMLoginName
>> GetLdapDN = oTrans.Get(1)
>>
>> End Function
>>
>> So I make an attempt to convert it to VB but it doesn't work - here's my
>> attempt...
>>
>> Function GetLdapDN([string]$vNTName){
>>
>> $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>>
>> $oTrans = New-Object -ComObject NameTranslate
>> $oTrans.Init 3,$vDNSDomain
>> $oTrans.Set 1, $vDNSDomain
>> $vNetBIOSDomain = $oTrans.Get(3)
>> $oTrans.Init 1, $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length - 1))
>> $oTrans.Set 3, $vNetBIOSDomain + $vNTName
>>
>> $GetLdapDN = $oTrans.Get(1)
>>
>> }
>>
>> All I get is "Unexpected token '3' in expression or statement." Any ideas
>> would be gratefully recieved!!!
>>
>> Many Thanks
>>
>> Mark Holland
>
> Mark,
>
> Try using parentheses when you call NameTranslate methods:
>
> $oTrans.Init( 3,$vDNSDomain )
> $oTrans.Set( 1, $vDNSDomain )
>
> Jeff
My System SpecsSystem Spec
Old 11-30-2007   #5 (permalink)
Mark Holland


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

Hi Brandon,

Thanks for the response - you've really pointed me in the right direction
but I'm not quite there:

1) If I do $a = (new-object
DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")) | get-member
there isn't a distinguishedname property. I guess this is because I'm not
binding to the LDAP object??

2) I don't have Quest CMDLets

3) I've tried doing the NameTranslate straight from the commandline

$oTrans = new-object -comobject "NameTranslate"
$oTrans.psbase.Init( 3,"LDAP://DC=domain,DC=co,DC=uk")

But I get "Method invocation failed because
[System.Management.Automation.PSMemberSet] doesn't contain a method named
'Init'."

If I remember the quotes I get "Unexpected token '3' in expression or
statement."

Many Thanks once again

Mark holland


Quote:

> or with Quest CMDLets

"Brandon Shell [MVP]" wrote:
Quote:

> Two things...
>
> 1) Try using psbase
> $oTrans.psbase.Init( 3,$vDNSDomain )
> $oTrans.psbase.Set( 1, $vDNSDomain )
>
> 2) There is much easier way to accomplish your goal (cant test these, but
> they should work.)
> (new-object
> System.DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")).distinguishedname
> or with Quest CMDLets
> Get-QADUser $logon | %{$_.distinguishedname}
>
> "Jeff" <jeff.hillman@xxxxxx> wrote in message
> news:909225c8-311d-44ca-b87a-895cd7e29e02@xxxxxx
Quote:

> > On Nov 30, 4:51 pm, Mark Holland
> > <MarkHoll...@xxxxxx> wrote:
Quote:

> >> Hello,
> >>
> >> I am trying to create a script in Powershell as apposed to my standard
> >> VBScript. One of the tasks I need to complete is to convert a
> >> sAMLoginName to
> >> a distinguished name. In VB I used the following function:
> >>
> >> Function GetLdapDN(sAMLoginName)
> >>
> >> vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
> >>
> >> Set oTrans = CreateObject("NameTranslate")
> >> oTrans.Init 3, vDNSDomain
> >> oTrans.Set 1, vDNSDomain
> >> vNetBIOSDomain = oTrans.Get(3)
> >> oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
> >> On Error Resume next
> >> oTrans.Set 3, vNetBIOSDomain & sAMLoginName
> >> GetLdapDN = oTrans.Get(1)
> >>
> >> End Function
> >>
> >> So I make an attempt to convert it to VB but it doesn't work - here's my
> >> attempt...
> >>
> >> Function GetLdapDN([string]$vNTName){
> >>
> >> $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
> >>
> >> $oTrans = New-Object -ComObject NameTranslate
> >> $oTrans.Init 3,$vDNSDomain
> >> $oTrans.Set 1, $vDNSDomain
> >> $vNetBIOSDomain = $oTrans.Get(3)
> >> $oTrans.Init 1, $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length - 1))
> >> $oTrans.Set 3, $vNetBIOSDomain + $vNTName
> >>
> >> $GetLdapDN = $oTrans.Get(1)
> >>
> >> }
> >>
> >> All I get is "Unexpected token '3' in expression or statement." Any ideas
> >> would be gratefully recieved!!!
> >>
> >> Many Thanks
> >>
> >> Mark Holland
> >
> > Mark,
> >
> > Try using parentheses when you call NameTranslate methods:
> >
> > $oTrans.Init( 3,$vDNSDomain )
> > $oTrans.Set( 1, $vDNSDomain )
> >
> > Jeff
>
>
My System SpecsSystem Spec
Old 11-30-2007   #6 (permalink)
Brandon Shell [MVP]


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

You can download the quest CMDLets for free here

Try this... I was on the bus and couldnt test. I am now at my desk and this
worked for me

PS> (New-Object System.DirectoryServices.DirectorySearcher([ADSI]"","(&(objectcategory=user)(sAMAccountName=$logon))",@('distinguishedname'))).findone().properties.distinguishedname

for a less cryptic version

$Filter = "(&(objectcategory=user)(sAMAccountName=$logon))"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"",$filter,@('distinguishedname'))
$user = $Searcher.Findone()
$User.properties.distinguishedname


As for why the COM object is not working as expect.. I am not sure. What
happens if you do a Get-Member on the COM Object?

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

MH> Hi Brandon,
MH>
MH> Thanks for the response - you've really pointed me in the right
MH> direction but I'm not quite there:
MH>
MH> 1) If I do $a = (new-object
MH> DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")) |
MH> get-member there isn't a distinguishedname property. I guess this is
MH> because I'm not binding to the LDAP object??
MH>
MH> 2) I don't have Quest CMDLets
MH>
MH> 3) I've tried doing the NameTranslate straight from the commandline
MH>
MH> $oTrans = new-object -comobject "NameTranslate"
MH> $oTrans.psbase.Init( 3,"LDAP://DC=domain,DC=co,DC=uk")
MH> But I get "Method invocation failed because
MH> [System.Management.Automation.PSMemberSet] doesn't contain a method
MH> named 'Init'."
MH>
MH> If I remember the quotes I get "Unexpected token '3' in expression
MH> or statement."
MH>
MH> Many Thanks once again
MH>
MH> Mark holland
MH>
Quote:
Quote:

>> or with Quest CMDLets
>>
MH> "Brandon Shell [MVP]" wrote:
MH>
Quote:
Quote:

>> Two things...
>>
>> 1) Try using psbase
>> $oTrans.psbase.Init( 3,$vDNSDomain )
>> $oTrans.psbase.Set( 1, $vDNSDomain )
>> 2) There is much easier way to accomplish your goal (cant test these,
>> but
>> they should work.)
>> (new-object
>> System.DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")).
>> distinguishedname
>> or with Quest CMDLets
>> Get-QADUser $logon | %{$_.distinguishedname}
>> "Jeff" <jeff.hillman@xxxxxx> wrote in message
>> news:909225c8-311d-44ca-b87a-895cd7e29e02@xxxxxx
>> m...
>>
Quote:

>>> On Nov 30, 4:51 pm, Mark Holland
>>> <MarkHoll...@xxxxxx> wrote:
>>>> Hello,
>>>>
>>>> I am trying to create a script in Powershell as apposed to my
>>>> standard
>>>> VBScript. One of the tasks I need to complete is to convert a
>>>> sAMLoginName to
>>>> a distinguished name. In VB I used the following function:
>>>> Function GetLdapDN(sAMLoginName)
>>>>
>>>> vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>>>>
>>>> Set oTrans = CreateObject("NameTranslate")
>>>> oTrans.Init 3, vDNSDomain
>>>> oTrans.Set 1, vDNSDomain
>>>> vNetBIOSDomain = oTrans.Get(3)
>>>> oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
>>>> On Error Resume next
>>>> oTrans.Set 3, vNetBIOSDomain & sAMLoginName
>>>> GetLdapDN = oTrans.Get(1)
>>>> End Function
>>>>
>>>> So I make an attempt to convert it to VB but it doesn't work -
>>>> here's my attempt...
>>>>
>>>> Function GetLdapDN([string]$vNTName){
>>>>
>>>> $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>>>>
>>>> $oTrans = New-Object -ComObject NameTranslate
>>>> $oTrans.Init 3,$vDNSDomain
>>>> $oTrans.Set 1, $vDNSDomain
>>>> $vNetBIOSDomain = $oTrans.Get(3)
>>>> $oTrans.Init 1, $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length
>>>> - 1))
>>>> $oTrans.Set 3, $vNetBIOSDomain + $vNTName
>>>> $GetLdapDN = $oTrans.Get(1)
>>>>
>>>> }
>>>>
>>>> All I get is "Unexpected token '3' in expression or statement." Any
>>>> ideas would be gratefully recieved!!!
>>>>
>>>> Many Thanks
>>>>
>>>> Mark Holland
>>>>
>>> Mark,
>>>
>>> Try using parentheses when you call NameTranslate methods:
>>>
>>> $oTrans.Init( 3,$vDNSDomain )
>>> $oTrans.Set( 1, $vDNSDomain )
>>> Jeff
>>>

My System SpecsSystem Spec
Old 11-30-2007   #7 (permalink)
Brandon Shell [MVP]


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

by here.. I meant http://www.quest.com/activeroles-server/arms.aspx

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

BS> You can download the quest CMDLets for free here
BS>
BS> Try this... I was on the bus and couldnt test. I am now at my desk
BS> and this worked for me
BS>
PS>> (New-Object
PS>> System.DirectoryServices.DirectorySearcher([ADSI]"","(&(objectcateg
PS>> ory=user)(sAMAccountName=$logon))",@('distinguishedname'))).findone
PS>> ().properties.distinguishedname
PS>>
BS> for a less cryptic version
BS>
BS> $Filter = "(&(objectcategory=user)(sAMAccountName=$logon))"
BS>
BS> $Searcher = New-Object
BS> System.DirectoryServices.DirectorySearcher([ADSI]"",$filter,@('disti
BS> nguishedname'))
BS>
BS> $user = $Searcher.Findone()
BS>
BS> $User.properties.distinguishedname
BS>
BS> As for why the COM object is not working as expect.. I am not sure.
BS> What happens if you do a Get-Member on the COM Object?
BS>
BS> Brandon Shell
BS> ---------------
BS> Blog: http://www.bsonposh.com/
BS> PSH Scripts Project: www.codeplex.com/psobject
MH>> Hi Brandon,
MH>>
MH>> Thanks for the response - you've really pointed me in the right
MH>> direction but I'm not quite there:
MH>>
MH>> 1) If I do $a = (new-object
MH>> DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")) |
MH>> get-member there isn't a distinguishedname property. I guess this
MH>> is because I'm not binding to the LDAP object??
MH>>
MH>> 2) I don't have Quest CMDLets
MH>>
MH>> 3) I've tried doing the NameTranslate straight from the commandline
MH>>
MH>> $oTrans = new-object -comobject "NameTranslate"
MH>> $oTrans.psbase.Init( 3,"LDAP://DC=domain,DC=co,DC=uk")
MH>> But I get "Method invocation failed because
MH>> [System.Management.Automation.PSMemberSet] doesn't contain a method
MH>> named 'Init'."
MH>> If I remember the quotes I get "Unexpected token '3' in expression
MH>> or statement."
MH>>
MH>> Many Thanks once again
MH>>
MH>> Mark holland
MH>>
Quote:
Quote:
Quote:

>>> or with Quest CMDLets
>>>
MH>> "Brandon Shell [MVP]" wrote:
MH>>
Quote:
Quote:
Quote:

>>> Two things...
>>>
>>> 1) Try using psbase
>>> $oTrans.psbase.Init( 3,$vDNSDomain )
>>> $oTrans.psbase.Set( 1, $vDNSDomain )
>>> 2) There is much easier way to accomplish your goal (cant test
>>> these,
>>> but
>>> they should work.)
>>> (new-object
>>> System.DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon"))
>>> .
>>> distinguishedname
>>> or with Quest CMDLets
>>> Get-QADUser $logon | %{$_.distinguishedname}
>>> "Jeff" <jeff.hillman@xxxxxx> wrote in message
>>> news:909225c8-311d-44ca-b87a-895cd7e29e02@xxxxxx
>>> o
>>> m...
>>>> On Nov 30, 4:51 pm, Mark Holland
>>>> <MarkHoll...@xxxxxx> wrote:
>>>>> Hello,
>>>>>
>>>>> I am trying to create a script in Powershell as apposed to my
>>>>> standard
>>>>> VBScript. One of the tasks I need to complete is to convert a
>>>>> sAMLoginName to
>>>>> a distinguished name. In VB I used the following function:
>>>>> Function GetLdapDN(sAMLoginName)
>>>>> vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>>>>>
>>>>> Set oTrans = CreateObject("NameTranslate")
>>>>> oTrans.Init 3, vDNSDomain
>>>>> oTrans.Set 1, vDNSDomain
>>>>> vNetBIOSDomain = oTrans.Get(3)
>>>>> oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
>>>>> On Error Resume next
>>>>> oTrans.Set 3, vNetBIOSDomain & sAMLoginName
>>>>> GetLdapDN = oTrans.Get(1)
>>>>> End Function
>>>>> So I make an attempt to convert it to VB but it doesn't work -
>>>>> here's my attempt...
>>>>>
>>>>> Function GetLdapDN([string]$vNTName){
>>>>>
>>>>> $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
>>>>>
>>>>> $oTrans = New-Object -ComObject NameTranslate
>>>>> $oTrans.Init 3,$vDNSDomain
>>>>> $oTrans.Set 1, $vDNSDomain
>>>>> $vNetBIOSDomain = $oTrans.Get(3)
>>>>> $oTrans.Init 1,
>>>>> $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length
>>>>> - 1))
>>>>> $oTrans.Set 3, $vNetBIOSDomain + $vNTName
>>>>> $GetLdapDN = $oTrans.Get(1)
>>>>> }
>>>>>
>>>>> All I get is "Unexpected token '3' in expression or statement."
>>>>> Any ideas would be gratefully recieved!!!
>>>>>
>>>>> Many Thanks
>>>>>
>>>>> Mark Holland
>>>>>
>>>> Mark,
>>>>
>>>> Try using parentheses when you call NameTranslate methods:
>>>>
>>>> $oTrans.Init( 3,$vDNSDomain )
>>>> $oTrans.Set( 1, $vDNSDomain )
>>>> Jeff

My System SpecsSystem Spec
Old 11-30-2007   #8 (permalink)
Mark Holland


 
 

Re: Help Translating "NamesTranlsate" from VBS to Powershell

Thank you so much!!

Both your example and the quest cmdlets both worked! I didn't know they were
free!!

10/10 Have a good day

Mark

"Brandon Shell [MVP]" wrote:
Quote:

> by here.. I meant http://www.quest.com/activeroles-server/arms.aspx
>
> Brandon Shell
> ---------------
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
>
> BS> You can download the quest CMDLets for free here
> BS>
> BS> Try this... I was on the bus and couldnt test. I am now at my desk
> BS> and this worked for me
> BS>
> PS>> (New-Object
> PS>> System.DirectoryServices.DirectorySearcher([ADSI]"","(&(objectcateg
> PS>> ory=user)(sAMAccountName=$logon))",@('distinguishedname'))).findone
> PS>> ().properties.distinguishedname
> PS>>
> BS> for a less cryptic version
> BS>
> BS> $Filter = "(&(objectcategory=user)(sAMAccountName=$logon))"
> BS>
> BS> $Searcher = New-Object
> BS> System.DirectoryServices.DirectorySearcher([ADSI]"",$filter,@('disti
> BS> nguishedname'))
> BS>
> BS> $user = $Searcher.Findone()
> BS>
> BS> $User.properties.distinguishedname
> BS>
> BS> As for why the COM object is not working as expect.. I am not sure.
> BS> What happens if you do a Get-Member on the COM Object?
> BS>
> BS> Brandon Shell
> BS> ---------------
> BS> Blog: http://www.bsonposh.com/
> BS> PSH Scripts Project: www.codeplex.com/psobject
> MH>> Hi Brandon,
> MH>>
> MH>> Thanks for the response - you've really pointed me in the right
> MH>> direction but I'm not quite there:
> MH>>
> MH>> 1) If I do $a = (new-object
> MH>> DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon")) |
> MH>> get-member there isn't a distinguishedname property. I guess this
> MH>> is because I'm not binding to the LDAP object??
> MH>>
> MH>> 2) I don't have Quest CMDLets
> MH>>
> MH>> 3) I've tried doing the NameTranslate straight from the commandline
> MH>>
> MH>> $oTrans = new-object -comobject "NameTranslate"
> MH>> $oTrans.psbase.Init( 3,"LDAP://DC=domain,DC=co,DC=uk")
> MH>> But I get "Method invocation failed because
> MH>> [System.Management.Automation.PSMemberSet] doesn't contain a method
> MH>> named 'Init'."
> MH>> If I remember the quotes I get "Unexpected token '3' in expression
> MH>> or statement."
> MH>>
> MH>> Many Thanks once again
> MH>>
> MH>> Mark holland
> MH>>
Quote:
Quote:

> >>> or with Quest CMDLets
> >>>
> MH>> "Brandon Shell [MVP]" wrote:
> MH>>
Quote:
Quote:

> >>> Two things...
> >>>
> >>> 1) Try using psbase
> >>> $oTrans.psbase.Init( 3,$vDNSDomain )
> >>> $oTrans.psbase.Set( 1, $vDNSDomain )
> >>> 2) There is much easier way to accomplish your goal (cant test
> >>> these,
> >>> but
> >>> they should work.)
> >>> (new-object
> >>> System.DirectoryServices.DirectoryEntry("WinNT://$NBDomain/$Logon"))
> >>> .
> >>> distinguishedname
> >>> or with Quest CMDLets
> >>> Get-QADUser $logon | %{$_.distinguishedname}
> >>> "Jeff" <jeff.hillman@xxxxxx> wrote in message
> >>> news:909225c8-311d-44ca-b87a-895cd7e29e02@xxxxxx
> >>> o
> >>> m...
> >>>> On Nov 30, 4:51 pm, Mark Holland
> >>>> <MarkHoll...@xxxxxx> wrote:
> >>>>> Hello,
> >>>>>
> >>>>> I am trying to create a script in Powershell as apposed to my
> >>>>> standard
> >>>>> VBScript. One of the tasks I need to complete is to convert a
> >>>>> sAMLoginName to
> >>>>> a distinguished name. In VB I used the following function:
> >>>>> Function GetLdapDN(sAMLoginName)
> >>>>> vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
> >>>>>
> >>>>> Set oTrans = CreateObject("NameTranslate")
> >>>>> oTrans.Init 3, vDNSDomain
> >>>>> oTrans.Set 1, vDNSDomain
> >>>>> vNetBIOSDomain = oTrans.Get(3)
> >>>>> oTrans.Init 1, Left(vNetBIOSDomain, Len(vNetBIOSDomain) - 1)
> >>>>> On Error Resume next
> >>>>> oTrans.Set 3, vNetBIOSDomain & sAMLoginName
> >>>>> GetLdapDN = oTrans.Get(1)
> >>>>> End Function
> >>>>> So I make an attempt to convert it to VB but it doesn't work -
> >>>>> here's my attempt...
> >>>>>
> >>>>> Function GetLdapDN([string]$vNTName){
> >>>>>
> >>>>> $vDNSDomain = "LDAP://dc=domain,DC=co,DC=uk/"
> >>>>>
> >>>>> $oTrans = New-Object -ComObject NameTranslate
> >>>>> $oTrans.Init 3,$vDNSDomain
> >>>>> $oTrans.Set 1, $vDNSDomain
> >>>>> $vNetBIOSDomain = $oTrans.Get(3)
> >>>>> $oTrans.Init 1,
> >>>>> $vNetBIOSDomain.substring(0,($vNetBIOSDomain.length
> >>>>> - 1))
> >>>>> $oTrans.Set 3, $vNetBIOSDomain + $vNTName
> >>>>> $GetLdapDN = $oTrans.Get(1)
> >>>>> }
> >>>>>
> >>>>> All I get is "Unexpected token '3' in expression or statement."
> >>>>> Any ideas would be gratefully recieved!!!
> >>>>>
> >>>>> Many Thanks
> >>>>>
> >>>>> Mark Holland
> >>>>>
> >>>> Mark,
> >>>>
> >>>> Try using parentheses when you call NameTranslate methods:
> >>>>
> >>>> $oTrans.Init( 3,$vDNSDomain )
> >>>> $oTrans.Set( 1, $vDNSDomain )
> >>>> Jeff
>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
SOLVED! "Cannot load Contacts" "0x8004104E" "MSOE.DLL" Vista mail
Unwanted Multiple contacts in "To","CC","BCC" of email send catago Vista mail
Vista not wotking with "My Computer" or "Control Panel", "Screen Saver" Vista General
How can I add the icons "Delete", "Cut", "Copy" and "Paste" in Vis Vista file management
WM5 Sync with Vista "Windows Calender", "Contacts", and "Mail" Vista General


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