【问题标题】:ruby open network file fails when run from task scheduler从任务计划程序运行时,ruby 打开网络文件失败
【发布时间】:2016-01-07 06:10:25
【问题描述】:

代码:

def log_it log_entry
  puts log_entry
  f = File.open("C:/test/test_open_file.log", "a")
  f.puts log_entry
  f.close
end

if File.exist?("C:/test/test_open_file.log")
  File.delete("C:/test/test_open_file.log")
end
begin
  f = File.open("//ServerName/sharename/sample_file.csv", "w")
  puts f.class
rescue Exception => e
  log_it "e.message: #{e.message}\n#{e.backtrace}"
end

当我从 cmd.exe 窗口运行它时,它运行良好,但如果我从计划任务运行它,它会失败,但有异常

e.message: Invalid argument @ rb_sysopen - //ServerName/sharename/sample_file.csv

我已经尝试过我的帐户(管理员帐户)和管理员本身,但都不能在计划任务中工作。我选择了“无论用户是否登录都运行”并选中“以最高权限运行”,并且“配置为:”设置为 Windows 7,Windows Server 2008 R2。这是一台 Windows 7 机器。我设置了“ Start in”到该脚本所在的目录。最后,我尝试在ruby.exe的属性对话框的兼容性选项卡上检查“以管理员身份运行”,没有区别。

这显然与实际尝试连接到远程服务器的帐户有关,但是如果我告诉它使用我的帐户,为什么我从命令运行它时没有问题提示?

【问题讨论】:

    标签: ruby windows


    【解决方案1】:

    如果有人遇到类似问题,我最终添加了这一行:

    system("net use x: \\\\ServerName\\sharename /user:username password")
    

    并将 File.Open 更改为

    f = File.open("x:/sample_file.csv", "w")
    

    这就是诀窍。不过,如果有人知道的话,仍然想知道如何使 UNC 版本正常工作...

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      相关资源
      最近更新 更多