【问题标题】:Upload file to SharePoint Document library using powershell使用 powershell 将文件上传到 SharePoint 文档库
【发布时间】:2013-12-12 18:56:47
【问题描述】:

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

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

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

通过此代码,文件已上传但已检出。我希望它自动签入。如果文件在那里,则首先自动签出然后签入。

【问题讨论】:

  • 我不会把这个作为答案,因为我只是从另一个帖子中复制。这可能会让你上路,consultingblogs.emc.com/robertoortega/archive/2012/03/03/…
  • 谢谢 Ola,我已经检查了这段代码,但我无法理解,我应该如何一起使用这两个代码。上传代码后调用此代码不起作用。谢谢
  • 您可以在尝试运行时发布输出吗?
  • 窗口突然消失。我正在使用上面的代码,它只上传文件,但不适用于签入/签出。那么我们可以使用任何函数来获得它。 !!非常感谢您的支持
  • 您能否编辑您的原始问题并复制并粘贴您正在运行的脚本,就像它在 .ps1 文件中一样。

标签: sharepoint powershell sharepoint-2010 powershell-2.0 powershell-3.0


【解决方案1】:

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

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

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

【讨论】:

  • 这似乎需要Sharepoint Snapin:Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  • @Nae 在 PowerShell 中使用 SharePoint 需要先安装 SP 或 PnP 模块。对于任何特定于应用程序的 PS 脚本都是如此。
【解决方案2】:

非常感谢您的回复,我尝试按照您建议的方式进行操作。

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 是:

://abc/enterprise/en-sg/RenderingAssets/oneMSCOM/

RenderingAssets : Document Libray
OneMSCOM : folder.

【讨论】:

    【解决方案3】:

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

    我使用了这两个参考文献中的脚本:

    .

    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()
    

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

    【讨论】:

    • 代码块格式由于某种原因在 PowerShell 脚本上不起作用。它从这里开始 --> Add-PSSnapin Microsoft.SharePoint.PowerShell [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
    • 谢谢@Ola 以上我已经发布了我正在使用的脚本。它仍然抛出一些错误:char:96 + ... COM/$($.Name)",$.OpenRead(),$true) } + ~ Missing expression after ','. At D:\Development\Manila Automation \PowerShell\UploadOla.ps1:5 char:96 + ... COM/$($.Name)",$.OpenRead(),$true) } 你能告诉我我正在使用的代码中的错误是什么吗非常感谢您的支持
    • 我的猜测是格式与复制和粘贴代码无关。使用 ISE,它将帮助您并确保格式正确。
    • 我添加了一个屏幕截图,以便您可以看到格式应该是什么样子。
    【解决方案4】:
    Add-PSSnapin microsoft.sharepoint.powershell
    $web = Get-SPWeb "http://dc01:9876"
    $lst = $web.Lists.TryGetList("Style Library")
    $DirLoc = "C:\Work\Style Library\20April2015"
    
    $relativeUrl = $lst.RootFolder.ServerRelativeUrl;
    
    function UploadToLibrary($spurl,$filepath){
        Get-ChildItem -path $filepath -Directory |ForEach-Object {
            $FldrName=($spurl,$_.Name -join "/")
            $CheckFolder= $web.GetFolder($FldrName)
            if(-not $CheckFolder.Exists)
            {
                $tmp=$web.GetFolder($spurl).SubFolders.Add($FldrName);
            }
            UploadToLibrary ($spurl,$_.Name -join "/") ($filepath,$_.Name -join "\") 
        }
        $FilesToAdd=$web.GetFolder($spurl)
        Get-ChildItem -path $filepath -File |ForEach-Object {
            $_.FullName
            $bytes = [System.IO.File]::ReadAllBytes($_.FullName)
            $x=$FilesToAdd.Files.Add($_.Name,$bytes,$true);
        }
    }
    
    UploadToLibrary $relativeUrl $DirLoc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2016-12-06
      • 2013-10-08
      • 1970-01-01
      • 2010-10-10
      • 2011-08-25
      • 2010-10-01
      相关资源
      最近更新 更多