Click here to Skip to main content
15,887,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

My goal is to write the data to text file located at remote location.
I'm able to write the data to file whose destination server credentials are same as source with below code. But could not write the data to file whose destination server credentials are difference from source.

Could you please help me in passing the destination server credentials so that it connects and writes the data?

Please find my existing code below


VB
' ======================================================== 
Option Explicit 
  
Dim oWsh, oWshSysEnv, objFSO, objWMIService 
Dim oDrives, oDrive, objOutFile, colItems, objItem 
Dim strLineDate, strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUse 
  
Set oWsh = WScript.CreateObject("WScript.Shell") 
Set oWshSysEnv = oWsh.Environment("PROCESS") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
  
strLineDate = Date() 
strLineTime = Time() 
  

'Gets FREE SPACE Report 
Set oDrives = objFSO.Drives 
For Each oDrive In oDrives 
Select Case oDrive.DriveType 
Case 2 'Fixed Drives     
strLineDriveSpace = strLineDriveSpace & " " & oDrive.DriveLetter & "\: " & Round
(oDrive.FreeSpace / (1024 * 1024 * 1024),2) & "GB free (" & Round(100 * (oDrive.FreeSpace / 
oDrive.TotalSize), 2) & " %) " 
End Select 
Next 

'Output to text 
Set objOutFile = objFSO.OpenTextFile("\\132.146.33.251\ft2\hdd_cpu_ram_MONv1.csv", 8, True) 
'objOutFile.WriteLine "Date,Time,Computer Name,Processor Usage (%),Memory Usage (%),Drive Free Space" objOutFile.WriteLine strLineDate & "," & strLineTime & "," & oWshSysEnv("COMPUTERNAME") & "," & strLineProcessorTime & "," & strLinePercentCommittedBytesInUse & "," & strLineDriveSpace 
  
'WScript.Echo "DONE" 
WScript.Quit
Posted
Updated 25-Jul-13 2:53am
v3
Comments
ZurdoDev 22-Jul-13 13:25pm    
You may have to run the script with the credentials.
krishna kishore58 23-Jul-13 5:02am    
Yes, I have tried to do it but could not avail.
Could you please suggest me how to do that?

Application on Windows, including VBScript, run in the context of the current user or the account which executes them in the case of services. You can change the account an application runs under by right-clicking an app and selecting "Run As..." (this varies from version to version in Windows).

However, you can't enforce this in code... meaning you can't make the end user pick that from the menu and run your app under different credentials. So, you have some options:

- You can run your app in a different user context. There are a lot of sample for running your code as a different user in a Windows environment. I won't cover them here but you can start with the search box on the top-right of this page. The drawback is that you have to have some way of getting the credentials you want into the app. Things like hard-coding username and passwords in the app are a bad idea. They aren't flexible and are easy to get. Putting it in a config file or the registry isn't as bad but you will need to encrypt the password. Prompting the user for the credentials is possible too but requires them to know the password and to not get frustrated typing it in each time you app needs to write to the remote server.

- You can write your app as a Windows service and deploy it to the local machine. On the client machine, run it under credentials that will work on the remote server. Just remember that account needs local privileges as well because it will be running as that remote user on the local machine. This solves your problem but makes IT be responsible for setting up the service accounts correctly on the client machines.

- Open up the share on the remote server so that you local user has write permissions. Or, set the share so everyone has write permissions but no read permissions. This turns the share into a dead-drop where the app can write a file but can't turn around and read it.

- Change how you are collecting your information. Since it looks like you are getting drive information, use WMI, Systems Center Manager, Nagios, or any of the other 10 million tools for getting drive data and other telemetry from workstations and servers in your network. Or, write to the event log on the local machine and use the Windows tools to pull that in. Or, setup a Syslog server on your network and post your data to the syslog server. Or, write a web service to receive the data. Or setup an FTP endpoint and send the file that way. Or, better yet, look for a PowerShell script that can do this and run it in your network logon script.
 
Share this answer
 
Comments
krishna kishore58 23-Jul-13 5:05am    
Already I have the script and as I stated earlier it is working fine for where source and destination server credentials are same. Not working for different credentials. So I just would need to pass credentials of the destination server so that we can write the data to the file.
Application on Windows, including VBScript, run in the context of the current user or the account which executes them in the case of services. You can change the account an application runs under by right-clicking an app and selecting "Run As..." (this varies from version to version in Windows).

However, you can't enforce this in code... meaning you can't make the end user pick that from the menu and run your app under different credentials. So, you have some options:

- You can run your app in a different user context. There are a lot of sample for running your code as a different user in a Windows environment. I won't cover them here but you can start with the search box on the top-right of this page. The drawback is that you have to have some way of getting the credentials you want into the app. Things like hard-coding username and passwords in the app are a bad idea. They aren't flexible and are easy to get. Putting it in a config file or the registry isn't as bad but you will need to encrypt the password. Prompting the user for the credentials is possible too but requires them to know the password and to not get frustrated typing it in each time you app needs to write to the remote server.

- You can write your app as a Windows service and deploy it to the local machine. On the client machine, run it under credentials that will work on the remote server. Just remember that account needs local privileges as well because it will be running as that remote user on the local machine. This solves your problem but makes IT be responsible for setting up the service accounts correctly on the client machines.

- Open up the share on the remote server so that you local user has write permissions. Or, set the share so everyone has write permissions but no read permissions. This turns the share into a dead-drop where the app can write a file but can't turn around and read it.

- Change how you are collecting your information. Since it looks like you are getting drive information, use WMI, Systems Center Manager, Nagios, or any of the other 10 million tools for getting drive data and other telemetry from workstations and servers in your network. Or, write to the event log on the local machine and use the Windows tools to pull that in. Or, setup a Syslog server on your network and post your data to the syslog server. Or, write a web service to receive the data. Or setup an FTP endpoint and send the file that way. Or, better yet, look for a PowerShell script that can do this and run it in your network logon script.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900