当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] OneDrive for Business:使用 PowerShell 创建文件夹

作者:精品下载站 日期:2024-12-14 15:40:01 浏览:14 分类:玩电脑

OneDrive for Business:使用 PowerShell 创建文件夹


要求:在 OneDrive for Business 网站中创建一个文件夹。

如何在 OneDrive for Business 中创建文件夹?

OneDrive 是 Microsoft 提供的一项基于云的存储服务,允许个人和团队从任何地方存储、共享和协作处理文件。如果您使用的是 OneDrive for Business,您可能需要创建一个单独的文件夹以使文件井然有序且易于查找。本文将向您展示如何在 OneDrive for Business 中创建新文件夹。

要在 OneDrive for Business 网站中创建新文件夹,请执行以下操作:

  1. 登录到您的 OneDrive 站点 (https://-my.sharepoint.com)。您可以打开 https://www.office.com/,使用您的凭据登录,然后从屏幕左上角的应用程序启动器华夫饼中单击“OneDrive”。
  2. 导航到要创建新文件夹的位置>>从工具栏中单击“新建”,然后选择“文件夹”。
  3. 为您的文件夹提供名称,然后单击“创建”按钮以在 OneDrive for Business 站点中创建文件夹。

    [玩转系统] OneDrive for Business:使用 PowerShell 创建文件夹

或者,您可以在电脑上的文件资源管理器中从 OneDrive 创建一个新文件夹。只需导航到 Windows 资源管理器中的 OneDrive 文件夹 >> 右键单击空白区域并选择“新建文件夹”。

您必须拥有网站集管理员权限才能访问其他用户的 OneDrive!如何在 Office 365 中添加对 OneDrive for Business 网站的网站集管理员访问权限?

OneDrive for Business:使用 PowerShell 创建文件夹

若要在 OneDrive for Business 站点中创建文件夹,请使用此 PowerShell 脚本。根据您的要求设置参数。


#Load SharePoint Online CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Parameters
$OneDriveSiteURL = "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"
$FolderName = "Archives"

#Setup Credentials to connect
$Cred = Get-Credential
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($OneDriveSiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the default "Documents" Library
    $List = $Ctx.Web.Lists.GetByTitle("Documents")
 
    #Check if Folder Exists already
    $Folders = $List.RootFolder.Folders
    $Ctx.Load($Folders)
    $Ctx.ExecuteQuery()
 
    #Get existing folder names
    $FolderNames = $Folders | Select -ExpandProperty Name
    if($FolderNames -contains $FolderName)
    {
        write-host "Folder Exists Already!" -ForegroundColor Yellow
    }
    else
    {
        #onedrive for business powershell create folder
        $NewFolder = $List.RootFolder.Folders.Add($FolderName)
        $Ctx.ExecuteQuery()
        Write-host "Folder '$FolderName' Created Successfully!" -ForegroundColor Green
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

请注意,您需要拥有 OneDrive 站点的权限才能运行这些命令。另外,请确保安装了 SharePoint Online PowerShell 模块(或 CSOM SDK)。

PnP PowerShell 在 OneDrive for Business 中创建文件夹

要在用户的 OneDrive 中创建文件夹,我们可以使用 Resolve-PnPFolder cmdlet。如果新文件夹尚不存在,则此 cmdlet 会创建一个新文件夹。


#Parameters
$OneDriveSiteURL = "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"
$FolderName = "Archives"
 
Try {
    #powershell connect to onedrive for business
    Connect-PnPOnline -Url $OneDriveSiteURL -Interactive
     
    #ensure folder in SharePoint Online using powershell
    Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

我们还可以使用 Add-PnPFolder cmdlet 通过 PowerShell 在 OneDrive for Business 中创建文件夹。

使用 PowerShell 确保所有 OneDrive 站点上都有一个文件夹

在运行此 PowerShell 脚本之前,请确保您拥有所有 OneDrive 站点的管理员权限。否则,您可能会收到“错误:访问被拒绝”。


#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$FolderName = "Archives"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $AdminCenterURL -Credential $Cred

    #Get All OneDrive sites
    $OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"

    #Iterate through Each OneDrive
    ForEach($Site in $OneDriveSites)
    {   
        Try {
            Write-host -f Yellow "Ensuring Folder '$FolderName' in $($Site.URL)" -NoNewline
            #Connect to OneDrive site
            Connect-PnPOnline -Url $Site.URL -Credential $Cred -ErrorAction Stop

            #ensure folder in SharePoint Online using powershell
            $NewFolder = Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName" -ErrorAction Stop

            Write-host -f Green " Done!"
        }
        Catch {
            write-host "`tError: $($_.Exception.Message)" -foregroundcolor Red
        }
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

结论

总之,使用 PowerShell 在 OneDrive for Business 中创建文件夹可以成为自动化流程和简化流程的有用工具。本指南中提供的脚本将允许您在任何 OneDrive for Business 帐户中的根文件夹或 OneDrive 中的特定路径上创建新文件夹。通过遵循本指南中概述的步骤,您将能够高效、轻松地在 OneDrive for Business 中创建新文件夹。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯