Reboot Remote Computer via WMI - NOT WORKING FOR MULTIPLE COMPUTERS

Discussion in 'Scripting' started by Megan Kielman, Sep 1, 2004.

  1. Hi all, I obtained the below script from the MS Script Center, I included
    this in a vbscript that actually pulls a list of computers to reboot from a
    spreadsheet. I started out with approximately 100 computers in the
    spreadsheet, the script would run w/o error but it wouldn't reboot the
    computers. However, when I narrowed down the spreadsheet to like 10
    computers, it would work just fine. What gives?!

    --------------piece from MS------------------------------------

    strComputer = "atl-dc-01"
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer &
    "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
    Next

    ------------------------------------------------------------------

    My Code, which incorporates the above code:

    'Get setup to read the directory
    Set rootDSE = GetObject("LDAP://wawwcidc10/RootDSE") 'bind directly to
    server
    Server = rootDSE.Get("dnsHostName")
    Set oConnection = CreateObject("ADODB.Connection")
    Set oRecordSet = CreateObject("ADODB.Command")
    oConnection.Provider = "ADsDSOObject"
    oConnection.Open "Active Directory Provider"
    Set oRecordSet.ActiveConnection = oConnection

    'Read an Excel spreadsheet.
    'Create the XLS object and get ready to read
    Set XLObj = CreateObject("Excel.Application")
    Call XLObj.Workbooks.Open("d:\scripts\rothtest.xls", 0) 'Name of the
    spreadsheet goes here
    If Err.Number <> 0 Then
    MsgBox "Unable to read spreadsheet"
    WScript.Quit
    End If
    XLObj.Sheets("Sheet1").Activate ' this is the sheet name
    Row = 2 'goto Row 2, bypass header

    'Read each cell of the spreadsheet until it's blank
    Do Until XLObj.Range("A:A").Cells(Row).Text = "" 'loop until end of
    worksheet
    CompName = RTrim(XLObj.Range("A:A").Cells(Row).Text) 'pull the groupname
    from the speadsheet.
    UserName = RTrim(XLObj.Range("B:B").Cells(Row).Text) 'pull the username
    from the speadsheet.

    'Now get the group name
    Set oRecordSet = oConnection.Execute("<LDAP://" & Server & ">;" & _
    "(name=" & CompName & ");name,CN,distinguishedName;subtree")
    If Not oRecordSet.EOF Then 'User found continue processing

    'Get the computers based on the distinguished name
    Set objComp = GetObject("LDAP://" & Server & "/" &
    oRecordSet.Fields("distinguishedName").Value)

    'Restart Computer

    Set objWMIService = GetObject("winmgmts:" &
    "{impersonationLevel=impersonate,(Shutdown)}!\\" & CompName & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from
    Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
    Next



    End if
    Row = Row + 1
    Loop

    'Cleanup
    Set oDest = Nothing
    'Set oUsers = Nothing
    XLObj.ActiveWorkbook.Save
    XLObj.ActiveWorkbook.Close
    Set UserFile = Nothing
    wscript.quit
     
    Megan Kielman, Sep 1, 2004
    #1
    1. Advertisements

  2. Why reinvent the wheel ?

    Why not just call the Resource Kit utility SHUTDOWN.EXE from the script ?

    Dave



    | Hi all, I obtained the below script from the MS Script Center, I included
    | this in a vbscript that actually pulls a list of computers to reboot from a
    | spreadsheet. I started out with approximately 100 computers in the
    | spreadsheet, the script would run w/o error but it wouldn't reboot the
    | computers. However, when I narrowed down the spreadsheet to like 10
    | computers, it would work just fine. What gives?!
    |
    | --------------piece from MS------------------------------------
    |
    | strComputer = "atl-dc-01"
    | Set objWMIService = GetObject("winmgmts:" _
    | & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer &
    | "\root\cimv2")
    | Set colOperatingSystems = objWMIService.ExecQuery _
    | ("Select * from Win32_OperatingSystem")
    | For Each objOperatingSystem in colOperatingSystems
    | ObjOperatingSystem.Reboot()
    | Next
    |
    | ------------------------------------------------------------------
    |
    | My Code, which incorporates the above code:
    |
    | 'Get setup to read the directory
    | Set rootDSE = GetObject("LDAP://wawwcidc10/RootDSE") 'bind directly to
    | server
    | Server = rootDSE.Get("dnsHostName")
    | Set oConnection = CreateObject("ADODB.Connection")
    | Set oRecordSet = CreateObject("ADODB.Command")
    | oConnection.Provider = "ADsDSOObject"
    | oConnection.Open "Active Directory Provider"
    | Set oRecordSet.ActiveConnection = oConnection
    |
    | 'Read an Excel spreadsheet.
    | 'Create the XLS object and get ready to read
    | Set XLObj = CreateObject("Excel.Application")
    | Call XLObj.Workbooks.Open("d:\scripts\rothtest.xls", 0) 'Name of the
    | spreadsheet goes here
    | If Err.Number <> 0 Then
    | MsgBox "Unable to read spreadsheet"
    | WScript.Quit
    | End If
    | XLObj.Sheets("Sheet1").Activate ' this is the sheet name
    | Row = 2 'goto Row 2, bypass header
    |
    | 'Read each cell of the spreadsheet until it's blank
    | Do Until XLObj.Range("A:A").Cells(Row).Text = "" 'loop until end of
    | worksheet
    | CompName = RTrim(XLObj.Range("A:A").Cells(Row).Text) 'pull the groupname
    | from the speadsheet.
    | UserName = RTrim(XLObj.Range("B:B").Cells(Row).Text) 'pull the username
    | from the speadsheet.
    |
    | 'Now get the group name
    | Set oRecordSet = oConnection.Execute("<LDAP://" & Server & ">;" & _
    | "(name=" & CompName & ");name,CN,distinguishedName;subtree")
    | If Not oRecordSet.EOF Then 'User found continue processing
    |
    | 'Get the computers based on the distinguished name
    | Set objComp = GetObject("LDAP://" & Server & "/" &
    | oRecordSet.Fields("distinguishedName").Value)
    |
    | 'Restart Computer
    |
    | Set objWMIService = GetObject("winmgmts:" &
    | "{impersonationLevel=impersonate,(Shutdown)}!\\" & CompName & "\root\cimv2")
    | Set colOperatingSystems = objWMIService.ExecQuery("Select * from
    | Win32_OperatingSystem")
    | For Each objOperatingSystem in colOperatingSystems
    | ObjOperatingSystem.Reboot()
    | Next
    |
    |
    |
    | End if
    | Row = Row + 1
    | Loop
    |
    | 'Cleanup
    | Set oDest = Nothing
    | 'Set oUsers = Nothing
    | XLObj.ActiveWorkbook.Save
    | XLObj.ActiveWorkbook.Close
    | Set UserFile = Nothing
    | wscript.quit
    |
    |
     
    David H. Lipman, Sep 1, 2004
    #2
    1. Advertisements

  3. How would I do that?

    But I want to know why this code isn't working!!!
     
    Megan Kielman, Sep 2, 2004
    #3
  4. "Megan Kielman" <[email protected](removethis)weyerhaeuser.com> a écrit dans le
    message de | How would I do that?
    |
    | But I want to know why this code isn't working!!!

    .....probably your script is running too fast for computers to respond ...
    Try to add some WScript.Sleep(3000) or some bigger delay and look if it
    works ...
     
    Yevgen Lazaryev, Sep 2, 2004
    #4
  5. Where should I add WScript.Sleep? at the end of the script so that there is
    a pause before it runs it for each computer?
     
    Megan Kielman, Sep 13, 2004
    #5
  6. ....you add it not in the end of the script but in the end of the loop
    .....Just before the line Loop in your script ...

    "Megan Kielman" <[email protected](removethis)weyerhaeuser.com> a écrit dans le
    message de
    | Where should I add WScript.Sleep? at the end of the script so that there
    is
    | a pause before it runs it for each computer?
    |
     
    Yevgen Lazaryev, Sep 14, 2004
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.