我想将同一文件上传到所有网站集中具有相同层次结构的多个网站集。我想使用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)};
通过此代码,文件已上传但已签出。我想让它自动检查。如果文件在那里,则首先自动签出然后签入。
请帮助我,因为我已经花了2天时间。但没有任何反应 非常感谢您的支持
此致
基尚
答案 0 :(得分:8)
以下是外行样式的简单脚本,经过测试,可以很好地将文件从驱动器上传到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();
答案 1 :(得分:0)
我在服务器上成功运行了此脚本。它将C:\Users\student\Documents\My Documents
中带有docx
文件扩展名的所有文档上传到名为Upload Demo
的文档库。上传后,它会检入该库中的所有文档。
我使用了这两个引用的脚本:
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()
以下是屏幕截图:
答案 2 :(得分:0)
非常感谢您的回复,我尝试按照您的建议进行操作。
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 :(得分:0)
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