Powershell - 无法比较Sharepoint列表中的字符串输出

时间:2016-01-13 09:16:36

标签: arrays powershell sharepoint

从SharePoint中提取列表后,我需要根据其BRTeam值验证每个项目。这是脚本:

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebUrl = "http://theoracle/WorkingHere/"
$sourceListName = "Policies & Procedures"

$spSourceWeb = Get-SPWeb $sourceWebUrl
$spSourceList = $spSourceWeb.Lists[$sourceListName]

$spSourceItems = $spSourceList.Items

$spSourceItems | ForEach-Object {
    Write-Host $_['Name']
    Write-Host $_['BRTeam']
}

代码在获取数据和将所需项目写入主机方面工作正常。

但是,如果我添加以下If-Statement来验证项目,我会看到一个错误:

if ($_['BRTeam'].Contains('HR')) {
    Write-Host $_['Name']
    Write-Host $_['BRTeam']
} 

我还尝试在分配$x -contains 'HR'后用$x = $_['BRTeam']替换布尔检查,但是这不返回任何输出(也没有错误)。错误如下:

Method invocation failed because [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue] doesn't contain a method named 'Contains'.
At line:21 char:9
+     if ($_['BRTeam'].Contains('HR')) {
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

有谁能让我知道我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我可以使用-Match运算符来解决此问题:

$spSourceItems | ForEach-Object {
    #Write-Host $_['ID']
    #Write-Host $_['Workflow Started']
    $x =  $_['BRTeam']

    if ($_['BRTeam'] -Match 'HR') {
        Write-Host $_['Name']
    }     
}

如果我担心其他一些BRTeams可能包含HR但实际上不是HR,我也可以对所有其他部门执行-NotMatch。

E.g:

$spSourceItems | ForEach-Object {
    #Write-Host $_['ID']
    #Write-Host $_['Workflow Started']
    $x =  $_['BRTeam']

    if ($_['BRTeam'] -Notmatch 'Accounts' -And $_['BRTeam'] -Notmatch 'IT') {
        Write-Host $_['Name']
    }     
}