简体   繁体   中英

cleanup bat command output in Jenkins Groovy

How to cleanup output of a bat command in pipeline script on Jenkins

Following is the pipeline script I have used.

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

Drive_list now contains following.

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

    C:        

    D:}

What should be done for extracting a list {'C:','D:'}

I have tried creating a list by tokenizing Drive_list using c_list = Drive_list.tokenize('\\r\\n') , and now c_list is

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

To clean up c_list I have tried following c_list.removeAll{ it.contains('DeviceID') resulted in

{[DeviceID , , C: , , D:]} which only removed first line, but still has others adding to this the drive IDs have some white characters which I am unable to remove.

Reason for none of the groovy methods are working because of encoding, modifying pipeline script by adding encoding option "UTF-16LE" worked.

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

You could use the groovy(java) native method to get root drives:

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

File[] Drive_list = File.listRoots()


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM