使用powershell将文件上载到SharePoint文档库
我想将同一文件上载到所有网站集中具有相同层次结构的多个网站集。我想使用PowerShell并包含自动签入/签出功能 我已经能够在SharePoint中上载该文件。下面是代码:使用powershell将文件上载到SharePoint文档库,sharepoint,powershell,sharepoint-2010,powershell-2.0,powershell-3.0,Sharepoint,Powershell,Sharepoint 2010,Powershell 2.0,Powershell 3.0,我想将同一文件上载到所有网站集中具有相同层次结构的多个网站集。我想使用PowerShell并包含自动签入/签出功能 我已经能够在SharePoint中上载该文件。下面是代码: [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null # create the Variable Path and Pass the source folder path $path = “D:\ABC\D
[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)};
根据此代码,文件已上载但已签出。我希望它能自动签入。如果文件存在,则首先自动签出,然后签入。我在服务器上成功运行了此脚本。它使用
docx
文件扩展名将C:\Users\student\documents\My documents
中的所有文档上载到名为Upload Demo
的文档库中。上传后,它将签入该库中的所有文档
我使用了这两个参考中的脚本:
非常感谢您的回复,我尝试了与您建议的相同的方式
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.
这是一个外行风格的简单脚本,经过测试,可以很好地将文件从驱动器上载到SharePoint文档库
我不打算把这个作为答案,因为我只是从另一篇文章中抄袭。这可能会让你的方式,谢谢奥拉,我已经检查了这个代码,但我不能理解,我应该如何使用这两个代码在一起。上传代码后调用此代码无效。谢谢您在尝试运行输出时发布输出?窗口突然消失。我正在使用上面的代码,它只上传文件,但不适用于签入/签出。因此,我们可以使用任何函数来获得它!!非常感谢您的支持您可以编辑原始问题并复制和粘贴您正在运行的脚本,与.ps1文件中的脚本完全相同。由于某些原因,PowerShell脚本上的代码块格式不起作用。从这里开始-->在Microsoft.SharePoint.PowerShell[System.Reflection.Assembly]中添加PSSnapin::LoadWithPartialName(“Microsoft.SharePoint”)>$NullThank@Ola以上我已经发布了我正在使用的脚本。它仍然抛出一些错误:char:96+。。。COM/$($.Name)”,$.OpenRead(),$true)}+~在“,”之后缺少表达式。位于D:\Development\Manila Automation\PowerShell\UploadOla.ps1:5 char:96+…COM/$($.Name)”,$.OpenRead(),$true)}你能告诉我我使用的代码中有什么错误吗非常感谢你的支持我猜复制和粘贴代码的格式已经失效了。使用ISE,它将帮助您并确保格式正确。我添加了一个屏幕截图,以便您可以看到格式应该是什么样子。这似乎需要:
Get-SPWeb:术语“Get-SPWeb”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
@Nae在PowerShell中使用SharePoint时,需要先安装SP或PnP模块。对于任何特定于应用程序的PS脚本都是如此。
://abc/enterprise/en-sg/RenderingAssets/oneMSCOM/
RenderingAssets : Document Libray
OneMSCOM : folder.
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();
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