Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
New Check - SYS24 - Search For A File
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Random-Thoughts committed Apr 6, 2018
1 parent a4c96c3 commit a766376
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions checks/system/sys-24-search-for-a-file.ps1
@@ -0,0 +1,77 @@
<#
DESCRIPTION:
Allows you to search for a specific file and fails on the result.
Note, depending on the number of files on the server, this check may have a high chance of timing out.
REQUIRED-INPUTS:
FileName - Name of the file to search for (do not inclue any paths). Wildcards are not supported.
FailOnFound - "True|False" - If the file is found, should the check return a fail result
DEFAULT-VALUES:
FileName = 'should-not-exist.txt'
FailOnFound = 'True'
DEFAULT-STATE:
Disabled
RESULTS:
PASS:
One or more matching files were found
No matching files were found
WARNING:
FAIL:
One or more matching files were found
No matching files were found
MANUAL:
NA:
APPLIES:
All Servers
REQUIRED-FUNCTIONS:
None
#>

Function sys-24-search-for-a-file
{
$result = newResult
$result.server = $env:ComputerName
$result.name = $script:lang['Name']
$result.check = 'sys-24-search-for-a-file'

#... CHECK STARTS HERE ...#

Try
{
[int] $index = ($script:chkValues['FileName']).LastIndexOf('.')
[string] $name = ($script:chkValues['FileName']).Substring(0, $index)
[string] $ext = ($script:chkValues['FileName']).Substring($index + 1)
[string] $filter = "filename='$name' AND extension='$ext'"
[string[]]$gCIMi = @((Get-CimInstance -ClassName 'CIM_DataFile' -Filter $Filter -ErrorAction SilentlyContinue).Name)

If ([string]::IsNullOrEmpty($gCIMi) -eq $false)
{
# One or more files found
$result.message = $script:lang['dt01']
$gCIMi | Sort-Object | ForEach-Object -Process { $result.data += '{0},#' -f $_ }
}
Else
{
# No files found
$result.message = $script:lang['dt02']
}

$result.message += ",#'$($script:chkValues['FileName'])'"

If ([string]::IsNullOrEmpty($result.data) -eq ($script:chkValues['FailOnFound'] -as [boolean])) { $result.result = $script:lang['Pass'] }
Else { $result.result = $script:lang['Fail'] }
}
Catch
{
$result.result = $script:lang['Error']
$result.message = $script:lang['Script-Error']
$result.data = $_.Exception.Message
}

Return $result
}

0 comments on commit a766376

Please sign in to comment.