Solved

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

Posted on 2010-11-10
8
471 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
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.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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