2012-11-15 41 views
0

我想用powershell來處理sharepoint文檔庫中的xml文件。不幸的是,我無法打開它並將其轉換爲xml。 :(我在文檔庫中有相同的文件,在硬盤上有本地文件當我從硬盤打開它時,一切都很好當我從Sharepoint文檔庫中打開它時,轉換爲xml失敗的原因是因爲在開頭的額外字符我比較了$ splistitem.File.OpenBinary()和get-content C:.... \ file.xml的結果,它與二進制比較是一樣的,問題是從字節中獲取字符串,我嘗試了所有可用的。編碼,但沒有奏效這裏是我的代碼:從Powershell中的Sharepoint文檔庫打開xml

# Add SharePoint snapin if needed 
    if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)  -eq $null) 
    { 
     Add-PSSnapin Microsoft.SharePoint.Powershell 
    } 

... 
    $splistitem = ... 
... 

    $encode = New-Object System.Text.UTF8Encoding 

    [xml]$xmlFromSharepoint = $encode.GetString($splistitem.File.OpenBinary()) #throws exception 
    [xml]$xmlFromFile = get-content C:\....\file.xml #works fine 

    $web.Dispose() 

    Write-Host "Press any key to close ..." 
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 

引發的異常與消息:

 Cannot convert value "?<?xml version="1.0" encoding="utf-8"?> 
..." to type "System.Xml.XmlDocument". Error: "Data at the 
    root level is invalid. Line 1, position 1." 
    At C:\aaa.ps1:14 char:24 
    + [xml]$xmlFromSharepoint <<<< = $encode.GetString($splistitem.File.OpenBinary 
    ()) 
     + CategoryInfo   : MetadataError: (:) [], ArgumentTransformationMet 
     adataException 
     + FullyQualifiedErrorId : RuntimeException 

難道有人知道如何解決上面的代碼

在此先感謝!

回答

2

我在回答我自己。 :)

文件的字符infront是UTF8編碼的字節順序標記。我發現我可以直接從Sharepoint流加載XML文件。這解決了這個問題。我的目標是在SharePoint庫中查找Reporting Services數據源的連接字符串。 .rsds文件是保存連接字符串的普通xml文件。這裏是修改它的代碼:

# Add SharePoint snapin if needed 
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)  -eq $null) 
{ 
    Add-PSSnapin Microsoft.SharePoint.Powershell 
} 

#Configure Connection strings 
$web = Get-SPWeb "http://localhost/c1" 
$reportsList = $web.Lists | Where-Object { $_.Title -eq "Reports" } 
$dataSource = $reportsList.Items | Where-Object { $_.Name -eq "Datasource.rsds" } 
$dataSource.File.CheckOut(); 
$xml = New-Object System.Xml.XmlDocument 
$stream = $dataSource.File.OpenBinaryStream(); 
$xml.Load($stream) 
$xml.DataSourceDefinition.ConnectString = "test" 
$stream.Position = 0; 
$stream.SetLength(0); 
$xml.Save($stream); 
$dataSource.File.SaveBinary($stream) 
$dataSource.File.CheckIn(""); 
$web.Dispose() 

Write-Host "Press any key to continue ..." 
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 

其實我意識到上面的代碼不起作用。要修改報告,我必須使用ReportServer Web服務。這裏是現在的工作代碼:

function ConfigureConnectionString 
{ 
param($webUrl, $connectionString) 
    $reportServerURI = "http://$serverUrl/ReportServer" 
    $ReportPathWildCard = "/"; 
    $NameSharedSchedule="NeverExpireCache"; 

    $reportServerURI2010 = "$reportServerURI/ReportService2010.asmx?WSDL" 
    $RS = New-WebServiceProxy -Class 'RS' -NameSpace 'RS' -Uri $reportServerURI2010 -UseDefaultCredential 
    $dataSources = $RS.ListChildren($ReportPathWildCard,$true) | Where-Object {$_.TypeName -eq "DataSource" -and $_.Path -like "*$webUrl*"} 
    foreach($ds in $dataSources) { 
     $dsDefinition = $RS.GetDataSourceContents($ds.Path) 

     $dsDefinition.CredentialRetrieval = "Store" 
     $dsDefinition.Enabled = "TRUE"; 
     $dsDefinition.UserName = "domain\user"; 
     $dsDefinition.Password = "Password"; 
     $dsDefinition.ConnectString = $connectionString; 

     #Update the DataSource with this new content 
     $RS.SetDataSourceContents($ds.Path, $dsDefinition); 
    } 
}