0
从SharePoint中提取列表后,我需要根据其值BRTeam
验证每个项目。这里是脚本:Powershell - 无法比较Sharepoint列表中的字符串输出
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语句来验证的项目,我看到了一个错误:
if ($_['BRTeam'].Contains('HR')) {
Write-Host $_['Name']
Write-Host $_['BRTeam']
}
我也曾尝试分配$x = $_['BRTeam']
后$x -contains 'HR'
更换布尔检查,但是这不返回输出(也没有错误)。错误如下:
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
任何人都可以让我知道我在这里失踪了吗?
原因'.Contains'没有工作是一个字段的值是一个SPFieldValue对象,不一定是字符串。你可以将它转换为一个字符串来显示它,这是Powershell用'-match'将它与一个字符串进行比较或者用'Write-Host'将它写入控制台时所做的。使用'-match'的替代方法是$ _ ['BRTeam']。ToString()。Contains('HR')' – Thriggle