Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使用批处理脚本对目录中的所有xml文件进行签名_Windows_Batch File_For Loop_Cmd - Fatal编程技术网

Windows 使用批处理脚本对目录中的所有xml文件进行签名

Windows 使用批处理脚本对目录中的所有xml文件进行签名,windows,batch-file,for-loop,cmd,Windows,Batch File,For Loop,Cmd,我正在尝试编写windows批处理脚本 在目录中搜索第一个.pem证书 询问用户密码 使用openSSL对此证书在目录中的所有.xml-文件进行签名 签名的名称应与.xml-文件的名称相同,但带有.sig后缀,例如xml:this.xml签名:this.xml.sig 用于签署文件的openSSL命令是 openssldgst-binary-sha512-sign-out 因此,要使用cert“cert.pem”创建xml文件“this.xml”的签名“this.xml.sig”,我必须使用以下

我正在尝试编写windows批处理脚本

  • 在目录中搜索第一个
    .pem
    证书
  • 询问用户密码
  • 使用openSSL对此证书在目录中的所有
    .xml
    -文件进行签名
  • 签名的名称应与
    .xml
    -文件的名称相同,但带有
    .sig
    后缀,例如
    xml:this.xml签名:this.xml.sig

    用于签署文件的openSSL命令是

    openssldgst-binary-sha512-sign-out

    因此,要使用cert“cert.pem”创建xml文件“this.xml”的签名“this.xml.sig”,我必须使用以下命令

    openssldgst-binary-sha512-sign cert.pem-out this.xml.sig this.xml

    然后提示您输入证书的密码,该密码应用于对XML进行签名

    因此,我编写了以下脚本,但当我双击
    .bat
    -脚本时,我遇到了以下问题:

    • 黑色的命令框立即打开和关闭,但什么也没有发生。脚本似乎没有在目录中找到find the
      .pem
      -文件,但有一个
    • 当我将第一个
      for
      循环更改为使用递归搜索(在
      之前添加
      /r
      )时,它会在子目录中找到一个
      .pem
      -文件,并要求我输入密码,但在此之后,命令窗口关闭,并且不会创建签名
    剧本:

    @echo off
    Rem placeholder for cert-file
    set cert=""
    
    for %%g in (*.pem) do (
      echo "Certificate found: " %%g
      set cert=%%g
      Rem exit loop as we found a cert-file
      goto :SignFiles
    )
    
    echo "No cert file found"
    exit
    
    :SignFiles
    Rem Ask user for Cert-Password
    set /p password="Please input cert-password: " 
    
    Rem debug
    echo %password%
    
    Rem sign all xml-files in directory
    for %%g in (*.xml) do (
      echo "Sign XML-File: " %%g
      Rem sign file and pass saved password for signing
      openssl dgst -binary -sha512 -out %%g.sig -sign=%cert% %%g < %password%
    )
    
    pause
    exit
    
    到第二个foor循环,并看到证书和xml文件的名称已正确写入cert.txt/file.txt。但仍然没有创建签名

    for . %%g ...
    
    应该是

    for %%g ...
    
    您的代码将产生语法错误,如果您只需单击该批处理即可运行该批处理,则会导致窗口关闭。要查看语法错误,需要从提示符运行批处理


    如果通过单击批处理来运行批处理,您将看不到有意生成的消息,因为
    echo
    后面的命令是
    退出
    。可能在退出之前插入一个
    pause
    命令,以确保看到消息-或将消息重定向到文件…

    将所有注释/备注从
    ::
    更改为
    Rem
    @Compo这不会更改行为:(您的脚本无法使用
    ,只需将它们更改为
    Rem
    ,重新运行脚本并用更新的代码更新问题。@Compo用更改的注释更新了问题,但仍然没有更改您在中使用的
    脚本行为。%%g(
    两次,将%%g中的两个更改为
    )(
    。这至少帮助我让脚本运行,但仍然没有完全工作。请查看更新的问题您需要
    echo%password%|openssl…
    ,即重定向器应该是管道。在我更改openssl命令后使用
    ,通过
    -passin
    参数正确传递密码。T谢谢你通过显示我的语法错误来帮助我!
    for %%g ...