0

I am filtering an export from power cli on my vcenters. I need to produce two csv files from the export Vmware creates from powercli. When I export, Esxi creates a column called GUEST then populates it with "servername.somedomain.com:Microsoft Windows Servers 2016" in the column and separates it with semicolon depending on the OS type.

I need to produce csv report that has all the servers with the same OS's and the associated columns as well as a report of all the like domain servers and associated columns.

Import csv doesn't support wildcards so I can't filter out somedomain.com nor can I filter on 2016 on input. I'm at a loss which direction to turn next and would appreciate or a concise reading on better powershell techniques to manipulate data in csv files for dummies? Below is what I use to do most of the filtering but I'm stuck on the cell described above.

 Import-CSV -Path "E:\esxi.csv | ? Folder -notlike *SharePoint*| ? Notes -notlike *Physical* | ? Notes -notlike *Sharepoint* | Export-Csv "E:\filtered.csv" -NoTypeInformation  

Here is a link to a sample csv.
https://drive.google.com/file/d/1DdqtVFVcZ0aFnoUDqB2ErNX_yA9LBO8H/view?usp=sharing

3
  • 1
    "Import csv doesn't support wildcards so I can't filter out somedomain.com" - you don't need a wildcard, you just said it was in the GUEST column? Import-Csv ... |? GUEST -notlike *somedomain.com* Commented Oct 19, 2021 at 13:49
  • 2
    Please post a representative portion of your sample data directly in the question (as an aside: the linked file isn't publicly accessible) and describe the desired output. Commented Oct 19, 2021 at 13:55
  • Mathias, I see what your saying, instead of trying to filtering "for" what I want, just filter out everything else... I'll give it a shot. Commented Oct 19, 2021 at 14:25

1 Answer 1

1

I need to produce csv report that has all the servers with the same OS

You'll want Group-Object - it'll group input objects based on some property expression, which is exactly what you need:

# Import CSV data
$data = Import-CSV -Path "E:\esxi.csv"

# Group by OS label (the substring following the last `:` in the GUEST column)
$OSGroups = $data |Group-Object { $_.GUEST -replace '^.*:(?=[^:]+$)'}

# Loop through the resulting groups and output a new CSV for each
foreach($OSGroup in $OSGroups){
  # Extract the OS name that we grouped on
  $OSName = $OSGroup.Name

  # Export relevant records to new CSV
  $OSGroup.Group |Export-Csv "PerOSReport_${OSName}.csv" -NoTypeInformation
}
Sign up to request clarification or add additional context in comments.

Comments

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.