Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBScript using admin from another domain and pulling in data from file

Posted on 2010-11-10
8
Medium Priority
?
499 Views
Last Modified: 2012-05-10
I administer 3 separate domains. I obviously have an admin account on all domain. I wanted to run a script remotely on machines in a domain that I am not currently logged into. I can access the domain via IP and when prompted I can simply enter my info for that domain to access files, PCs, servers etc.
I am new to scripting and already have a script to check what printers are already installed on a remote machine.
The problem is running the script on a remote machine within another domain. I know I can conenct to a server or PC and run it from there but wanted to know if there was a way to run it without doing logging into a machine on that domain.
ALSO... Is there a way to insert/pull computer names or IP addresses into a script and then print out the result to a file? Currently the script only prints to the screen.
Help thanks.
Below is the current script:
strComputer = "remotePCName"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then
        strPrinterType = "Local"
    Else
        strPrinterType = "Network"
    End If
    Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next
0
Comment
Question by:DLazarus001
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:danora
ID: 34106631
Hi DLazarus001

You will need to script with a string then use the > and quote where you want the output file saving to

strRun = strinfo >C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt"
0
 

Author Comment

by:DLazarus001
ID: 34106870
Thanks..
How would I pull a list of computer names into the script and have it run for each computer it pulls in?
0
 
LVL 3

Accepted Solution

by:
danora earned 1500 total points
ID: 34107015
You can call a txt file with a list of computer names in, i think this site might help you out - http://www.eggheadcafe.com/software/aspnet/29528325/run-script-agains-a-list-of-computers.aspx

The other way to do it would be to run your script against every device in AD, i think this is the easiest option as you can connect directly to the domain and enumerate a list of machines to run the script against.

Hope this helps
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DLazarus001
ID: 34107042
Thanks but because I am a newbie: how do I run the script against every device in AD?... I guess I would also need to put a loop in this also?
0
 
LVL 3

Expert Comment

by:danora
ID: 34107094
I have had a quick look around and found this for you.


On Error Resume Next

Dim PCQuery, objConnection, objCommand, objRecordSet
Dim oRootDSE, strDNC

'First get domain information
Set oRootDSE = GetObject("LDAP://rootDSE")
strDNC = oRootDSE.get("defaultNamingContext")
' other categories = computer, user, printqueue, group
PCQuery = "<LDAP://" & strDNC & _
     ">;(objectCategory=computer)" & _
       ";distinguishedName,name;subtree"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = PCQuery
Set objRecordSet = objCommand.Execute

Do Until objRecordSet.EOF
    'assign the computer name and distinguished path to variables
    strComputer = objRecordSet.Fields("name")
    strComputerDN = objRecordSet.Fields("distinguishedName")
    'Put the worker process of your code in here
    '*******************************************
       
   
    '*******************************************
    objrecordset.MoveNext
Loop

objConnection.Close
0
 

Author Comment

by:DLazarus001
ID: 34107228
Does this look correct?
On Error Resume Next

Dim PCQuery, objConnection, objCommand, objRecordSet
Dim oRootDSE, strDNC

'First get domain information
Set oRootDSE = GetObject("LDAP://rootDSE")
strDNC = oRootDSE.get("defaultNamingContext")
' other categories = computer, user, printqueue, group
PCQuery = "<LDAP://" & strDNC & _
     ">;(objectCategory=computer)" & _
       ";distinguishedName,name;subtree"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = PCQuery
Set objRecordSet = objCommand.Execute

Do Until objRecordSet.EOF
    'assign the computer name and distinguished path to variables
    strComputer = objRecordSet.Fields("name")
    strComputerDN = objRecordSet.Fields("distinguishedName")
   
'Put the worker process of your code in here
    '*******************************************

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then
        strPrinterType = "Local"
    Else
        strPrinterType = "Network"
    End If
Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next
strRun = strinfo >C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt"  
   
    '*******************************************
    objrecordset.MoveNext
Loop

objConnection.Close
0
 
LVL 3

Expert Comment

by:danora
ID: 34107255
Looks pretty good to me, you just need to change the destination of where the output file will be saved :)
0
 

Author Closing Comment

by:DLazarus001
ID: 34474107
The answer did not completely satisfy the problem. But it help point me in a another direction
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question