如何使用PnP

时间:2019-07-24 15:33:29

标签: powershell sharepoint-online

我正在尝试使用Add-PnPFile将文件共享从本地计算机上传到SharePoint,我也拥有具有每个文件的所有属性(“修改者”,“创建者”)的csv。

我在下面编写了这段代码,以获取csv文档中文件的所有属性,并进行了测试,以查看用户在使用Add-PnPFile命令上传文件之前是否已存在于租户中。

Function Upload-Nom{
    Param (
            [parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            [Alias('FullName')]
            [string[]]$Path = $PWD
            )
    Begin {}
    Process {
            ForEach ($item in $Path) {
                #iterate all the file urls in the csv
                Import-Csv $item | ForEach-Object {



                                    #capture all the properties you need to update the file properties on sharepoint
                                    $name = $_.Name
                                    $fullName = $_.FullName
                                    $itemtype = $_.'Item Type'
                                    $relativepath = $_.Path -replace '(sites\/honours\/)'
                                    $modifiedbyuser = $_.'Modified By'
                                    $createdbyuser = $_.'Created By'
                                    $modified = $_.Modified


                                    $path = $_.Path -replace '\/','\'
                                    $path = $path -replace '(sites\\honours\\)'
                                    $fullurl ="C:\Users\modonny\Downloads\" +$path+"\"+ $name



                                    #convert dates to SP format
                                    [DateTime]$dateformats = New-Object System.DateTime;
                                    if([DateTime]::TryParse($_.Modified, [ref]$dateformats)){            
                                       $cdob = $dateformats;            
                                    }

                                    $modifieduser = Get-PnPUser | ? Title -eq $modifiedbyuser
                                    $createduser = Get-PnPUser | ? Title -eq $createdbyuser
                                    #check if user exists in tenancy
                                    if($modifieduser){
                                        $muserid = $modifiedbyuser.Email

                                    }else{

                                        $muserid = "john.doe@test.gov.uk"

                                    }

                                    if($createduser){
                                        $cuserid = $createduser.Email

                                    }else{
                                        $createduser = Get-PnPUser | ? Email -EQ "john.doe@test.gov.uk"
                                        $cuserid = "john.doe@test.gov.uk"

                                    }



                                    $object = @{}
                                    $object.Add("Modified",$cdob)
                                    $object.Add("Editor" ,$muserid)
                                    $object.Add("Author" ,$cuserid)



                                        if($fullurl | Test-Path){
                                           if($itemtype -eq 'Folder'){
                                                write-host "this item is a folder"

                                            }else{

                                                #upload files to sharepoint with the data in the $object variable
                                                Add-PnPFile -Path $fullurl -Folder $relativepath -Values $object

                                            }

                                        }

                                    }

             }


    }


Upload-Nom -Path "C:\Users\modonny\Documents\testing.csv"

代码运行完毕后,所有文件都将上载,但“修改者/创建者”属性不是。

1 个答案:

答案 0 :(得分:0)

示例脚本供您参考。

#region Variables 
$Username = "user@tenant.onmicrosoft.com" 
$Password = "password" 
$siteURL = "https://tenant.sharepoint.com/sites/Community" 

#endregion Variables

#region Credentials 
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force 
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePass) 
#endregion Credentials

Connect-PnPOnline -Url $siteURL -Credentials $PSCredentials

$user=Get-PnPUser | ? Email -eq "user1@tenant.onmicrosoft.com"
Add-PnPFile -Path "C:\Lee\test.docx" -Folder "MyDoc" -Values @{Editor=""+$user.Id+"";Modified="7/24/2019"}