0

In my design, I encounter a problem, that is:

in database, Form table A, I defined a variable name "MyParams", its value is "1,2,3", or "2,3,5" or "5,6,9", that is, the variable will change based on the business logic.

In powershell, below script will deal with some text splitted by ~, it will check if the columns which extract from variable "MyParams" in database is blank, if any columns with blank value, it will exclude this record.

    Get-Content "${myFile}.tmp" | Where-Object { $_.Split("~")[1] -ne "" -and $_.Split("~")[2] -ne "" -and $_.Split("~")[3] -ne ""  }|Out-File $myFile -Encoding ASCII 

here , the numbers (1,2,3) in $.Split("~")[1] $.Split("~")[2] $_.Split("~")[3] come from variable "MyParams" which is configed in database.

my problem is, the MyParams's value is often change, How can I originize my above code to adjust this ?

I think maybe I need to use foreach-object inner Where-object , corrct? anyone can help me? thx.

1
  • Can you show us what your tmp file looks like? Also, what is your expected output? It's not clear what you want out of your tmp file. Commented Aug 31, 2012 at 17:58

4 Answers 4

1

Assuming $myparams is an array like 1,2,3:

Get-Content "${myFile}.tmp" | %{$splitted = $_ -split "~"; if(!($myparams | ?{$splitted[$_] -eq ""})){$_} }
Sign up to request clarification or add additional context in comments.

2 Comments

@CharlieShi - How come it didn't help? Care to explain?
thx for your help. I mean I got help from your code sample. but didn't get my code written like your code sample.
1

I had the same question and found that you need to use the script block syntax like so:

Get-Module -ListAvailable | where {($_.Name -notlike "Microsoft*" -and $_.Name -notlike "PS*") -and $_.HelpInfoUri}

This gives you access to the standard logical operators available to PowerShell instead of being limited to this from Where-Object.

Refer to Example 7 from this TechNet article.

Comments

0

well, I solved myself, I use the -and to combine all the arguments and return the bool value, have tested, this worked. below code sample may not work, you need to format it in one line, remove spare spaces.

Get-Content "${sftpFile}.tmp2" | ForEach-Object {
$line = $_.Split("~");
$columnOverseeCount = $columnsOverseeList.Count;
if($columnOverseeCount -eq 0)
{
    if (($line[4] -ne ""))
    {
    [String]::Join("~", $line);
    }
}
else
{
    $columnOverseeFlag = $true;
    foreach($columnOverseeValue  in $columnsOverseeList)
    {
        $columnOverseeFlag = $columnOverseeFlag -and  ($line[$columnOverseeValue] -ne "");
    };
    if(($line[4] -ne "") -and $columnOverseeFlag)
    {
        [String]::Join("~", $line) | Out-File -Append $sftpFile -Encoding ASCII
    }
    else
    {
        [String]::Join("~", $line) | Out-File -Append "${sftpFile}.filterByColumns" -Encoding ASCII
    }
}

}

Comments

-1

Put $myParams in a variable, then use it within Where-Object

$myParams = "1,2,3"
$indexes = [int[]] $myParams.Split(',')

Get-Content "${myFile}.tmp" | Where-Object {
  $_.Split("~")[$indexes[0]] -ne "" -and 
  $_.Split("~")[$indexes[1]] -ne "" -and 
  $_.Split("~")[$indexes[2]] -ne ""  } | Out-File $myFile -Encoding ASCII 

1 Comment

Anyone care to explain the -1s?

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.