在powershell中从Sharepoint文档库打开xml [英] Open xml from Sharepoint document library in powershell

查看:47
本文介绍了在powershell中从Sharepoint文档库打开xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 powershell 操作来自 sharepoint 文档库的 xml 文件.不幸的是,我无法打开它并将其转换为 xml.:( 我在文档库和本地硬盘驱动器上有相同的文件.当我从硬盘驱动器打开它时一切正常.当我从 Sharepoint 文档库打开它时,转换为 xml 失败,因为在开头有一个额外的字符文件.我二进制比较了 $splistitem.File.OpenBinary() 和 get-content C:....\file.xml 的结果,它是一样的.问题是从字节中获取字符串.我尝试了所有可用的编码,但没有任何效果.这是我的代码:

I'm trying to manipulate a xml file from sharepoint document library with powershell. Unfortunately I cannot open it and cast it to xml. :( I have same file in the document library and locally on hard drive. When I open it from hard drive everything is fine. When I open it from Sharepoint Document Library the cast to xml fails because of an extra character in the beginning of the file. I binary compared the result from $splistitem.File.OpenBinary() and from get-content C:....\file.xml and its the same. The problem is with getting the string from bytes. I tried all available encodings, but nothing worked. Here is my code:

   # 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

推荐答案

我自己回答.:)

file 前面的字符是 UTF8 编码的字节顺序标记.我发现我可以直接从 sharepoint 流加载 xml 文件.这解决了问题.我的目标是在 sharepoint 库中更改 Reporting Services 数据源的连接字符串..rsds 文件是一个普通的 xml 文件,其中包含连接字符串.这是修改它的代码:

The character infront of file is a byte order mark for UTF8 encoding. I found that I can load the xml file directly from sharepoint stream. This sloves the problem. My goal was to chages the connection string for Reporting Services datasource in sharepoint library. The .rsds file is a normal xml file which holds the connection string. Here is the code to modify it:

# 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 服务.这是现在的工作代码:

Actually I realized that the above code doesn’t work. To modify reports I have to use the ReportServer web service. Here is now the working code:

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);
    }
}

这篇关于在powershell中从Sharepoint文档库打开xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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