Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get primary server name using vbscript

Status
Not open for further replies.

tbscmgi

MIS
Sep 17, 2003
66
US
To All-
I'm trying to get the primary server name. I have 2 server, a backup and a primary per location.
I'm getting the backup server name instead of the primary.
The last 3 digits of the ip address tell identify the server, 195 is the primary.

below is the script.

Thanks for any help I can get.
Tony

Sub ShowSVRIPAddress

strComputer = "."

Set objIPaWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colIPaItems = objIPaWMIService.ExecQuery("Select * from Win32_NTDomain",,48)
For Each IPAobjItem in colIPaItems
tmpsvripaddr = IPAobjItem.DomainControllerAddress
if Right(tmpsvripaddr,3) > 0 then
svripaddr = Left(tmpsvripaddr,(Len (tmpsvripaddr)-3)) & 195
svripaddr1 = Right(svripaddr,(Len(svripaddr)-2))
if Right(svripaddr,3) = 195 then
strComputer = svripaddr1
ShowSVRName strComputer
end if
end if
next
set objIPaWMIService = Nothing
End Sub
'============================================================================
'
' S h o w S V R N a m e
' =======================
'
'
Sub ShowSVRName(svripaddr1)
Set objSVRnWMIService = GetObject( _
"winmgmts:{impersonationLevel=impersonate," _
& "authenticationLevel=pktPrivacy}!root/cimv2")

Set colSVRnItems = objSVRnWMIService.ExecQuery ("Select * from Win32_NTDomain",,48)
For Each SVRNobjItem in colSVRnItems
tmpsvrname = SVRNobjItem.DomainControllerName
next
set objSVRnWMIService = Nothing
End Sub
 
Does this provide the info?
Dim objSysInfo

Set objSysInfo = CreateObject("ADSystemInfo")
WScript.Echo objSysInfo.PDCRoleOwner

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
[1]
> if Right(tmpsvripaddr,3) > 0 then
> svripaddr = Left(tmpsvripaddr,(Len (tmpsvripaddr)-3)) & 195
> svripaddr1 = Right(svripaddr,(Len(svripaddr)-2))
> if Right(svripaddr,3) = 195 then

You append "195" to a string, then you ask whether that resultant string ends with "195". That is essential what you are doing.
[2]
>Sub ShowSVRName(svripaddr1)
You have not run out of name. Why choose a variable in global scope with respect to the sub?
[3]
>Set objSVRnWMIService = GetObject( _
> "winmgmts:{impersonationLevel=impersonate," _
> & "authenticationLevel=pktPrivacy}!root/cimv2")

Where do you bind to? Does svripaddr matter no more?

 
I'm trying to get the server name for the IP address ending in 195. that why I'm passing the IP Address.
The first sub get the IP Address and the second sub get the server name for that IP Address.


Thanks
Tony
 
Why don't you just change your query to be something like this?

"Select * From Win32_NTDomain Where DomainControllerAddress Like '\\%.%.%.195'"

Then you only need one WMI query to get the information you need.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thank you.
That work great. when I try 196 it not giving me anything.
196 is the backup server.
 
Do you need to retrieve both? If so try this query:

"Select * From Win32_NTDomain Where DomainControllerAddress Like '\\%.%.%.195' Or DomainControllerAddress Like '\\%.%.%.196'"

in the for...next something like

If Right(strDCAddr, 3) = "195" Then
WScript.Echo "PDC: " & objItem.DomainControllerName
Elseif Right(strDCAddr, 3) = "196" Then
WScript.Echo "BDC: " & objItem.DomainControllerName
End If

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
tsuji,

This is actually quite valid if you are connecting to WMI on the local machine.

Code:
    Set objSVRnWMIService = GetObject( _
        "winmgmts:{impersonationLevel=impersonate," _
        & "authenticationLevel=pktPrivacy}!root/cimv2")

In fact, since the default level of impersonation is "impersonate" and the default namespace is "root\cimv2" (on Win2k and above I believe) you can actually get way with this.

Code:
Set objIPaWMIService = GetObject("winmgmts:\\")
    Set colIPaItems = objIPaWMIService.ExecQuery("Select * from Win32_NTDomain")

As you can see, no namespace or computer name or impersonation level is defined. The only reason you see many examples where the computer name and namespace are used is because it is possible to set the default namespace to something other than root\cimv2 in which case you would need to provide a computer name and namespace (for the class being used).

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
tbscmgi,

So to be on the safe side, your WMI connection should at least be something like this. You used it in your ShowSVRIPAddress Sub, but not in your ShowSVRName Sub.

Code:
GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
>This is actually quite valid if you are connecting to WMI on the local machine.
Sure. But are you interested in connect to local m/c?!

As you seem to not understand why the question must be answered rather than ducked with irrelevance, this is the least what you should do.

>Set objSVRnWMIService = GetObject( _
> "winmgmts:{impersonationLevel=impersonate," _
> & "authenticationLevel=pktPrivacy}!root/cimv2")

[tt]
Set objSVRnWMIService = GetObject( _
"winmgmts:{impersonationLevel=impersonate," _
& "authenticationLevel=pktPrivacy}![red]\\" & svripaddr1 & "\[/red]root\cimv2")[/tt]

Without using the argument you intend to pass, why pass anything?

Every time you said it works. I don't buy it. One time you said you get the backup server and the second time you said you get the primary server. The reason is each time you run the deficient script on the respective server.
 
Oh, I see what you're getting at and you are correct. Sorry if I misunderstood what you were getting at. This is why a single WMI query was suggested...I didn't see much use of the second sub, even if the parameter being passed was used.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
tsuji & dm4ever-
Thank you for all of your help.

The reason I use the following code is because I was getting permission error.

Set objSVRnWMIService = GetObject( _
"winmgmts:{impersonationLevel=impersonate," _
& "authenticationLevel=pktPrivacy}!root/cimv2")

This script is run from a local machine, but want the
information from the server it is connected to.

I pass the parameter as you suggest and it work find,
for both IP Address.

Thanks
Tony

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top