使用PowerShell和spws与GetListItems和1结果 [英] Using powershell and spws with GetListItems and 1 result

查看:98
本文介绍了使用PowerShell和spws与GetListItems和1结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试使用GetListItems并枚举结果。如果超过1,我可以枚举结果。如果只有一个结果,我无法使用相同的代码枚举结果。我如何强制它返回所有结果的对象数组,即使只有
1结果?


目前正在获得。


1结果是$ listnotify.data.row.gettype()(Name = XmlElement,BaseType = System.Xml.XmlLinkedNode)


> 1结果是$ listnotify.data.row.gettype( )(Name = Object [],BaseType = System.Array)


 [string] $ Today = [dateTime] :: Today.ToString(" yyyy-MM-dd")

$ uri =" https://blah.com/_vti_bin/lists.asmx?wsdl"

#创建服务
$ service = New-WebServiceProxy -Uri $ uri -Namespace SpWs -UseDefaultCredential

#列表名称
$ listName ="产品列表"

#创建xml查询以检索列表。
$ xmlDoc = new-object System.Xml.XmlDocument
$ queryNotification = $ xmlDoc.CreateElement(" Query")
$ queryProductNamesList = $ xmlDoc.CreateElement(" Query")
$ queryOwners = $ xmlDoc.CreateElement(" Query")
$ queryTeams = $ xmlDoc.CreateElement(" Query")
$ viewFields = $ xmlDoc.CreateElement(" ViewFields")
$ queryOptions = $ xmlDoc.CreateElement(" QueryOptions")

[string] $ Today = [dateTime] :: Today.ToString(" yyyy-MM-dd")


#Notification查询查询或结束支持通知日期达到
$ queryNotification.InnerXml = @"
< Where>
< Or>
< And>
< Eq>
< FieldRef Name =" Status" />
< Value Type =" Text"> Active< / Value>
< / Eq>
< And>
< Leq>
< FieldRef Name =" CalcReviewIntervalNotificationDa" />
< Value Type =" DateTime"> $ Today< / Value>
< / Leq>
< Eq>
< FieldRef Name =" Notify_x0020_On" />
< Value Type ='Text'> Review Interval< / Value>
< / Eq>
< / And>
< / And>
< And>
< Eq>
< FieldRef Name =" Status" />
< Value Type =" Text"> Active< / Value>
< / Eq>
< And>
< Leq>
< FieldRef Name =" calcEndOfSupportNotificationDate" />
< Value Type =" DateTime"> $ Today< / Value>
< / Leq>
< Eq>
< FieldRef Name =" Notify_x0020_On" />
< Value Type ='Text'>支持终止< / Value>
< / Eq>
< / And>
< / And>
< /或>
< / Where>
" @



$ rowLimit =" 500"
$ listNotify = $ null


#Get产品列表通知警报的结果
try {
$ service = New-WebServiceProxy -Uri $ uri - 命名空间SpWs -UseDefaultCredential
}
catch {
写错误$ _ -ErrorAction:'SilentlyContinue'
}

if($ service -ne $ null)
{
#Get产品列表需要通知警报
try {
$ listNotify = $ service.GetListItems($ listName,"",$ queryNotification,$ viewFields, $ rowLimit,$ queryOptions,"")
}
catch {
Write-Error $ _ -ErrorAction:'SilentlyContinue'
}
}

#Works,结果超过1。
$ listNotify.data.row | foreach(写主机$ _。ows_Product_x0020_Name)




感谢您提供的任何帮助!

解决方案


对于单一列表项,请使用下面的行直接获取字段值,无需foreach:

#使用1个结果。 
write-host


listNotify.data.row.ows_Product_x0020_Name


谢谢


最诚挚的问候


Hello,

I'm trying to use GetListItems and enumerate results. I can enumerate the results if there is more than 1. I cannot enumerate results utilizing same code if there is only one result. How do I force it to return an object array for all results, even if only 1 result?

Currently getting.

1 Result is $listnotify.data.row.gettype() (Name=XmlElement, BaseType=System.Xml.XmlLinkedNode)

>1 Result is $listnotify.data.row.gettype() (Name=Object[], BaseType=System.Array)

[string]$Today = [dateTime]::Today.ToString("yyyy-MM-dd")
         
$uri = "https://blah.com/_vti_bin/lists.asmx?wsdl"                 
            
# Create the service            
$service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential            
            
# The name of the list             
$listName = "Product List"             
            
# Create xml query to retrieve list.             
$xmlDoc = new-object System.Xml.XmlDocument            
$queryNotification = $xmlDoc.CreateElement("Query")            
$queryProductNamesList = $xmlDoc.CreateElement("Query")  
$queryOwners = $xmlDoc.CreateElement("Query")  
$queryTeams = $xmlDoc.CreateElement("Query")  
$viewFields = $xmlDoc.CreateElement("ViewFields")            
$queryOptions = $xmlDoc.CreateElement("QueryOptions")            

[string]$Today = [dateTime]::Today.ToString("yyyy-MM-dd")


#Notification Query looking for Review or End Of Support Notification Dates Reached
$queryNotification.InnerXml =@"
<Where>
    <Or>
        <And>
            <Eq>
                <FieldRef Name="Status" />
                <Value Type="Text">Active</Value>
            </Eq>
             <And>
                 <Leq>
                    <FieldRef Name="CalcReviewIntervalNotificationDa" />
                    <Value Type="DateTime">$Today</Value>
                 </Leq>
                 <Eq>
                    <FieldRef Name="Notify_x0020_On" />
                    <Value Type='Text'>Review Interval</Value>
                 </Eq>
             </And>
        </And>
        <And>
            <Eq>
                <FieldRef Name="Status" />
                <Value Type="Text">Active</Value>
            </Eq>
             <And>
                 <Leq>
                     <FieldRef Name="calcEndOfSupportNotificationDate" />
                     <Value Type="DateTime">$Today</Value>
                 </Leq>
                 <Eq>
                     <FieldRef Name="Notify_x0020_On" />
                     <Value Type='Text'>End Of Support</Value>
                 </Eq>
	    </And>
        </And>
    </Or>
</Where>
"@



$rowLimit = "500"            
$listNotify = $null             
           

#Get Product List Results for Notification Alerts            
try{            
    $service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential              
}            
catch{             
    Write-Error $_ -ErrorAction:'SilentlyContinue'             
}

if($service -ne $null)
    {            
    #Get Product List Notification Required Alerts
    try{                    
        $listNotify = $service.GetListItems($listName, "", $queryNotification, $viewFields, $rowLimit, $queryOptions, "")
        }            
    catch{             
        Write-Error $_ -ErrorAction:'SilentlyContinue'            
        }   
    }

#Works with more than 1 result.
$listNotify.data.row | foreach (write-host $_.ows_Product_x0020_Name)


Thanks for any assistance you can provide!

解决方案

Hi,

For the single one list item, please use the line below to get the field value directly without foreach:

#Works with 1 result.
write-host


listNotify.data.row.ows_Product_x0020_Name

Thanks

Best Regards


这篇关于使用PowerShell和spws与GetListItems和1结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆