Link to home
Start Free TrialLog in
Avatar of Affaan Abyaz
Affaan AbyazFlag for India

asked on

Use different credenatils to pull the report from other domain servers

attached script works well in the same domain servers but I want to use different credential to be used by this script in order to run against another domain servers.. we have multiple domains in a same forest and i need some help in updating the script...
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

Have you tried using Invoke-Command with the -Credential property?  You'll have to cache the credentials to a variable using Get-Credential so something like:

$creds = Get-Credential
Invoke-Command -ComputerName <server name> -ScriptBlock {Insert code} -Credential $creds

Open in new window

Avatar of Affaan Abyaz


I think I missed to mention that its a VBScript.. Please save it with .vbs extension to check on the code.
Hi, try changing this line:
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Open in new window

to this
	Set objLocator=CreateObject("WbemScripting.SWbemLocator")
	Set objWMIService=objLocator.ConnectServer(strComputer, "root\default", "domain\remoteuser", "remotepassword")

Open in new window

then change remoteuser and remotepassword as required.

by anyway can we encrypt the credentials ?
Please note that I will have to schedule it on the server as a job aswell..
Hi Rob,

I have updated the script and executed the same from different domain.. its did not pop-up any error message and executing but in the report its not giving any values either.. attached is the report
Sorry, it wasn't meant to have root\default in it.  It should be root\CimV2
	Set objLocator=CreateObject("WbemScripting.SWbemLocator")
	Set objWMIService=objLocator.ConnectServer(strComputer, "root\CimV2", "domain\remoteuser", "remotepassword")

Open in new window

As for encrypting the password, you can't really do that in VBScript.  What you could do to make it a bit more secure, is have the script accept the username and password as script parameters, and then add those parameters to the scheduled task.  To make it a bit more secure, if the script did some known string manipulation on the passed parameters, you can specify a reverse engineered parameter to the script, and have the script manipulate that into the correct password.

For example, if your password was mypass you could pass
to the script, and in the script have this
strPassword = ""
For intChr = 1 To Len(strPassed)
	If intChr = 1 Then
		strPassword = Chr(Asc(Mid(strPassed, intChr, 1)) + 64)
		strPassword = strPassword & Chr(Asc(Mid(strPassed, intChr, 1)) + 64)
	End If

Open in new window

and strPassword would contain the correct password.  This way, the password can only be sniffed out at run time, which is unlikely, but not impossible.


Thanks Rob.. that works well.. I am able to use it with different credentails and works well.. one last query and its about the same thing hidding the credentails in vbscript.. I am thinking about coverting vb to exe and the problem I found is that it has a variable set for the location. Line 53 says OutputDir = "C:\HP\Wintel\Admin_Tasks\DiskSpace" and if I replace it with ".\" in order to check for servers.txt and my outfiles to be created in the same folder where as I am running this script, it is not working...

could you please help me in fixing this ? and then we can have this questions closed gracefully :D
Avatar of RobSampson
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Rob. As always your solution works.