Use different credenatils to pull the report from other domain servers

Affaan Abyaz
Affaan Abyaz used Ask the Experts™
on
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...
DiskSpace-Report-HTML.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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

Author

Commented:
I think I missed to mention that its a VBScript.. Please save it with .vbs extension to check on the code.
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Rob.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

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

Author

Commented:
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
2015-11-02_18-52-21.png
Most Valuable Expert 2012
Top Expert 2014

Commented:
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
90!33
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)
	Else
		strPassword = strPassword & Chr(Asc(Mid(strPassed, intChr, 1)) + 64)
	End If
Next

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.

Regards,

Rob.

Author

Commented:
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
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hi, some EXE conversions still create a temporary VBS (which is plain text) to execute it, then delete it immediately after.  You may want to try to find an EXE packager that does not do this.  I think the latest one from VBSEdit doesn't do this anymore.

You can try changing the OutputDir to this
OutputDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

and then echoing that path with
MsgBox OutputDir

to see what directory it picks, but that should work.

Rob.

Author

Commented:
Thanks Rob. As always your solution works.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial