Running A Script from Microsoft

I am trying to run a windows script from TechNet on a server to fix an error. However, when I save it from Notepad as Windows Script (or even VBS) it errors out. Please see the script below.

What I am looking for is a step through on how to copy, paste, and execute this script. Any help is apprecaited!

Thank you!
   (c) 2007, Microsoft Corp.
// Check the version of the operating system. Stop the script if the version is earlier than 6.
if ( ! CheckOSVersion() )
    WScript.Echo("ERROR: This script will only work on Longhorn Server or above.");
// Retrieve the local computer's rootDSE LDAP object.
var localRootDse = null;
    localRootDse = GetObject("LDAP://localhost/rootDSE");
    WScript.Echo("There was an error attempting to retrieve the localhost RootDSE object.");
    WScript.Echo("Perhaps this machine is not a Domain Controller on the network?");
    WScript.Echo("ErrorCode: " + e.number);
// Retrieve several rootDSE properties
var dnsHostName = localRootDse.Get("dnsHostName");
var dsServiceName = localRootDse.Get("dsServiceName");
var defaultNamingContext = localRootDse.Get("defaultNamingContext");
// Open the default naming context
var ncObj = GetObject("LDAP://" + defaultNamingContext);
// Get the "FSMO Role Owner"
var strfsmoNtdsa = ncObj.FsmoRoleOwner;
var fsmoNtdsaObj = GetObject("LDAP://" + strfsmoNtdsa);
// Get the parent object of "FSMO Role Owner"
var fsmoServerObj = GetObject(fsmoNtdsaObj.Parent);
// By using the Server Reference, retrieve the name of the PDC computer
var strFsmoComputer = fsmoServerObj.ServerReference;
var fsmoComputerObj = GetObject("LDAP://" + strFsmoComputer);
var pdcName = fsmoComputerObj.Get("name");
// Get the RootDSE object for the PDC
var pdcRootDse = GetObject("LDAP://" + pdcName + "/rootDSE");
// Check whether the PDC is a legacy domain or not.
var domainControllerFunctionality = pdcRootDse.Get("domainControllerFunctionality");
if ( domainControllerFunctionality > 2 )
    WScript.Echo("Domain is already operating in a mode higher than Windows Server 2003 mode. Stopping script execution.");
// Get the default naming context for the PDC
var pdcDefaultNamingContext = pdcRootDse.Get("defaultNamingContext");
// Retrieve the well known object from the PDC
var pdcSystem = GetObject("LDAP://" + pdcName + "/<WKGUID=AB1D30F3768811D1ADED00C04FD8D5CD," + pdcDefaultNamingContext + ">");
// Get the distinguished name for the well known object
var pdcDistinguishedName = pdcSystem.Get("distinguishedName");
// Check whether the task has already been run
var taskMarker = null;
    taskMarker = GetObject("LDAP://" + pdcName + "/<WKGUID=6ACDD74F3F314ae396F62BBE6B2DB961,CN=Server," + pdcDistinguishedName + ">");
    if ( e.number == -2147016656 ) // Check and see if error code is ERROR_DS_NO_SUCH_OBJECT
        taskMarker = null;
        WScript.Echo("Error attempting to retrieve well known object from PDC.");
        WScript.Echo("Name: " + + "\nDescription: " + e.description + "\nCode: " + e.number + "\nMessage: " + e.message);
// If the well known object exists, the SAM upgrade is already running. Therefore, stop the script.
if ( taskMarker != null )
    WScript.Echo("SAM upgrade task already being run. No work done.");
// Get the Server container with that distinguished name
var serverObj = GetObject("LDAP://" + pdcName + "/CN=Server," + pdcDistinguishedName);
// Prepare a safe array (for example, VBArray) with one entry
var jsArray = new Array(1);
jsArray[0] = "B:32:6ACDD74F3F314ae396F62BBE6B2DB961:"+ dsServiceName;
var vbArray = JS2VBArray(jsArray);
    // Append an entry to the "Other-Well-Known-Objects" attribute for the 
    // previous server object.
    serverObj.PutEx(3, "otherWellKnownObjects", vbArray);
    WScript.Echo("Unexpected error attempting to put the well known GUID.");
    WScript.Echo("ErrorCode: " + e.number);
WScript.Echo("Running upgrade task.");
// Set the "runSamUpgradeTasks" attribute in the local rootDSE
localRootDse.Put("runSamUpgradeTasks", 1);
// Remote the binary data from the previous well known object entry 
serverObj.PutEx(4, "otherWellKnownObjects", vbArray);
// The upgrade is complete.
function CheckOSVersion()
    var wbemFlagReturnImmediately = 0x10;
    var wbemFlagForwardOnly = 0x20;
    var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");
    var colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL",
                                      wbemFlagReturnImmediately | wbemFlagForwardOnly);
    var enumItems = new Enumerator(colItems);
    for (; !enumItems.atEnd(); enumItems.moveNext()) {
        var objItem = enumItems.item();
        var fullVersion = objItem.Version;
        var indexPoint = fullVersion.indexOf(".");
        if ( indexPoint == -1 )
            return false;
        var majorVersion = fullVersion.substring(0, indexPoint);
        return (majorVersion >= "6");
    return false;
function JS2VBArray( objJSArray )
    var dictionary = new ActiveXObject( "Scripting.Dictionary" );
    for ( var i = 0; i < objJSArray.length; i++ )
        dictionary.add( i, objJSArray[ i ] );
    return dictionary.Items();

Back to the top

Open in new window

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.

Rommel SultanCommented:
That script is Javascript.

Save the code into a file on your desktop named, "SamUpgradeTask.js"
Drop into a CMD prompt and be sure your in your desktop folder.
Run cscript SamUpgradeTask.js
Reboot the server.

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
Majo2469Author Commented:
That got me further than I was, thanks :)

Now getting error:
Line: 170
Char: 6
Error: Expected ';'
Code: 800A03EC

Majo2469Author Commented:
Nevermind - ID10T error!

It helps to remove "Back To Top" from the scripting :P

Majo2469Author Commented:
Excellent answer! Great Job Rsultan!!!

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
Shell Scripting

From novice to tech pro — start learning today.