0

the idea is to import a csv, and then if the value "infohostname" contains a nullorwithespace delete the entire line

    Function Last_NAS_Parse {

$Import_IP = Import-Csv -Path "$destination_RAW_NAS\audit_nas_8_$((Get-Date).ToString('yyyy-MM-dd')).txt" -Header @("date","infohostname","Version","SMTP","Value_1","Value_2","Value_3","Value_4","Value_5","Value_6","Value_7","Value_8","Value_9")
$Import_IP | ForEach-Object {
   if ( [string]::IsNullOrWhiteSpace($_.infohostname)


  }

But don't know how can i delete the line after this is selected, thanks.

2
  • Firstly, your code is missing parentheses and brackets in order to compile as posted. Is your goal to delete the entire row from the file, or simply clear it? Commented Nov 23, 2018 at 19:10
  • @trebleCode the idea is just to delete the entire row from the file Commented Nov 23, 2018 at 19:12

2 Answers 2

2

IMO you don't need a function just a Where-Object:

$Header = ("date","infohostname","Version","SMTP","Value_1","Value_2","Value_3","Value_4","Value_5","Value_6","Value_7","Value_8","Value_9")
$Import_IP = Import-Csv -Path "$destination_RAW_NAS\audit_nas_8_$((Get-Date).ToString('yyyy-MM-dd')).txt" -Header $Header | 
   Where-Object {![string]::IsNullOrWhiteSpace($_.infohostname)}

But of course you could wrap that in a function
(but a function without passed parameters and returned values isn't a real function)

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

Comments

0

Loop over the results and store only objects where that boolean evaluation is true, and create a new file. In order to delete the row of the existing file, I believe you'd have to convert it to XLS/X and access it as a COM object

$results = @()
Function Last_NAS_Parse {

    $Import_IP = Import-Csv -Path C:\path\to\file.csv
    $Import_IP | ForEach-Object {
        if ( [string]::IsNullOrWhiteSpace($_.infohostname)) 
        {
            Out-Null
        }
        else
        {
            $results += $_
        }
    }
}

Last_NAS_Parse
$results | Export-CSV "C:\export\path\of\newfile.csv"

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.