You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#的PSCommand中使用Where-Object?绑定参数报错解决

解决C#调用PowerShell Where-Object时的FilterScript参数绑定错误

错误信息

System.Management.Automation.RemoteException: 'Cannot bind parameter 'FilterScript'. Cannot convert the "$_.InstalledOn -gt $StartTimeGenerated" value of type "System.String" to type "System.Management.Automation.ScriptBlock".'

问题代码片段

if(command == "get-hotfix")
{
    _HotFixViewAll = ReadInfo("ViewAllHotFix");
    _hotfixInterval = Convert.ToDecimal(ReadInfo("HotFixInterval"));

    if (_HotFixViewAll != "Y")
    {
        DateTime DateNow = DateTime.Now;
        TimeSpan datetimeInterval = TimeSpan.FromDays((double)_hotfixInterval);
        DateTime StartTimeGenerated = DateNow.Subtract(datetimeInterval);

        var filterScript = ScriptBlock.Create("$_.InstalledOn -gt $StartTimeGenerated");
        var whereCommand = new Command("Where-Object");
        whereCommand.Parameters.Add("FilterScript", filterScript);
        whereCommand.Parameters.Add("ArgumentList", StartTimeGenerated);
        pipeline.Commands.Add(whereCommand);
    }

    var sortCommand = new Command("Sort-Object");
    sortCommand.Parameters.Add("Property", "InstalledOn"); 
    sortCommand.Parameters.Add("Descending");
    pipeline.Commands.Add(sortCommand);
}

问题原因

脚本块中的$StartTimeGenerated是C#本地变量,PowerShell远程运行空间无法直接访问该变量;同时你通过ArgumentList传递参数的方式不正确,导致脚本块在远程执行时被错误识别为字符串类型,触发参数绑定失败。

解决方案

方案1:直接嵌入日期值到脚本块

将C#中的StartTimeGenerated转换为PowerShell可解析的ISO 8601格式字符串,直接拼接到脚本块内容中,无需额外传递参数:

if (_HotFixViewAll != "Y")
{
    DateTime DateNow = DateTime.Now;
    TimeSpan datetimeInterval = TimeSpan.FromDays((double)_hotfixInterval);
    DateTime StartTimeGenerated = DateNow.Subtract(datetimeInterval);

    // 转换为PowerShell可识别的日期字符串
    string isoDate = StartTimeGenerated.ToString("o");
    var filterScript = ScriptBlock.Create($"$_.InstalledOn -gt [DateTime]'{isoDate}'");
    
    var whereCommand = new Command("Where-Object");
    whereCommand.Parameters.Add("FilterScript", filterScript);
    
    pipeline.Commands.Add(whereCommand);
}

方案2:使用参数化脚本块传递变量

定义带参数的脚本块,通过ArgumentList将C#变量传递给PowerShell,确保远程运行空间能正确识别变量:

if (_HotFixViewAll != "Y")
{
    DateTime DateNow = DateTime.Now;
    TimeSpan datetimeInterval = TimeSpan.FromDays((double)_hotfixInterval);
    DateTime StartTimeGenerated = DateNow.Subtract(datetimeInterval);

    // 定义接收DateTime参数的脚本块
    var filterScript = ScriptBlock.Create(@"param([DateTime]$StartTime) $_.InstalledOn -gt $StartTime");
    
    var whereCommand = new Command("Where-Object");
    whereCommand.Parameters.Add("FilterScript", filterScript);
    // 传递C#变量作为脚本块参数
    whereCommand.Parameters.Add("ArgumentList", StartTimeGenerated);
    
    pipeline.Commands.Add(whereCommand);
}

注意事项

  • 若部分系统中Get-HotFix返回的InstalledOn是字符串类型,需先转换为DateTime再比较,例如:[DateTime]$_.InstalledOn -gt ...
  • 方案1适合简单场景,避免参数传递的复杂性;方案2更适合复杂逻辑,减少字符串拼接可能的格式错误。

内容的提问来源于stack exchange,提问作者NeoGenesis521

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 22:30:00