0

I have a problem with using the variables in my script coming from a csv file. Everytinme the text UNC is found, I want to do something. And when the text UNC is not found, I want to do something else. This is great for running a script locally or on the remote server.

Script

$File = (Import-Csv -Path S:\Input\Auto_Clean.csv | Select-String -NotMatch "#")

Foreach ($Item in $File) {
    If ( $SERVER -eq "UNC" ) { 
        Write-Host "UNC: $SERVER, $PATH, $OlderThanDays" } 
        else {
        Write-Host "Not UNC: $SERVER, $PATH, $OlderThanDays"}
}

CSV-file

# Input file:
SERVER, PATH, OlderThanDays
Server1, E:\Share\Dir1, 10
Server2, F:\Share\Dir2, 5
UNC, \\Domain\Share\Dir1, 30

Desired result in cosole

Not UNC: Server1, E:\Share\Dir1, 10
Not UNC: Server2, F:\Share\Dir2, 5
UNC: UNC, \\Domain\Share\Dir1, 30

The output of Write-Host $Fileis correctly displaying the labels/variants:

Write-Host $File
@{SERVER=UNC; PATH=\\domain\Share\Dir1; OlderThanDays=10}

Thank you for your help.

1 Answer 1

1

I think this is what you want:

Import-Csv -Path "S:\Input\Auto_Clean.csv" | % {
    if ($_.Server -eq "UNC")
    {
        Write-Host "UNC: $($_.Server), $($_.Path), $($_.OlderThanDays)" 
    }
    else
    {
        Write-Host "Not UNC: $($_.Server), $($_.Path), $($_.OlderThanDays)"
    }
}

This produces the following output:

Not UNC: Server1, E:\Share\Dir1, 10
Not UNC: Server2, F:\Share\Dir2, 5
UNC: UNC, \\Domain\Share\Dir1, 30

If you want to ignore lines starting with # you can use the following:

(Get-Content "S:\Input\Auto_Clean.csv") -notmatch '^#' | ConvertFrom-CSV | % {
    if ($_.Server -eq "UNC")
    {
        Write-Host "UNC: $($_.Server), $($_.Path), $($_.OlderThanDays)" 
    }
    else
    {
        Write-Host "Not UNC: $($_.Server), $($_.Path), $($_.OlderThanDays)"
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you Martin, I just tried it but it's always displaying 'Not UNC: ..' lines and the 'UNC one' line is displayed as 'Not UNC:...' too. At least it does display something this time :) Isn't there an option to use the format Foreachwithout piping?
Are you sure your CSV matches what you have posted, I have updated my answer with the output that I see?
Very odd, but it is indeed working now. I copied it again properly this time. Thank you very much Martin. Still figuring out how to have it in variable through the For each loop instead of piping.
I see now that I had my filtering in place to remove lines starting with #as in Select-String -NotMatch "#". That's why my code was not working..
Yes that's right, Select-String will return a Microsoft.PowerShell.Commands.MatchInfo object, I think you were expecting something different. You don't need the notmatch as the import-csv will do that for you.
|

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.