Solved

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

Posted on 2010-11-10
8
441 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 500 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now