在 PowerShell 中找不到时跳过项目
Skip item when not found in PowerShell
我有这个删除 OneDrive 项目的脚本,但我只是想知道如何修改我的脚本,以便在找不到该项目时跳过该项目而不是停止执行。
现在我收到这个错误并且停止执行。
Error: Item does not exist. It may have been deleted by another user.
我应该在 forEach 中尝试 catch 吗?这会阻止它停止执行吗?
我之所以先在这里问它,是因为它删除了超过 400 万个项目,我永远不知道它什么时候会发生。
#Config Variables
$SiteURL = "https://companyname-my.sharepoint.com/personal/username/"
$ListName = "Documents"
$FolderServerRelativeURL = "/personal/sfdsf/Documents/PC-sdfd-sdf-01-Dec-0257"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -useWebLogin
$List = Get-PnPList $ListName
#Get All Items from Folder in Batch
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize 5000 -Fields ID, FileDirRef, FileRef -ScriptBlock `
{ Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
"Getting Items from Folder '$FolderServerRelativeURL'" -Status "Getting Items $global:Counter of $($List.ItemCount)"; }
#Get List Items from the folder and Sort List Items based on Server Relative URL - Descending
$ItemsFromFolder = $ListItems | Where { $_.FieldValues["FileDirRef"] -match $FolderServerRelativeURL } | Select-Object @{Name = "ServerRelativeURL"; Expression = { $_.FieldValues.FileRef } }, ID | Sort-Object -Descending -Property ServerRelativeURL
Write-host "Total Number of Items Found in the Folder:"$ItemsFromFolder.count
#Delete List Items in Batch
$Batch = New-PnPBatch
ForEach ($Item in $ItemsFromFolder) {
Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
Write-host "Item Queued for deletion:"$Item.ServerRelativeURL
Invoke-PnPBatch -Batch $Batch -Details
}
Invoke-PnPBatch -Batch $Batch -Details
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
记下引发错误的行号并将该行换行在 try/catch 中。假设该行原来是 Remove-PnPListItem 命令,那么它看起来像这样(如果你想看看缺少什么,然后取消注释 Write-Host 并根据需要修改它。):
try {
Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
} catch {
#Write-Host "Missing $ListName"
}
编辑:
这是关于“您想知道的关于异常的一切”的页面:
https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions
以达林的回答为基础。我检查了 Remove-PNPListItem 的联机文档,它没有 ErrorAction 参数,因此您需要使用 $ErrorActionPreference 变量来实际使 Catch 工作,因为它仅适用于终止错误。
$ErrorActionPreference = "Stop"
try {
Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
} catch {
#Write-Host "Missing $ListName"
}
$ErrorActionPreference = "Continue"
那是说我没有安装那个包所以你可能只想尝试将 -ErrorAction "STOP" 添加到你的 Remove-PnPListItem 并在尝试首选项变量之前看看它是否有效。
我有这个删除 OneDrive 项目的脚本,但我只是想知道如何修改我的脚本,以便在找不到该项目时跳过该项目而不是停止执行。
现在我收到这个错误并且停止执行。
Error: Item does not exist. It may have been deleted by another user.
我应该在 forEach 中尝试 catch 吗?这会阻止它停止执行吗?
我之所以先在这里问它,是因为它删除了超过 400 万个项目,我永远不知道它什么时候会发生。
#Config Variables
$SiteURL = "https://companyname-my.sharepoint.com/personal/username/"
$ListName = "Documents"
$FolderServerRelativeURL = "/personal/sfdsf/Documents/PC-sdfd-sdf-01-Dec-0257"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -useWebLogin
$List = Get-PnPList $ListName
#Get All Items from Folder in Batch
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize 5000 -Fields ID, FileDirRef, FileRef -ScriptBlock `
{ Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
"Getting Items from Folder '$FolderServerRelativeURL'" -Status "Getting Items $global:Counter of $($List.ItemCount)"; }
#Get List Items from the folder and Sort List Items based on Server Relative URL - Descending
$ItemsFromFolder = $ListItems | Where { $_.FieldValues["FileDirRef"] -match $FolderServerRelativeURL } | Select-Object @{Name = "ServerRelativeURL"; Expression = { $_.FieldValues.FileRef } }, ID | Sort-Object -Descending -Property ServerRelativeURL
Write-host "Total Number of Items Found in the Folder:"$ItemsFromFolder.count
#Delete List Items in Batch
$Batch = New-PnPBatch
ForEach ($Item in $ItemsFromFolder) {
Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
Write-host "Item Queued for deletion:"$Item.ServerRelativeURL
Invoke-PnPBatch -Batch $Batch -Details
}
Invoke-PnPBatch -Batch $Batch -Details
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
记下引发错误的行号并将该行换行在 try/catch 中。假设该行原来是 Remove-PnPListItem 命令,那么它看起来像这样(如果你想看看缺少什么,然后取消注释 Write-Host 并根据需要修改它。):
try {
Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
} catch {
#Write-Host "Missing $ListName"
}
编辑:
这是关于“您想知道的关于异常的一切”的页面: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions
以达林的回答为基础。我检查了 Remove-PNPListItem 的联机文档,它没有 ErrorAction 参数,因此您需要使用 $ErrorActionPreference 变量来实际使 Catch 工作,因为它仅适用于终止错误。
$ErrorActionPreference = "Stop"
try {
Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
} catch {
#Write-Host "Missing $ListName"
}
$ErrorActionPreference = "Continue"
那是说我没有安装那个包所以你可能只想尝试将 -ErrorAction "STOP" 添加到你的 Remove-PnPListItem 并在尝试首选项变量之前看看它是否有效。