Jenkins Groovy中的cleanup bat命令输出

时间:2019-06-07 08:36:46

标签: jenkins groovy jenkins-pipeline wmic

如何在Jenkins上的管道脚本中清理bat命令的输出

以下是我使用的管道脚本。

Drive_list = bat (  label: 'Get Drive List',
                    returnStdout: true,
                    script: 'wmic logicaldisk where DriveType=3 get DeviceID'
                    ).trim()

Drive_list现在包含以下内容。

{D:\Jenkins\workspace\test>wmic logicaldisk where DriveType=3 get DeviceID 
    DeviceID  

    C:        

    D:}

提取列表{'C:','D:'}

应该怎么做?

我尝试通过使用c_list = Drive_list.tokenize('\r\n')标记Drive_list来创建列表,现在c_list是

{[D:\Jenkins\workspace\test>wmic logicaldisk where DriveType=3 get DeviceID , DeviceID , , C: , , D:]}

要清理c_list,我尝试了以下操作  c_list.removeAll{ it.contains('DeviceID')导致

{[DeviceID , , C: , , D:]}仅删除了第一行,但还有其他添加到该驱动器ID中的一些白色字符,我无法删除。

2 个答案:

答案 0 :(得分:1)

没有一种常规方法可以使用编码的原因,因此通过添加编码选项“ UTF-16LE”来修改管道脚本。

Drive_list = bat (  label: 'Get Drive List',
                    returnStdout: true,
                    encoding  : "UTF-16LE",
                    script: 'wmic logicaldisk where DriveType=3 get DeviceID'
                    ).trim()

答案 1 :(得分:0)

您可以使用groovy(java)本机方法获取根驱动器:

https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listRoots()

File[] Drive_list = File.listRoots()