vb.net 运行 gpg.exe 作业步骤从 PC 运行正常,但不能从 SQL Server 代理计划运行

Posted

技术标签:

【中文标题】vb.net 运行 gpg.exe 作业步骤从 PC 运行正常,但不能从 SQL Server 代理计划运行【英文标题】:vb.net runs gpg.exe step of job runs ok from PC but not from SQL Server Agent schedule 【发布时间】:2019-01-23 15:31:58 【问题描述】:

我的 PC 上的 Visual Studio 一切正常,从“开始”按钮运行它。当我构建可执行文件并将可执行文件复制到生产机器并通过生产机器上的 SQL Server 代理安排作业时——创建文件一切正常,但加密位不起作用。 gpg.exe 在生产服务器上:\sql2014\c$\Program Files (x86)\GnuPG\bin

gpg 在我的 PC 上:C:\Program Files (x86)\GnuPG\bin

filename.csv 可以在正确的位置创建 - 我用这两个名称进行了测试 Dim Extract_File As String = “\sql2014\e$\Extracts\ProgramName\filename.csv” 'Dim Extract_File As String = “E:\Extracts\ProgramName\filename.csv” '在我的电脑上执行此操作,我必须将 E: 更改为 C:

这一行调用函数: FileEncrypted = Encrypt_File(Extract_File, Batch_Timestamp)

Private Function Encrypt_File(File_To_Encrypt As String, Batch_Timestamp As Date)
    On Error GoTo Encrypt_File_Error
    Dim Success As Boolean = False
    Dim sourceName As String = File_To_Encrypt
    Dim gpgProcess = New Process()
    ‘Test with working directory - no effect
    ‘gpgProcess.StartInfo.UseShellExecute = False 
    'gpgProcess.StartInfo.WorkingDirectory = "\\sql2014\c$\Program Files (x86)\GnuPG\bin\"
    ‘gpgProcess.StartInfo.FileName = "gpg.exe"
    gpgProcess.StartInfo.FileName = \\sql2014\c$\Program Files (x86)\GnuPG\bin\gpg.exe  ‘This works from my PC
    ‘gpgProcess.StartInfo.FileName = \\sql2014\c$\Program Files (x86)\GnuPG\bn\gpg.exe  ‘If I change this path took the “i” out of bin I get an error:  The system cannot find the file specified
    gpgProcess.StartInfo.UseShellExecute = False 
    gpgProcess.StartInfo.CreateNoWindow = True
    gpgProcess.StartInfo.Arguments = "--batch --yes --recipient reciptname --encrypt " & sourceName
    gpgProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    gpgProcess.Start()
    gpgProcess.WaitForExit()
    If FileExists(sourceName & ".gpg") Then
        Success = True
    End If
Encrypt_File_Exit:
    On Error Resume Next
    ‘gpgProcess.WaitForExit() moved this up to 
    gpgProcess.Close()
    Return Success
    Exit Function
Encrypt_File_Error:
    Error_Handler("SomeModule.vb", "Encrypt_File", Err, System_Output, Batch_Timestamp)
    Resume Encrypt_File_Exit
End Function

关于如何解决此问题的任何建议。当它在我的 PC 上运行时,它会在与 filename.csv 相同的目录中创建一个 filename.csv.gpg。在生产服务器上,它不会创建 gpg,也不会给出可见的错误消息。

【问题讨论】:

【参考方案1】:

这就是我解决这个问题的方法。我从 NuGet 包管理器安装了 OpenPgpLib 并重新编写了这个函数,如下所示。 我从 Kleopatra 工具创建了 .asc 文件,并将其保存在下面代码位的 pubkey 中使用的位置。 OpenPgp 来自包。

    Private Function Encrypt_File(File_To_Encrypt As String, Log_File As String, Batch_Timestamp As Date)
        Dim Success As Boolean = False
        Dim encryptthis As String = File_To_Encrypt
        Dim thisencrypted As String = File_To_Encrypt & ".gpg"
        Dim pubkey As String = "\\sql2014\c$\Data_Programs\MyDirectory\<thepublickeyfile>.asc"
        Try
            OpenPgp.EncryptFile(encryptthis, thisencrypted, pubkey, False, False)
            If FileExists(thisencrypted) Then
                Success = True
            End If
        Catch ex As Exception
            App_Logger(Log_File, ex.StackTrace.ToString(), System_Output, Batch_Timestamp)
            Success = False
        End Try
        Return Success
    End Function

【讨论】:

以上是关于vb.net 运行 gpg.exe 作业步骤从 PC 运行正常,但不能从 SQL Server 代理计划运行的主要内容,如果未能解决你的问题,请参考以下文章

从 Excel 导入数据 - VB.NET

VB.NET - 传递要在函数中使用的表达式

如何从 VB.NET 运行 DOS/CMD/命令提示符命令?

从 C# 转换为 vb.net 后,它显示错误

C#/VB.NET 从PDF中提取表格

从 Isnumeric() VB.NET 获取错误