Help! Convert vbScript to Powershell

I'm having problems with the following task:

I need to run a report every 30 mins or so to check who has open connections to a directory on a file server. I need to be able to
do a generic report of usernames that are accessing one top level folder or any of its child directories. But I also need to run the same
thing against another file server and I need a full report of users against a top level folder AND all of it's subdirectories.

Currently I am able to provide a list of usernames and file paths using this vbScript and piping it into a file:

Set objConnection = GetObject("WinNT://emea/MyServer/LanmanServer")
Set colResources = objConnection.Resources
For Each objResource in colResources
    Wscript.Echo "Path: " & objResource.Path & VbTab & "User: " & objResource.User
    Wscript.Echo
Next

Open in new window


Don't worry about the overall task. My problem is that I want to convert this vbScript to native Powershell.
But I'm struggling with setting up  the connection! How do I convert this bit:

Set objConnection = GetObject("WinNT://emea/MyServer/LanmanServer")

Open in new window


Unfortunately the target server is not a Windows box, so it doesn't understand the [adsi] binding from Powershell (I don't think!).
But I also understand that I should be able to leverage the underlying .net or something that both Powershell and vbScript share?

Can anyone assist?
LVL 3
Tommy_CooperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
[ADSI] "WinNT://emea/MyServer/LanmanServer"

Open in new window

should also work for non-Windows systems. It is the same as [DirectoryServices.DirectoryEntry] and uses LDAP. If it does not work, try
New-Object DirectoryServices.DirectoryEntry "LDAP://emea/MyServer/LanmanServer"

Open in new window

0
Tommy_CooperAuthor Commented:
Hello Qlemo

Thanks for your response.

Undoftunately this doesn't work. This is the output if I create a variable for that new-object and look at its members. Then if I try to inspect the object:
PS C:\Users\JustMe> $conn = New-Object DirectoryServices.DirectoryEntry "LDAP://emea/MydumbServer/LanmanServer"

PS C:\Users\JustMe> $conn |gm


   TypeName: System.DirectoryServices.DirectoryEntry

Name                        MemberType Definition                                                                                   
----                        ---------- ----------                                                                                   
ConvertDNWithBinaryToString CodeMethod static string ConvertDNWithBinaryToString(psobject deInstance, psobject dnWithBinaryInstance)
ConvertLargeIntegerToInt64  CodeMethod static long ConvertLargeIntegerToInt64(psobject deInstance, psobject largeIntegerInstance)   



PS C:\Users\JustMe> $conn
format-default : The following exception occurred while retrieving member "distinguishedName": "An invalid dn syntax has been specified.
"
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand

Open in new window

In my unknowledgeable opinion, this is because the NetApp filers don't like the LDAP connection string. I belive that as in the vbScript, they will only respond to a WinNt connection string that I suspect is using RPC to bind.

With that in mind, I did try a similar test using the WinNt string:
PS C:\Users\JustMe> $conn = New-Object DirectoryServices.DirectoryEntry "WinNT://emea/MyDumbServer/LanmanServer"

PS C:\Users\JustMe> $conn | gm


   TypeName: System.DirectoryServices.DirectoryEntry

Name                        MemberType Definition                                                                                   
----                        ---------- ----------                                                                                   
ConvertDNWithBinaryToString CodeMethod static string ConvertDNWithBinaryToString(psobject deInstance, psobject dnWithBinaryInstance)
ConvertLargeIntegerToInt64  CodeMethod static long ConvertLargeIntegerToInt64(psobject deInstance, psobject largeIntegerInstance)   



PS C:\Users\JustMe> $conn
format-default : The following exception occurred while retrieving member "path": "The remote procedure call failed.
"
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
 

Open in new window


Notice the different error when trying to look at the object.

I'm assuming the MemberType is created by the [DirectoryServices.DirectoryEntry]. Is there something  like this that we could use that behaves exactly like the vbScript:

GetObject("WinNT://emea/MyServer/LanmanServer")
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Both objects are ok, but incomplete. Format-Default tries to format according to the type, and fails. So, don't use format-default here.
You should look at ALL members first:
$connWNT  = New-Object DirectoryServices.DirectoryEntry "WinNT://emea/MyDumbServer/LanmanServer"
$connLDAP = New-Object DirectoryServices.DirectoryEntry "LDAP://emea/MydumbServer/LanmanServer"
$connWNT | gm *
$connWNT | fl *
$connLDAP | gm *
$connWNT | fl *

Open in new window

This will also show "hidden" properties.
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Tommy_CooperAuthor Commented:
OK - That is a little better. Except for the vbScript issue of case! WinNt nneds to be WinNt and and not WinNT!

But I think we are still far off. Now both methods show the same members. But they are all blank. They do look like Active Directory type members;

PS C:\Users\justMe> $connWNT | fl *


AuthenticationType : 
Children           : 
Guid               : 
ObjectSecurity     : 
Name               : 
NativeGuid         : 
NativeObject       : 
Parent             : 
Password           : 
Path               : 
Properties         : 
SchemaClassName    : 
SchemaEntry        : 
UsePropertyCache   : 
Username           : 
Options            : 
Site               : 
Container          : 

Open in new window

So I'm not sure if this is doing exactly what I want it to.
Again, (in my simple mind!) looking at the vbScript, I can see that I'm pulling back properties named 'path' and 'user'. These obviously don't exist in the output from: DirectoryServices.DirectoryEntry

Obviously, I don't really know what I'm talking about, so I do hope this makes some kind of sense!
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
? WinNT is correct, not WinNt, unless NetApp has an implementation issue. Which might be the case and reason for all that.
The direct translation of the VB code is (and should work, and that is the end of my wisdom):
$objConnection = New-Object DirectoryServices.DirectoryEntry "WinNT://emea/MyDumbServer/LanmanServer"
foreach ($objResource in $objConnection.Resources)
{
    Write-Host "Path: $($objResource.Path)`tUser: $($objResource.User)"
}

Open in new window

0
Tommy_CooperAuthor Commented:
Thank you Qlemo for your time and effort.

Anyone else have any idea why this may not work?

Correction.... Anyone know how to get it working?!

As I said in the beginning, the vbScript works just fine. And I thought we could ‘port’ just about anything due to the underlying architecture of the Windows OS/scripting engine and powershell.

I am convinced that this should be possible. Sadly, my organisation is huge and I will probably figure this out before getting the Storage team to implement the NetApp modules for PoSH!

If only I had a filer to test against!!
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
So I take it you tried my last code, and it still does not work?
0
Tommy_CooperAuthor Commented:
Sorry! I missed that fact!

This is what I get when just running the script:
PS C:\temp> .\try.ps1
Path:   User:
PS C:\temp>

Open in new window


Nope, sadly, same error when examining the object:
PS C:\temp> $objConnection = New-Object DirectoryServices.DirectoryEntry "WinNT:
//emea/MyDumbServer/LanmanServer"
PS C:\temp> $objConnection |gm


   TypeName: System.DirectoryServices.DirectoryEntry

Name                        MemberType Definition
----                        ---------- ----------
ConvertDNWithBinaryToString CodeMethod static string ConvertDNWithBinaryToSt...
ConvertLargeIntegerToInt64  CodeMethod static long ConvertLargeIntegerToInt6...


PS C:\temp> $objConnection |fl *


fl : The remote procedure call failed.
At line:1 char:17
+ $objConnection |fl *
+                 ~~~~
    + CategoryInfo          : NotSpecified: (:) [Format-List], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Micr
   osoft.PowerShell.Commands.FormatListCommand

PS C:\temp>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That is really strange, there should be no difference between VBS and PS, with one exception - GetObject can retrieve an existing instance (e.g. of Excel), while New-Object always creates a new one. But there is no GetObject in PS.
0
Tommy_CooperAuthor Commented:
Question not answered. Solution not found.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.