Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

Below is the text file data I have

Disk ###  Status         Size     Free     Dyn  Gpt
--------  -------------  -------  -------  ---  ---
Disk 0    Online          100 GB  1024 KB
Disk 1    Online         6144 MB  1024 KB
Disk 2    Online          200 GB  1024 KB
Disk 3    Online           10 GB  1024 KB


I want to put this data in csv like below, removing the last 2 columns and adding the server value

server  Disk ###  Status         Size     Free     
------  --------  -------------  -------  ------- 
s1      Disk 0    Online          100 GB  1024 KB         
s1      Disk 1    Online         6144 MB  1024 KB         
s1      Disk 2    Online          200 GB  1024 KB         
s1      Disk 3    Online           10 GB  1024 KB         


What I have tried:

$list = Get-Content -Path "C:\server.txt"
foreach($server in $list)
{

    $diskpart = cmd /c 'echo list disk | diskpart'
    
}

$Lines = $diskpart
$Out = $False
$Line=@()
ForEach ($Line In $Lines)
{
    If ($Line -match 'DISKPART>') 
    {
        $Out = $False
    }
    If ($Out -eq $True) 
    {
        $Line | Out-File "C:\d.txt" -Append
    }
    If ($Line -match 'DISKPART>') 
    {
        $Out = $True
    }
}

$data  = Import-Csv -Path C:\d.txt


I tried reading line using foreach,but getting output with headers and ---

Disk ###  Status         Size     Free     Dyn  Gpt
---------------------------------------------------
Disk 0    Online          100 GB  1024 KB          


Please let me know how can I extract columns from text file and append the servername to it. Need some idea to do this
Posted
Updated 30-Sep-21 21:57pm

1 solution

You can use about Split - PowerShell | Microsoft Docs[^] to separate the fields of the input. You then just need to use Write-Output to rewrite as required.

Note that you need to call the code that does the rewriting once gfore every server. Your sample above does it for only the last server found.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900