Casting with C# source code in PowerShell -


i have custom c# method in powershell script writing. goal of function figure out if line exists in file. here source:

$source = @" using system;  public class differ {     public static bool isinfile(array file, string line)     {         foreach(string curline in file)         {             if(curline.replace(" ", "") == line.replace(" ", ""))             {                 return true;             }         }          return false;     } } "@  add-type -typedefinition $source  $file1 = get-content "path\to\file" $file2 = get-content "path\to\file"  $diff = new-object system.collections.specialized.ordereddictionary  $linenumber = 0 $file1 | foreach { [string]$theline = $_      if ([differ]::isinfile([array]$file2, [string]$theline) -eq $false -and -not [system.string]::isnullorwhitespace($theline)) {         $diff.add($linenumber, $_.tostring().trim())     }      $linenumber++ }  $diffheader =  @{expression={$_.name};label="line"}, ` @{expression={$_.value};label="content"}  $diff | sort-object line | format-table $diffheader -autosize 

it gives me following error when run script:

exception calling "isinfile" "2" argument(s): "unable cast object of type 'system.management.automation.psobject' type 'system.string'." @ c:\users\jcarl\dropbox\powershell\compare_config\comapre.ps1:33 char:9 +     if ([differ]::isinfile([array]$file2, [string]$theline) -eq $false) { +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : notspecified: (:) [], methodinvocationexception     + fullyqualifiederrorid : invalidcastexception 

i guessing erroring on second object trying pass in isinfile, not sure. have verified powershell loads both files variables correctly , there string in $theline variable.

your function declaration wrong. need use string[] instead of array. replace:

public static bool isinfile(array file, string line) 

with

public static bool isinfile(string[] file, string line) 

and casts in ps code([string] , [array]) unnecessary.

personally, skip c# , use built-in powershell operators this. it's cleaner. prefer load c#-code if need shave off many nanoseconds possible, again, compiled application better :-) try this:

$file1 = get-content "path\to\file" $file2 = get-content "path\to\file"   $diff = new-object system.collections.specialized.ordereddictionary  $linenumber = 0 $trimmedfile2 = $file2 | % { $_ -replace " " }  $file1 | foreach {      if($trimmedfile2 -notcontains ($_ -replace " ") -and -not [system.string]::isnullorwhitespace($_)) {         $diff.add($linenumber, $_.trim())     }      $linenumber++ }  $diffheader =  @{expression={$_.name};label="line"}, @{expression={$_.value};label="content"}  $diff | sort-object line | format-table $diffheader -autosize 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -