Search code examples
jenkinsgroovyjenkins-pipelinewmic

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.


Solution

  • 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()