OneDrive
参考文献
Json 参数
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/driveitem?view=odsp-graph-online
流程参考
1.https://github.com/MarcelMeurer/PowerShellGallery-OneDrive
2.https://www.sepago.de/blog/onedrive-powershell-module-new-version-with-improved-authentication/
另一个方法的url
https://answers.microsoft.com/en-us/msoffice/forum/all/how-do-i-creategenerate-a-list-of-folders-or-files/d9a33a45-a22d-48d5-9f68-63a82078060a
同样需要配置PowerShell 和SharePoint Online(或错误信息为“Connect-PnPOnline”未被识别为 cmdlet 的名称)
如果您的主操作系统是 Windows 10,或者安装了PowerShellGet,则可以运行以下命令来安装 PowerShell cmdlet:
在线 SharePoint:Install-Module SharePointPnPPowerShellOnline
SharePoint 2016:Install-Module SharePointPnPPowerShell2016
SharePoint 2013:Install-Module SharePointPnPPowerShell2013
您可以从PnP PowerShell 存储库的发布部分下载安装文件。这些文件将每月更新一次。运行安装并重新启动任何打开的 PowerShell 实例以使用 cmdlet。
https://github-com.translate.goog/pnp/PnP-PowerShell/releases?_x_tr_sl=en&_x_tr_tl=zh-CN&_x_tr_hl=zh-CN&_x_tr_pto=sc
如果您没有计算机的本地管理员访问权限,您也可以添加“-Scope CurrentUser”来安装它。Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser
PnP PowerShell下载原帖为:
https://sharepoint-stackexchange-com.translate.goog/questions/204052/connect-pnponline-is-not-recognized-as-the-name-of-a-cmdlet?_x_tr_sl=en&_x_tr_tl=zh-CN&_x_tr_hl=zh-CN&_x_tr_pto=sc
配置
下载打开 PowerShell
Install-Module -Name OneDrive -Scope CurrentUser -force
检查安装
Get-Help -Name OneDrive
OneDrive网址(本案例为OneDrive 个人版)
https://portal.azure.com/#home
找到 “注册应用程序”
受支持的账户类型可以自行选择
重新定向url可以在此处选择
此处我选择 WEB URL为:http://localhost:3001/redirect
点击注册
在 “证书和密码”选项中添加新的客户端密码
此处值需要保存下来,值只显示一次,之后将会隐藏
选择“身份验证”勾选ID令牌,允许后面访问进行验证。
配置清单
"signInAudience": "AzureADandPersonalMicrosoftAccount",
"tags": [
"supportsConvergence:true",
"availableToOtherTenants:true",
"accessTokenVersion:1",
"appModelVersion:2"
],
"tokenEncryptionKeyId": null,
"verifiedPublisher": {
"displayName": null,
"verifiedPublisherId": null,
"addedDateTime": null
}
}
获取身份令牌,确保访问成功(PowerShell运行)
已有数数据:
客户端ID:0576a249-34b4-461d-90ee-2570a376e6c8 (点击概述,客户端ID)
应用密钥:mra8QxYpZwOCTyFV6xQNhKiURwziHL5Xf4ZcwY
重定向URI:http://localhost:3000/redirect (自定义URL)
获取身份令牌
Get-ODAuthentication -ClientID 0576a249-34b4-461d-90ee-2570a376e6c8 -AppKey mra8Q~xYpZwOCTyFV6xQNhKiURwziHL5Xf4Z~cwY -RedirectURI http://localhost:3000/redirect
有效期为一小时(‘保存为变量’$Auth=)
提示:使用“-AutoAccept”以避免确认窗口
$Auth= Get-ODAuthentication -ClientID 0576a249-34b4-461d-90ee-2570a376e6c8 -AppKey mra8Q~xYpZwOCTyFV6xQNhKiURwziHL5Xf4Z~cwY -RedirectURI http://localhost:3000/redirect -RefreshToken $Auth.refresh_token
下图为访问成功
列出 OneDrive 文件夹中的文件:
Get-ODChildItems -AccessToken $Auth.access_token -Path "/BACKUP Azure Global Bootcamp"
在画面中显示内容
提示:对于 OneDrive 个人版,将 ResourceId 留空 (-ResourceId “”)
打印$Auth参数
echo $Auth
显示$Auth.access_token
Get-ODChildItems -AccessToken $Auth.access_token -ResourceId "" -path "/"
显示name,size,shared列 文档在最上面
Get-ODChildItems -AccessToken $Auth.access_token -ResourceId "" -path "/" -SelectProperties "name,size,shared"
数组保存至 $r
$r=Get-ODChildItems -AccessToken $Auth.access_token -ResourceId "" -path "/" -SelectProperties "id,name,size,shared"
显示$r
echo $r
显示$r第一条的name值
echo $r[0].id
根据id查找对应的文件,输出大小(可以在git.md中查找资料)
替换 -path为-Elementid
$r=Get-ODChildItems -AccessToken $Auth.access_token -ResourceId "" -Elementid "4A9F64E3919132!1916" -SelectProperties
配置好后运行脚本
1 上面也运行过了,需要重新获取时运行
$Auth=Get-ODAuthentication -ClientID 0576a249-34b4-461d-90ee-2570a376e6c8 -AppKey mra8Q~xYpZwOCTyFV6xQNhKiURwziHL5Xf4Z~cwY -RedirectURI http://localhost:3000/redirect
// 专业版需要添加
-ResourceId "https://onedrive.live.com/"
// 清空 D盘下文件
"Type`tPath`tName`tSize" | Out-File d:\result.txt
2 配置代码 复制粘贴(保存地址为d:\result.txt,没有的话会自动添加)
function Get-My-OD-Files ($path)
{
$Content=@(Get-ODChildItems -AccessToken $Auth.access_token -ResourceId $resourceIdGiven -Path $path)
$CountFiles=@($Content|where {!$_.folder}).count
$CountFolders=@($Content|where {$_.folder}).count
if ($Verbose) {write-host("Current folder: '"+$path+"' (contains folders/files: "+$CountFolders+"/"+$CountFiles+")")}
$global:AllFiles+=$CountFiles
$global:AllFolders+=$CountFolders
$ErrorActionPreference = "Stop"
foreach ($entry in $content)
{
if ($entry.folder)
{
if ($Verbose) {write-host ("Found folder '"+$entry.name+"'")}
$NewPath=($path+"/"+$entry.name).Replace("//","/")
Get-My-OD-Files($NewPath)
$output = "Folder`t"+$NewPath+"`t`t"+$entry.size
$output | Out-File -Append d:\result.txt
} else
{
if ($Verbose) {write-host("Download file "+$entry.name+ " to "+$LocalPath)}
$global:size+=$entry.size
$output = "File`t"+$path.Replace("//","/")+"`t"+$entry.name+"`t"+$entry.size
$output | Out-File -Append d:\result.txt
}
}
}
1 先进行参数配置的运行
$global:size=0
$global:AllFiles=0
$global:AllFolders=0
$Verbose=$true
// 此处为注释
// 个人版运行时保持为空。
// 专业版登录时url为(登录OneDrive网站后用户上方url https://onedrive.live.com/)
$resourceIdGiven=""
3 运行指令
Get-My-OD-Files "/"
write-host ("Number of files: "+$global:AllFiles)
write-host ("Number of folders: "+$global:AllFolders)
write-host ("Bytes transfered: {0:N0}" -f $global:Size)
通过方法退出当前登录的账号
Get-ODAuthentication -ClientID 0576a249-34b4-461d-90ee-2570a376e6c8 -AppKey mra8Q~xYpZwOCTyFV6xQNhKiURwziHL5Xf4Z~cwY -RedirectURI http://localhost:3000/redirect -LogOut $true