I am new to Powershell and I have tried a few different code adjustments but keep getting stuck.
I have a CSV file from 3rd party application that I need to replace a string
host_display_name | serice_display_name | service_output |
---|---|---|
Server1 | Disk / | DISK OK - free space: / 16566 MB (86% inode=96%); |
Server1 | Disk /Logs | DISK OK - free space: / 16566 MB (86% inode=96%); |
Server1 | mem | OK - 25.7% (1023324 kB) used. |
Server2 | Disk / | DISK OK - free space: / 16566 MB (76% inode=86%); |
Server2 | Disk /Logs | DISK OK - free space: / 16566 MB (56% inode=56%); |
Server2 | mem | OK - 25.7% (1023324 kB) used. |
I would like to update it to show
host_display_name | serice_display_name | service_output |
---|---|---|
Server1 | Disk / | DISK OK - free space: / 16566 MB (86% FREE) |
Server1 | Disk /Logs | DISK OK - free space: / 16566 MB (66% FREE) |
Server1 | mem | OK - 25.7% (1023324 kB) used. |
Server2 | Disk / | DISK OK - free space: / 16566 MB (76% FREE) |
Server2 | Disk /Logs | DISK OK - free space: / 16566 MB (56% FREE) |
Server2 | mem | OK - 25.7% (1023324 kB) used. |
Here is my Powershell Code
import-csv C:\Projects\Excel_Data_Powershell\Output.csv | foreach { If($_.ou -match “inode=*%”) {$_.OU -replace “inode=*%”,”FREE”}} | export-csv C:\Projects\Excel_Data_Powershell\OutputUPDATED.csv
CodePudding user response:
You could use a calculated property with Select-Object
to recreate the service_output column based on your condition to replace the values inside the parentheses. For replacing them you can use -replace
, as for the regex being used it's likely to be improved but this seems to do the trick. See https://regex101.com/r/TrDBXu/1 for the details.
Import-Csv C:\Projects\Excel_Data_Powershell\Output.csv | Select-Object *, @{
Name = 'service_output'
Expression = { $_.service_output -replace '(\(\d %).*', '$1 FREE)' }
} -ExcludeProperty service_output |
Export-Csv C:\Projects\Excel_Data_Powershell\OutputUPDATED.csv -NoTypeInformation