本文介绍了如何将数据写入位于远程位置的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我的目标是将数据写入位于远程位置的文本文件。 我能够将数据写入目标服务器凭据与源代码相同的文件。但无法将数据写入目标服务器凭据与源不同的文件。 您能否帮我传递目标服务器凭据以便连接和写入数据? 请在下面找到我现有的代码 ' ================== ====================================== 选项 明确 Dim oWsh ,oWshSysEnv,objFSO,objWMIService Dim oDrives,oDrive,objOutFile,colItems,objItem Dim strLineDate,strLineTime,strLineProcessorTime,strLineDriveSpace,strLinePercentCommittedBytesInUse Set oWsh = WScript.CreateObject( WScript.Shell) 设置 oWshSysEnv = oWsh.Environment( PROCESS) 设置 objFSO = CreateObject ( Scripting.FileSystemObject) Set objWMIService = GetObject( winmgmts:\\\\\root \ CYV2) strLineDate = 日期() strLineTime =时间() ' 获取免费空间报告 设置 oDrives = objFSO.Drives 对于 每个 oDrive 在 oDrives 选择 案例 oDrive.DriveType 案例 2 ' 固定驱动器 strLineDriveSpace = strLineDriveSpace& & oDrive.DriveLetter& \:&回合(oDrive.FreeSpace /( 1024 * 1024 * 1024 ), 2 )& GB free(& Round( 100 *(oDrive.FreeSpace / oDrive.TotalSize), 2 )& %) 结束 选择 下一步 ' 输出到文本 设置 objOutFile = objFSO.OpenTextFile( \\132.146.33.251\ft2\hdd_cpu_ram_MONv1.csv, 8 , True ) ' objOutFile.WriteLine日期,时间,计算机名称,处理器使用率(%),内存使用率(%),驱动器可用空间objOutFile.WriteLine strLineDate&放大器; ,& strLineTime& ,& oWshSysEnv(COMPUTERNAME)& ,& strLineProcessorTime& ,& strLinePercentCommittedBytesInUse& ,& strLineDriveSpace ' WScript.EchoDONE WScript.Quit 解决方案 Windows上的应用程序,包括VBScript,在当前用户或执行它们的帐户的上下文中运行在服务的情况下。您可以通过右键单击某个应用并选择运行方式...来更改应用程序运行的帐户(这在Windows中随版本的不同而不同)。 但是,您无法在代码中强制执行此操作...这意味着您无法让最终用户从菜单中选择并在不同的凭据下运行您的应用程序。所以,你有一些选择: - 你可以在不同的用户环境中运行你的应用程序。在Windows环境中,有很多用于将代码作为不同用户运行的示例。我不会在这里介绍它们,但您可以从本页右上角的搜索框开始。缺点是你必须有一些方法来获得你想要的凭据到应用程序。在应用程序中硬编码用户名和密码之类的东西是个坏主意。它们不灵活且易于获取。将它放在配置文件或注册表中并没有那么糟糕,但您需要加密密码。提示用户输入凭据也是可能的,但要求他们知道密码,并且在每次应用程序需要写入远程服务器时都不要沮丧地输入密码。 - 您可以将应用程序编写为Windows服务并将其部署到本地计算机。在客户端计算机上,在可在远程服务器上运行的凭据下运行它。请记住,帐户也需要本地权限,因为它将作为本地计算机上的远程用户运行。这解决了您的问题,但让IT负责在客户端计算机上正确设置服务帐户。 - 打开远程服务器上的共享,以便您在本地用户具有写入权限。或者,设置共享,以便每个人都具有写权限但没有读权限。这会将共享变成死角,应用程序可以在其中写入文件但无法转身阅读。 - 更改收集信息的方式。由于看起来您正在获取驱动器信息,因此请使用WMI,Systems Center Manager,Nagios或任何其他1000万个工具从网络中的工作站和服务器获取驱动器数据和其他遥测。或者,写入本地计算机上的事件日志并使用Windows工具将其拉入。或者,在网络上设置Syslog服务器并将数据发布到syslog服务器。或者,编写Web服务以接收数据。或者设置FTP端点并以这种方式发送文件。或者,更好的是,查找可以执行此操作并在网络登录脚本中运行它的PowerShell脚本。 Windows上的应用程序,包括VBScript,在当前用户的上下文中运行或在服务的情况下执行它们的帐户。您可以通过右键单击某个应用并选择运行方式...来更改应用程序运行的帐户(这在Windows中随版本的不同而不同)。 但是,您无法在代码中强制执行此操作...这意味着您无法让最终用户从菜单中选择并在不同的凭据下运行您的应用程序。所以,你有一些选择: - 你可以在不同的用户环境中运行你的应用程序。在Windows环境中,有很多用于将代码作为不同用户运行的示例。我不会在这里介绍它们,但您可以从本页右上角的搜索框开始。缺点是你必须有一些方法来获得你想要的凭据到应用程序。在应用程序中硬编码用户名和密码之类的东西是个坏主意。它们不灵活且易于获取。将它放在配置文件或注册表中并没有那么糟糕,但您需要加密密码。提示用户输入凭据也是可能的,但要求他们知道密码,并且在每次应用程序需要写入远程服务器时都不要沮丧地输入密码。 - 您可以将应用程序编写为Windows服务并将其部署到本地计算机。在客户端计算机上,在可在远程服务器上运行的凭据下运行它。请记住,帐户也需要本地权限,因为它将作为本地计算机上的远程用户运行。这解决了您的问题,但让IT负责在客户端计算机上正确设置服务帐户。 - 打开远程服务器上的共享,以便您在本地用户具有写入权限。或者,设置共享,以便每个人都具有写权限但没有读权限。这会将共享变成死角,应用程序可以在其中写入文件但无法转身阅读。 - 更改收集信息的方式。由于看起来您正在获取驱动器信息,因此请使用WMI,Systems Center Manager,Nagios或任何其他1000万个工具从网络中的工作站和服务器获取驱动器数据和其他遥测。或者,写入本地计算机上的事件日志并使用Windows工具将其拉入。或者,在网络上设置Syslog服务器并将数据发布到syslog服务器。或者,编写Web服务以接收数据。或者设置FTP端点并以这种方式发送文件。或者,更好的是,查找可以执行此操作并在网络登录脚本中运行它的PowerShell脚本。 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' ========================================================Option ExplicitDim oWsh, oWshSysEnv, objFSO, objWMIServiceDim oDrives, oDrive, objOutFile, colItems, objItemDim strLineDate, strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUseSet 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 ReportSet oDrives = objFSO.DrivesFor Each oDrive In oDrivesSelect Case oDrive.DriveTypeCase 2 'Fixed DrivesstrLineDriveSpace = strLineDriveSpace & " " & oDrive.DriveLetter & "\: " & Round(oDrive.FreeSpace / (1024 * 1024 * 1024),2) & "GB free (" & Round(100 * (oDrive.FreeSpace /oDrive.TotalSize), 2) & " %) "End SelectNext'Output to textSet 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 解决方案 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.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. 这篇关于如何将数据写入位于远程位置的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!