Avatar of Affaan Abyaz
Affaan Abyaz
Flag 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...
DiskSpace-Report-HTML.txt
VB ScriptPowershellWindows OSScripting Languages

Avatar of undefined
Last Comment
Affaan Abyaz

8/22/2022 - Mon
Jason Crawford

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

Affaan Abyaz

ASKER
I think I missed to mention that its a VBScript.. Please save it with .vbs extension to check on the code.
RobSampson

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Affaan Abyaz

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

ASKER
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
RobSampson

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Affaan Abyaz

ASKER
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
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Affaan Abyaz

ASKER
Thanks Rob. As always your solution works.