-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

2
  • 3
    Am I wrong or did you already asked this ... Powershell scripting custom object Commented Aug 13, 2020 at 9:41
  • 1
    Import-Csv .\YourFile.csv -Delimiter ':' -Header 'ServerName','Status' Commented Aug 13, 2020 at 10:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

-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

6 Comments

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
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?
Sorry theo, you are definitely right, but it will probably be the following question.
@RoXTar well.. in that case, the standard approach of using try..catch and -ErrorAction Stop should suffice.
@theo in that case for the entire file and not for each line, right? You can't sort out individual error lines like that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.