-3
   This data is store in a file

    AUTOM01-AYEHU1:No Updates Available
    AUTOM01-AYEHU2:No Updates Available
    AUTOM01-AYEHU3:No Updates Available
    AUTOM01-AYEHU4:No Updates Available
    AUTOM01-AYEHU5:No Updates Available
    AUTOM01-AYEHU6:No Updates Available
    
    

I have a above dataset in a file i need to create 2 powershell custom object with the name of (SERVERNAME,STATUS) and put respective data into it .

before : is servername and rest is status

prabhat
  • 1
  • 3
  • 3
    Am I wrong or did you already asked this ... [Powershell scripting custom object](https://stackoverflow.com/questions/63377205/powershell-scripting-custom-object) – Olaf Aug 13 '20 at 09:41
  • 1
    [`Import-Csv`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-7) `.\YourFile.csv -Delimiter ':' -Header 'ServerName','Status'` – iRon Aug 13 '20 at 10:00

2 Answers2

0

This is the answer if you really want 2 objects

$fileLines = @("AUTOM01-AYEHU1:No Updates Available","AUTOM01-AYEHU2:No Updates Available")

 $serverArray = [System.Collections.ArrayList]::new()
 $statusArray = [System.Collections.ArrayList]::new()
 foreach($fileLine in $fileLines)
 {
    $splittedLine = $fileLine.split(":")
    $serverArray.Add([PSCustomObject]@{ServerName=$splittedLine[0]})
    $statusArray.add([PsCustomobject]@{Status=$splittedLine[1]})
 }

find them in $serverArray and $statusArray

RoXTar
  • 164
  • 1
  • 13
-1

As commented in your previous question, you can simply do

$result = (Get-Content -Path 'thefile.txt' -Raw) -replace ':', '=' | ConvertFrom-StringData

ConvertFrom-StringData returns a Hashtable, in which by default the items are unordered.
As you would like to keep the order as in the input file, you may want to use this instead:

$result = Get-Content -Path 'D:\Test\thefile.txt' | ConvertFrom-String -Delimiter ':' -PropertyNames Server, Status

To return an array of PSCustomObjects:

Server         Status              
------         ------              
AUTOM01-AYEHU1 No Updates Available
AUTOM01-AYEHU2 No Updates Available
AUTOM01-AYEHU3 No Updates Available
AUTOM01-AYEHU4 No Updates Available
AUTOM01-AYEHU5 No Updates Available
AUTOM01-AYEHU6 No Updates Available
Theo
  • 57,719
  • 8
  • 24
  • 41
  • This is working but order is not correct i mean in file the order of entry is different but through script order got changed so can we fix that – prabhat Aug 13 '20 at 11:09
  • this solution is fast and short, but if you want to log error entries and continue working with valid ones, this solution won't make you happy. Or am I missing something? – RoXTar Aug 14 '20 at 09:48
  • Sorry theo, you are definitely right, but it will probably be the following question. – RoXTar Aug 14 '20 at 13:18
  • @RoXTar well.. in that case, the standard approach of using `try..catch` and `-ErrorAction Stop` should suffice. – Theo Aug 14 '20 at 13:23
  • @theo in that case for the entire file and not for each line, right? You can't sort out individual error lines like that. – RoXTar Aug 14 '20 at 13:35
  • @RoXTar What are you hammering on about? – Theo Aug 14 '20 at 13:38