[英]cleanup bat command output in Jenkins Groovy
如何在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中的白色字符,我无法删除。
常规方法均无法使用的原因是由于编码,因此通过添加编码选项“ UTF-16LE”来修改管道脚本是可行的。
Drive_list = bat ( label: 'Get Drive List',
returnStdout: true,
encoding : "UTF-16LE",
script: 'wmic logicaldisk where DriveType=3 get DeviceID'
).trim()
您可以使用groovy(java)本机方法来获取根驱动器:
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listRoots()
File[] Drive_list = File.listRoots()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.