I have this code that gives me the user logged on name of a remote computer, but if i run the program in certaing computers, it returns with the error "Not Available Due Restrictions" which is set if the scope cant connect...

why it connects fine in some computers and other not..is there something i have to add to the code for it to run fine in any computer


this is the code

Dim connection As New ConnectionOptions
                        connection.Username = ("ADMIN")
                        connection.Password = ("something")
                        
                        connection.Authority = String.Format("ntlmdomain:MCMXNTEX55")
                        Dim scope As New ManagementScope("\\" & IPADDRESSH & "\root\CIMV2", connection)
                        Try
                            scope.Connect()
                            Dim query As New ObjectQuery("SELECT * FROM Win32_ComputerSystem")
                            Dim searcher As New ManagementObjectSearcher(scope, query)
                            Try
                                For Each queryObj As ManagementObject In searcher.Get()
                                    Dim usnm As String = queryObj("UserName")
                                                                    Next

                            Catch err As ManagementException
                                DataGridView1.Rows.Item(sum).Cells.Item(6).Value = "An error occurred"
                            Catch unauthorizedErr As System.UnauthorizedAccessException
                                DataGridView1.Rows.Item(sum).Cells.Item(6).Value = "Connection error"
                            End Try
                        Catch ex As Exception
                            DataGridView1.Rows.Item(sum).Cells.Item(6).Value = "Not Available Due Restrictions"
                        End Try

Dani AI

Generated

The behavior described by (works on some machines, fails with a “Not Available Due Restrictions” on others) is almost always an environment/permission issue rather than a bug in the client code. Common root causes are: the remote WMI/DCOM ports blocked by firewall, the supplied account not having remote/admin rights on that host, Windows Remote UAC filtering for local accounts (Vista and later), or WMI namespace permissions denying “Remote Enable.”

A practical diagnostic checklist:

  • The Windows Management Instrumentation service must be running on the target machine.
  • The host firewall should allow WMI/DCOM (RPC endpoint mapper TCP 135 plus RPC dynamic ports) or enable the built‑in “Windows Management Instrumentation (WMI‑In)” firewall rules.
  • The account used for the connection must exist on (or be trusted by) the remote machine and have sufficient rights: domain admin, local Administrator, or explicit WMI namespace permission. Check WMI Control (wmimgmt.msc) -> Properties -> Security -> Root\CIMV2 and ensure the account has “Remote Enable.”
  • On Vista/Server2008+ machines, local accounts are filtered by Remote UAC. The registry value LocalAccountTokenFilterPolicy at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System controls this behavior; setting it to 1 removes the filter (but reduces security).
  • Use a quick external test (wbemtest or a PowerShell WMI/CIM query) against the IP to separate network/authentication problems from code issues.

As pointed out, My.User.Name simply returns the user on the process host, so it only helps when the code runs locally on each machine. For centralized inventory scenarios, prefer a domain service account or use WinRM/CIM (WinRM uses fixed ports and is often easier to configure through corporate firewalls). Also consider enumerating logged‑on sessions (Win32_LoggedOnUser) if the goal is to find interactive users rather than relying on a single WMI property. Security best practice: avoid hardcoding credentials; use managed service accounts or secure credential stores.

Why not use the My namespace? My.User.Name .
It gives the name of the currently logged on user and would save you a whole shrew of coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.