首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用powershell将文件上载到SharePoint文档库

使用powershell将文件上载到SharePoint文档库
EN

Stack Overflow用户
提问于 2013-11-27 08:30:48
回答 4查看 59.7K关注 0票数 5

我希望将同一文件上载到所有网站集合中具有相同层次结构的多个网站集合。我想使用PowerShell并包含自动签入/签出功能。

我已经能够上传文件在SharePoint。下面是密码。:

代码语言:javascript
运行
复制
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

# create the Variable Path and Pass the source folder path
$path = “D:\ABC\DEF\26Nov\”;

# create the Variable destination and pass the URL of the SharePoint List
$destination = "complete URL of File will be mentioned here";

# Store the current user default credentials in the Variable Credentials
$credentials = [System.Net.CredentialCache]::DefaultCredentials;

# Create the object of the Webclient
$webclient = New-Object System.Net.WebClient;

# Pass the user credentials
$webclient.Credentials = $credentials; Get-ChildItem

# “For Each” loop will upload all of the files one by one onto the destination using the UploadFile method
Get-ChildItem $path | ForEach-Object { $webclient.UploadFile($destination + “/” + $_.Name, “PUT”, $_.FullName)};

通过这段代码,文件被上传,但签出。我想让它自动入住。如果文件在那里,那么首先自动签出,然后签入。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-07-24 10:17:34

下面是外行人风格的简单脚本,经过测试,可以很好地将文件从驱动器上传到SharePoint文档库

http://soreddymanjunath.blogspot.in/2014/07/add-file-to-document-library-using.html

代码语言:javascript
运行
复制
cls

asnp "*sh*"

$url=Read-Host "Enter Site Url" 

$web=Get-SPWeb -Identity $url

if($web)
{
try
{
$list = $web.Lists.TryGetList("Documents")

$files = Get-ChildItem -Path "D:\Manju" -Force -Recurse

    foreach ($file in $files)
    {
      $stream = $file.OpenRead()

      $done= $list.RootFolder.Files.Add($file.Name, $stream, $true)

      Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green         

    }
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
}

else
{
Write-Host "Site Doesn't exist"
}

$list.Update();
票数 8
EN

Stack Overflow用户

发布于 2013-11-27 10:14:58

我在服务器上成功地运行了这个脚本。它使用C:\Users\student\Documents\My Documents文件扩展名为docx将所有文档上传到一个名为Upload Demo的文档库。上传后,检查该库中的所有文档。

我使用了这两个引用中的脚本:

  • http://consultingblogs.emc.com/robertoortega/archive/2012/03/03/checking-in-all-files-in-a-document-library-from-powershell.aspx
  • http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/23/use-powershell-cmdlets-to-manage-sharepoint-document-libraries.aspx

代码语言:javascript
运行
复制
Add-PSSnapin Microsoft.SharePoint.PowerShell 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
$spWeb = Get-SPWeb -Identity "http://mia-sqlbi.adventureworks.msft/"
$spFolder = $spWeb.GetFolder("Upload Demo")
$spFileCollection = $spFolder.Files 

Get-ChildItem "C:\Users\student\Documents\My Documents" -filter ?*.docx? | ForEach {
    $spFileCollection.Add("Upload Demo/$($_.Name)",$_.OpenRead(),$true) 
} 

function global:Get-SPSite($url) {
    return new-Object Microsoft.SharePoint.SPSite($url)
}

$siteColletion = Get-SPSite("http://mia-sqlbi.adventureworks.msft/");

$folder = $siteColletion.RootWeb.Folders["Upload Demo"];

$collFiles = $folder.Files;

for ($intIndex=0; $intIndex -ne $folder.Count; $intIndex++) {
    if ($folder[$intIndex].CheckedOutBy.LoginName -eq "adventureworks\student") {
      $folder[$intIndex].CheckIn("");
   }
}

$siteColletion.Dispose()

下面是它应该是什么样子的屏幕截图:

票数 0
EN

Stack Overflow用户

发布于 2013-11-27 10:43:18

非常感谢你的回应,我试着像你建议的那样做。

代码语言:javascript
运行
复制
Add-PSSnapin Microsoft.SharePoint.PowerShell     

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

$spWeb = Get-SPWeb -Identity "//abc/enterprise/en-sg/RenderingAssets/" $spFolder =  $spWeb.GetFolder("oneMSCOM") $spFileCollection = $spFolder.Files 

Get-ChildItem "D:\test" -filter ?*.txt? | ForEach {
    $spFileCollection.Add("oneMSCOM/$($.Name)",$.OpenRead(),$true) 
}

function global:Get-SPSite($url){ 
    return new-Object Microsoft.SharePoint.SPSite($url) 
}

$siteColletion = Get-SPSite("://abc/enterprise/en-sg/RenderingAssets/");

$folder = $siteColletion.RootWeb.Folders["Upload Demo"];

$collFiles = $folder.Files;

for ($intIndex=0; $intIndex -ne $folder.Count; $intIndex++) { 
    if ($folder[$intIndex].CheckedOutBy.LoginName -eq "FAREAST\v-kisriv") { 
        $folder[$intIndex].CheckIn(""); 
    }
}

$siteColletion.Dispose()

在此,应该上载文件的完整URL是:

代码语言:javascript
运行
复制
://abc/enterprise/en-sg/RenderingAssets/oneMSCOM/

RenderingAssets : Document Libray
OneMSCOM : folder.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20237294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档