Reputation: 2227
#Config Variables
$SiteURL = "https://company.sharepoint.com/sites/Test-Reports"
$FolderURL= "/Shared Documents/" #Folder's Site Relative Path
$oldCount = 16
function fileOps {
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Files from the Folder
$FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File
Write-host "Total Number of Files in the Folder:" $FolderItems.Count
if ($FolderItems.Count > $script:oldCount) {
Write-host "I am here"
}
$oldCount = $FolderItems.Count
ForEach($File in $FolderItems)
{
$File.Name
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
}
For (;;) {
fileOps
timeout 20
I am trying out powershell to get a list of files from my sharedpoint site. This lists number of the files there.I am struggling with the format though and am short on time. Once a new file is dropped, I want it to print out the name of the new file that is added and break out of this loop to call a python script. This function needs to run indefinetly.
While($TRUE)
fileOps
Upvotes: 0
Views: 105
Reputation: 61028
Continuing from my comments:
script:
scoping on your $oldCount
variable inside the function, so it can update the value on that while it has been defined elsewhere$FolderItems
variable to always be an array. Arrays have a .Count
property, single items do not#Config Variables
$SiteURL = "https://company.sharepoint.com/sites/Test-Reports"
$FolderURL= "/Shared Documents/" #Folder's Site Relative Path
$oldCount = 16
function fileOps {
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive -ErrorAction Stop
#Get All Files from the Folder
# use the `@()` construct to ensure you receive an array of items
$FolderItems = @(Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File -ErrorAction Stop)
Write-host "Total Number of Files in the Folder:" $FolderItems.Count
# PowerShell uses `-gt` operator for Greater Than, not the `>`
if ($FolderItems.Count -gt $script:oldCount) {
Write-host "I am here"
}
# set the script-scoped variable to its new value
$script:oldCount = $FolderItems.Count
# output the file names
$FolderItems.Name
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
}
Upvotes: 2