简体   繁体   中英

Storing each value of a PowerShell process into a Java object array

I run a commmand through a Java process to get multiple disk volumes out of powershell. The output looks like this:

Powershell 输出

I now want to save an instance of each disk so they can then be inserted into an SQL database.

Here is where I am so far:

public class Disk {

    private String Letter;
    private String Label;
    private String Type;
    private String Health;
    private String Op;
    private String Size;
    private String Remaining;

    public Disk(String letter, String label, String type, String health, String op, String size, String remaining) {
        Letter = letter;
        Label = label;
        Type = type;
        Health = health;
        Op = op;
        Size = size;
        Remaining = remaining;
    }

    private List<Disk> diskTable = new ArrayList<Disk>();

    
    public void getDiskInfo() {
        
        //call the powershell process
        ProcessBuilder pb = new ProcessBuilder();

        pb.command("powershell.exe", "/c", "Get-Volume | fl DriveLetter, FileSystemLabel, FileSystemType, HealthStatus, OperationalStatus, Size, SizeRemaining");

        try {

            //read in the output from the powershell process
            Process Diskprocess = pb.start();
            BufferedReader Diskreader =
                    new BufferedReader(new InputStreamReader(Diskprocess.getInputStream()));

            String line;
            while ((line = Diskreader.readLine()) != null) {

                //split the key and the value up as I won't need to store the key.
                final String[] pieces = line.split(":", 2);
                
                    if (pieces.length > 1) {
                        String key = pieces[0];
                        String value = pieces[1];
                        
                        //line below is just to check format of output
                        System.out.println(line);

                        //store each value into a disk instance
                        
                        //add each value into the list of disks
                        
                    }
                }
        } catch (Exception e) {
            e.printStackTrace();
            e.getCause();
        }
    }
    
    //some other method here about adding into an SQL database
}

If there was just one disk it would be fine, I could just input each value into the database but in theory there could be numerous volumes on a single computer.

I'm not too worried about the SQL part at the moment. I believe if I have an array of disks I can just insert each disk into my database based on the array's index.

The values are ordered, in the order you specified them, so whenever you see a value for DriveLetter , you know the information for a new drive will follow. Use that.

diskTable.clear();
Disk disk = null;

for (String line; (line = Diskreader.readLine()) != null; ) {
    final String[] pieces = line.split(":", 2);
    if (pieces.length == 2) {
        String name = pieces[0].trim();
        String value = pieces[1].trim();

        switch (name) {
            case "DriveLetter":
                disk = new Disk();
                diskTable.add(disk);
                disk.Letter = value;
                break;
            case "FileSystemLabel":
                disk.Label = value;
                break;
            ...
            case "SizeRemaining":
                disk.Remaining = value;
                break;
            default:
                // Ignore
        }
    }
}

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