在PowerShell中使用C#源代码强制转换

在PowerShell中使用C#源代码强制转换,c#,powershell,C#,Powershell,我正在编写的PowerShell脚本中有一个自定义的C#方法。该函数的目标是确定文件中是否存在某一行。以下是我的资料来源: $source = @" using System; public class Differ { public static bool isInFile(Array file, String line) { foreach(string curLine in file) { if(curLine.Re

我正在编写的PowerShell脚本中有一个自定义的C#方法。该函数的目标是确定文件中是否存在某一行。以下是我的资料来源:

$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
运行脚本时,会出现以下错误:

Exception calling "isInFile" with "2" argument(s): "Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.String'."
At 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

我猜它在我试图传递给isInFile的第二个对象上出错,但我不确定。我已验证PowerShell是否正确地将这两个文件加载到它们的变量中,并且$theLine变量中有一个字符串。

您的函数声明是错误的。您需要使用
string[]
而不是
Array
。替换:

public static bool isInFile(Array file, String line)

PS代码([string]和[array])中的强制转换是不必要的

就我个人而言,我会跳过c#,使用内置的powershell操作符,就像这样。它更干净。如果我需要尽可能减少纳秒数,我宁愿只加载C代码,但同样,编译后的应用程序会更好:-)尝试以下方法:

$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

当您执行
$theLine | GM
时,是否表示对象类型为
System.String
$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