locked
PowerShell ver. 2.0: Possible to list amount of files in a folder without counting sub folders? RRS feed

  • Question

  • So, I finished a script which is going to be used for monitoring purposes with SCOM. It's quite simple; count files in a single folder and output the data to a text file which will then be read by SCOM. In a few cases, restart a service when a certain threshold is exceeded.

    Everything was tested and ready to be rolled out into production, when I found out that the particular server the script will be running on, only runs PowerShell 2.0 (see version numbers below). And 2.0 apparently does not support Get-ChildItem -File (the -File parameter). So I don't know how to filter out the sub folders in the relevant folders where files need to be counted.

    PS C:\Users\xxx> $PSVersionTable

    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.4253
    BuildVersion                   6.0.6002.18111
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    PSRemotingProtocolVersion      2.1

    Relevant part of the script:

    [int]$threshold = 58
    $fileCount = (Get-ChildItem $folder).Count
    $filecount > $loggfil
    if ($fileCount -gt $threshold){
    Restart-Service servicename}

    The problem with this is that I have no control over the amount of sub folders in the folder, and one of the scripts is supposed to be set up with a service restart when the threshold exceeds 0. So now I've had to add the amount of folders to the threshold, which is definitely not best practice. If someone adds a new folder, then the particular service will be restarted every time the script runs.

    Anyone know of a solution (in PS 2.0)?


    Friday, October 13, 2017 11:09 AM

Answers

  • Get-ChildItem <path> |Where{-not $_.PsIsContainer}

    \_(ツ)_/

    Friday, October 13, 2017 11:18 AM

All replies

  • Get-ChildItem <path> |Where{-not $_.PsIsContainer}

    \_(ツ)_/

    Friday, October 13, 2017 11:18 AM
  • Seems to work perfectly, thanks!
    Monday, October 16, 2017 8:21 AM