1

I have a powershell script that runs and collects information and puts it in a .csv file. A sample of the information looks like what is listed below, with each line starting with a unique server name followed by a random unique identifier in contained a pair of ( ).

"GDR01W01SQ004 (e785754f-eeb1)","1","4","63","NY-TER-PLN-P-5N"
"GDR01L02D001 (4b889a4d-d281)","4","12","129","CO-FDP-STE-NP-5N"

I have a second powershell script that runs and takes this .csv file and its information and formats it into a report with a header and proper spacing.

Can someone please assist me with removing the text in between the ( ) as well as the ( )?

I would like the entries for each line to look like the following:

"GDR01W01SQ004","1","4","63","NY-TER-PLN-P-5N"

Thank you very much in advance!

Here is the script I have been using.

####################PowerCLI Check####################

# Verify whether the PowerCLI module is loaded, if not load it.
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin VMware.VimAutomation.Core -ErrorAction Stop
}




################### Run Time Section ####################
#This script should be run from a server that has DNS records for all entries in vcenters.txt
$file = get-Content c:\reports\vcenter\vcenters.txt  

foreach ( $server in $file) { 
Connect-VIserver -Server $server

Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes | Out-Null

} 


# Command for Custom Annotations.
Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes -expandproperty customfields  | Export-Csv -path “c:\reports\vcenter\vcenall.csv” -NoTypeInformation

# Takes vcenall.csv and sorts only the Name and Notes columns and selects all but the custom fields. Capacity Reporting script caprep.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes, Name | Select-Object Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes |Export-csv capacity.csv -NoTypeInformation

#Used to remove domain from server name
(Get-Content capacity.csv) | ForEach-Object { $_ -replace ".domain.local", "" } | Set-Content capacity.csv


# Takes vcenall.csv and sorts only the Notes column and selects only the Name and Notes columns. Nimsoft comparison script nimcomp.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes | Select-Object Name, Notes | Export-csv nimsoft.csv -NoTypeInformation

# Takes vcenall.csv and sorts only the Name columns and exports all fields. Backup/Restore comparison script bure.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Name | Export-csv bure.csv -NoTypeInformation
3
  • Can you show us the script you have? Commented Apr 16, 2015 at 14:26
  • Even though I am shooting in the dark since I don't know too much of powershell, but I am sure regex could work. Here is a link that shows how to extract contents from a pair of () you could work on that stackoverflow.com/questions/9113231/… Commented Apr 16, 2015 at 14:32
  • Does your csv data contain a header? Commented Apr 16, 2015 at 14:49

1 Answer 1

0

I think you need to add more information but just using what you have let try this one approach

Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{
    $_.server  = (($_.server).split("(")[0]).Trim()
    $_
} 

We import the csv data and assign a header. If you already have one then this parameter can be omitted.

Then we examine each row of data as an object. Change the server data by splitting it up by its spaces. If this data is for server names then it is safe to assume that that everything before the first space is the server name. This approach is dependent on the space being there. We could also use the same logic with the ( but this would be easier if the space was a guarantee.

So we update the server and then send the data back down the pipe with $_.

Sample Output

server        1 2  3   4               
------        - -  -   -               
GDR01W01SQ004 1 4  63  NY-TER-PLN-P-5N 
GDR01L02D001  4 12 129 CO-FDP-STE-NP-5N

Edit based on comments

Since it is a server display name I changed the logic to split based on the "(". Also using the Split() method instead of the -split operator.

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

2 Comments

@Jones537 glad it helped. I updated to suit your needs as well. Check the update. Also consider marking this as the answer if you feel it addressed your question.
That worked!! Thank You so Much!! I changed it a bit, as I found that I had a server display name with a space, so I just changed that first ( to a ; and it cleaned everything up perfectly: (Get-Content vcenall.csv) | % { $_ -replace "(",";" } | Set-Content vcenall.csv Import-Csv C:\reports\vcenter\vcenall.csv | ForEach-Object{ $_.Name = ($_.Name -split ";")[0] $_ } | Export-csv vcenall.csv -NoTypeInformation

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.