简体   繁体   中英

How to get domain name in vbscript for windows 2012 server R2?

This worked in windows 2008 server R2, but not on windows 2012 server R2. What is the equivalent in 2012?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AD Manager</title>
<script language="vbscript" type="text/vbscript">
<!--
Sub Validate        
    Dim objSysInfo, owsh
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set owsh = CreateObject("WScript.Network")  
    username = owsh.username
    domain2 = objSysInfo.DomainShortName    <==== This is the line that fails
    document.getElementById("UserName").value =  domain2 & "\" & username
    document.getElementById("Password").value = "1234"
    document.getElementById("frmADManager").submit()
End Sub
//-->
</script>
</head>
<body onload="Validate()">
<form id="frmADManager" action="ProcessLogin.asp" method="post">
<input type="hidden" id="UserName" name="UserName" value="" />
<input type="hidden" id="Password" name="Password" value="" />
</form>
</body>
</html>

You appear to be mixing VBScript with JavaScript in a possibly awkward way.

Here is one method that should work with either OS. There are many ways you could achieve this in a single language. I am surprised that the code you were using actually works on Windows 2008 R2 (yes, I know it does - I just tested it).

Take a look at this article for more information on how you can mix client side JavaScript with VBScript on the same page: http://msdn.microsoft.com/en-us/library/aa260861(v=vs.60).aspx

In this example, we have split your VBScript and JavaScript into separate scripts, where one calls the other.

Tested to work on Windows 2008 R2 and Windows 2012 Server.

NOTE: I do not recommend storing passwords like this in a production environment. Anyone can intercept this password as it loading client side. Presumably you are also using HTTP - the password will be visible in clear text. Consider a more secure alternative - as I am not sure why you are doing this or where you are feeding these credentials, I can't offer you a suggestion as to how to change things.

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>AD Manager</title>
        <script language="vbscript">
            function getDomainInfo()
                Dim objSysInfo, owsh
                Set objSysInfo = CreateObject("ADSystemInfo")
                Set owsh = CreateObject("WScript.Network")  
                username = owsh.username
                getDomainInfo = objSysInfo.DomainShortName & "\" & owsh.username
            End function
        </script>
        <script language="javascript">
            function validate()
            {
                document.getElementById("UserName").value =  getDomainInfo();
                document.getElementById("Password").value = "1234";
                document.getElementById("frmADManager").submit();
            }
        </script>
    </head>
    <body onload="validate()">
        <form id="frmADManager" action="ProcessLogin.asp" method="post">
            <input type="hidden" id="UserName" name="UserName" value="" />
            <input type="hidden" id="Password" name="Password" value="" />
        </form>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM