首页 > 解决方案 > Powershell Where-Object 用于两种模式之一

问题描述

我正在编写一个powershell脚本,它将连接到服务器并备份遵循两种模式之一的所有数据库,我当前的代码如下:

#The GetDbInstances function is imported from another file
GetDbInstances $Server $User $Password | 
    Where-Object { $_.Name -like "*Type1*" -or $._Name -like "*Type2*" } |
    ForEach-Object {
        Backup-SqlDatabase -ServerInstance $Server -Database $_.Name -BackupFile $TargetPath
    }

我的问题在于{ $_.Name -like "*Type1*" -or $._Name -like "*Type2*" },它根本无法运行并给我错误

$._Name : The term '$._Name' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is 
correct and try again.

我的问题是该表达式的正确语法是什么?

标签: powershell

解决方案


检查第二个条件变量。

它应该是$_.Name


推荐阅读