?
Solved

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

Posted on 2010-11-10
8
Medium Priority
?
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

719 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