比较powershell中具有通配符的2个阵列
假设我们有这样的2.txt文件: users.txt比较powershell中具有通配符的2个阵列,powershell,Powershell,假设我们有这样的2.txt文件: users.txt user.name1 user.name2 admin.name1 admin.name2 shares.txt \\someserver\somefolder\user.name1 \\someserver\somefolder\user.name2 \\someserver\someotherfolder\admin.name1 \\someotherserver\somefolder\admin.name2 \\someplaceel
user.name1
user.name2
admin.name1
admin.name2
shares.txt
\\someserver\somefolder\user.name1
\\someserver\somefolder\user.name2
\\someserver\someotherfolder\admin.name1
\\someotherserver\somefolder\admin.name2
\\someplaceelse\somefolder\no.name
我想比较这两个数据点,只显示“shares.txt”中与“users.txt”中的名称匹配的行。我想我们需要使用某种通配符搜索,因为用户名总是包含在uncpath中,但不会完全相同,下面是一个我迄今为止尝试但未成功的示例
$users = Get-Content ".\users.txt"
$shares = Get-Content ".\shares.txt"
foreach ($line in $users)
{
if ("*$line*" -like "$shares")
}
Write-Host "this line matches $line"
}
}
要获得预期信息,必须在搜索中包含循环的当前值
代码中的问题是like运算符只允许表达式右侧的通配符。因此,匹配必须以相反的方式进行。
选择字符串-路径“\shares.txt”-模式(获取内容“\users.txt”)-SimpleMatch
欢迎使用堆栈溢出!你能不能补充一些细节,说明为什么这会起作用?仅代码的答案不是很有用,可能会被删除。这确实符合我的意图,因此请保留它,这里的问题是我在查询“$\ux”中没有使用当前值。简单的错误。我还需要添加“$line”,因为路径的末尾可能也有一些行话。
foreach ($line in $users)
{
if ($shares -like "*$line*")
{
Write-Host "$($shares | where {$_ -like "*$line*"}) this line matches $line"
}
}