共用方式為


PS script for blog: Enumeration of the files failed

Here’s the script for the Blog:  Reasons why the error “ Enumeration of the files failed ” may occur during System State backup

by Mike Rosado, posted June 18, 2010

 

Open Notepad.exe, copy & paste the script below and save it to a file named GetInvalidImagePathsV2.ps1 

###########################################################################
#
# NAME: GetInvalidImagePathsV2.ps1
#
# AUTHOR: Suhas Rao
# REVISED BY: Mark Stanfill
#
# COMMENT: This script was created to help minimize the time of parsing through the registry searching for invalid ImagePaths
# under the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices keys.
#
# For more details, search for "Enumeration of the files failed" in a blog posted on:
#          https://blogs.technet.com/b/askcore/
#
# Disclaimer:
#
# The sample scripts are not supported under any Microsoft standard support program or service.
# The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including,
# without limitation, any implied warranties of merchantability or of fitness for a particular purpose.
# The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.
# In no event shall Microsoft, its authors, or anyone else involved in the creation, production,
# or delivery of the scripts be liable for any damages whatsoever (including, without limitation,
# damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss)
# arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
#
#
# VERSION HISTORY:
# 1.0 - Initial release
# 2.0 - Included searching for spaces in the path
#
###########################################################################

$Verbose = $false;

if (($ARGS.Count -gt 0) -and ($ARGS[0] -ieq "-verbose"))
{
$Verbose = $true;
}

#
# The list of possible reasons for failure
#
$FailureReasons = @{
"INVALID_CHARS" = "The service path contains invalid characters. " +
"Characters < > : `" | ? cannot be used in a file path.";
"INVALID_FORMAT" = "The service path does not have a proper path format. " +
"Only paths beginning with [<Drive>]: format are supported.";
"DOUBLE_SLASH" = "The service path contains double inverted slashes. " +
"UNC Network paths or paths containing double inverted slashes are not supported.";
"RELATIVE_PATH" = "The service path is relative. " +
"Only absolute paths are supported.";
"FOWARD_SLASH" = "The service path contains a foward slash. " +
"Only paths containing an inverted slash are supported.";
"REPARSE_POINT" = "The service path contains a reparse point. " +
"Paths containing a reparse point are not supported.";
"UNRECOGNIZED_PATH" = "Unable to check the path. " +
"Expecting the ImagePath for the service to be a .dll or .exe";
"SPACE_IN_PATH" = "The service path contains spaces, " +
"the whole path needs to be enclosed using double quotes";

                   }

#
# The failure INVALID_CHARS can occur due to the following type of characters
#
$InvalidChars = @{
"*\*" = "DOUBLE_SLASH";
"*..*" = "RELATIVE_PATH";
"*.*" = "RELATIVE_PATH";
"*/*" = "FOWARD_SLASH"

}

#
# Display the service info
#
function PrintServiceInfo([System.Management.ManagementObject] $Service, [string] $Header,
[string] $Reason, [string] $Color)
{
$Name = $Service.Name
$Caption = $Service.Caption
$Path = $Service.PathName
$Info = $FailureReasons.Item($Reason)

    if ($Color -eq "")
{
$Color = "White"
}

    Write-Host "$Header`n" `
" Service Name : $Name`n" `
" Service Caption : $Caption`n" `
" Registry key : HKEY_LOCAL_MACHINESYSTEMCurrentControlSetservices$NameImagePath`n" `
" Value : $Path`n" `
" Reason : $Info`n" `
"`n" `
-ForeGroundColor "$Color"
}

#
# For verbose mode, print extra info for every path
#
function PrintStatus([Boolean] $IsBadPath)
{
if ($Verbose -eq $true)
{
if ($IsBadPath -eq $true)
{
Write-Host "ERROR" -ForeGroundColor Red;
}
else
{
Write-Host "OK" -ForeGroundColor Green;
}
}
}

#
# This is a core function that fetches the service path given the input from registry
# It expects the service path to be a .dll or .exe
#
function GetActualPathFromServiceImagePath([string] $ServiceImagePath)
{
$ActualPathName = $null
$IndexForPath = $null

    $ExeIndex = $ServiceImagePath.ToLower().IndexOf(".exe");
$DllIndex = $ServiceImagePath.ToLower().IndexOf(".dll");

    ##
## NOTE: Assumption is that the Service Path Always ends in dll or exe
##
if(($ExeIndex -eq -1) -and ($DllIndex -eq -1))
{
return $null
}

    ##
## If the path contains both Dll And Exe then we should use the One that Comes First
##
if(($ExeIndex -ne -1) -and ($DllIndex -ne -1))
{
if($ExeIndex -gt $DllIndex)
{
$IndexForPath = $DllIndex +4;
}
else
{
$IndexForPath = $ExeIndex +4;
}
}
else
{
if($ExeIndex -eq -1)
{
$IndexForPath = $DllIndex +4;
}
else
{
$IndexForPath = $ExeIndex +4;
}
}

    $ActualPathName = $ServiceImagePath.Substring(0,$IndexForPath)

    $Quote = "`""
if($ActualPathName.StartsWith($Quote))
{
$ActualPathName = $ActualPathName.Remove(0,1);
}

    if ($ActualPathName.StartsWith("\?") -or $ActualPathName.StartsWith("\."))
{
$ActualPathName = $ActualPathName.Substring(4);
}

    return $ActualPathName
}

##################################################################################
## Main ##
##################################################################################

$Services = Get-WmiObject Win32_Service
$BadServices = $null
$Reasons = $null
$UnrecognizedPath = 0

for ($i = 0; $i -lt $Services.Count; $i++)
{
##
## Get the actual Exe Path
##
$ActualPathName = GetActualPathFromServiceImagePath $Services[$i].PathName
if($ActualPathName -eq $null)
{
$Path = $Services[$i].PathName
$Name = $Services[$i].Name
$UnrecognizedPath = 1;

        PrintServiceInfo $Services[$i] "WARNING:" "UNRECOGNIZED_PATH" "Yellow"
continue;
}

########new
#######make sure all paths with spaces have double quotes around them

if ( ($Services[$i].PathName -match "^[^x22].*s.*.exe.*" ) -eq $true)
{
$BadServices += ,$Services[$i];
$Reasons += ,"SPACE_IN_PATH";

    PrintStatus($true);
continue;

   }

 

 

    if ($Verbose -eq $true)
{
Write-Host -nonewline "Analyzing path '$ActualPathName' ..."
}

    ##
## Check for Attributes
##
if((Test-Path -IsValid $ActualPathName) -eq $False)
{
$BadServices += ,$Services[$i] ;
$Reasons += ,"INVALID_CHARS";

        PrintStatus($true);
continue;
}

    ##
## Check for invalid chars
##
foreach ($Key in $InvalidChars.Keys)
{
$Value = $InvalidChars.Item($Key);

        if ($ActualPathName -like $Key)
{
$temp = $Key.Replace("*","")

            $BadServices += ,$Services[$i]
$Reasons += ,$Value;
}

    }

    ##
## The Start string must be in the below specified format
##
if((($ActualPathName -match "^[a-z]:\") -ne $true))
{
$BadServices += ,$Services[$i]
$Reasons += ,"INVALID_FORMAT";

        PrintStatus($true);
continue;
}

    ##
## Check for Reparse points
##
$RootPath = [System.IO.Path]::GetPathRoot($ActualPathName)

   
$Path = $ActualPathName
$DoesPathExist = Test-Path -Path $ActualPathName
while(($DoesPathExist -eq $true) -and ($Path -ne $RootPath))
{

         $h = [System.IO.File]::GetAttributes($Path);

         if ($h.CompareTo([System.IO.FileAttributes]::ReparsePoint) -ge 0)
{
$BadServices += ,$Services[$i]
$Reasons += ,"REPARSE_POINT";

            PrintStatus($true);
break;
}

         if ($Path.Contains("") -ne $true)
{
break;
}

         $strPath = $Path.Substring(0,$Path.LastIndexOf(""));
$Path = $strPath
}

    PrintStatus($false);
}

if ($BadServices.Count -gt 0)
{
echo ""
echo "Following are the service(s) found to be reporting invalid paths."
echo ""

    for ($i=0; $i-lt$BadServices.Count; $i++)
{
$Count = $i + 1
PrintServiceInfo $BadServices[$i] "$Count." $Reasons[$i]
}
}
elseif ($UnrecognizedPath -ne 1)
{
echo ""
Write-Host "No invalid service paths found." -ForeGroundColor Green
echo ""
}

Comments

  • Anonymous
    November 15, 2010
    I keep getting the following error from PoSh1Unexpected token ' ' in expression or statement.At C:UsersJESDesktopGetInvalidImagePathsV2.ps1:89 char:50               "    Service Name    : $Namen&quot;  <<<<
  • Anonymous
    February 15, 2011
    Same issue as above.  Maybe an issue of cut and paste?  Any idea how to fix?
  • Anonymous
    February 16, 2011
    The comment has been removed
  • Anonymous
    March 01, 2011
    The comment has been removed
  • Anonymous
    April 05, 2011
    The comment has been removed
  • Anonymous
    May 10, 2011
    The comment has been removed
  • Anonymous
    May 18, 2011
    The comment has been removed
  • Anonymous
    June 15, 2011
    there is one } missing at the end...
  • Anonymous
    June 27, 2012
    ########################################################################### NAME: GetInvalidImagePathsV2.ps1 AUTHOR:      Suhas RaoREVISED BY:  Mark Stanfill COMMENT: This script was created to help minimize the time of parsing through the registry searching for invalid ImagePaths         under the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices keys.          For more details, search for "Enumeration of the files failed" in a blog posted on:         blogs.technet.com/.../askcore    Disclaimer:      The sample scripts are not supported under any Microsoft standard support program or service.   The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including,   without limitation, any implied warranties of merchantability or of fitness for a particular purpose.   The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.   In no event shall Microsoft, its authors, or anyone else involved in the creation, production,   or delivery of the scripts be liable for any damages whatsoever (including, without limitation,   damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss)   arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages. VERSION HISTORY:1.0 - Initial release2.0 - Included searching for spaces in the path ###########################################################################$Verbose = $false;if (($ARGS.Count -gt 0) -and ($ARGS[0] -ieq "-verbose")){   $Verbose = $true;} The list of possible reasons for failure $FailureReasons = @{                   "INVALID_CHARS"      = "The service path contains invalid characters. " +                                          "Characters < > : &quot; | ? cannot be used in a file path.&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;INVALID_FORMAT&quot; &nbsp; &nbsp; = &quot;The service path does not have a proper path format. &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Only paths beginning with [&lt;Drive&gt;]: format are supported.&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;DOUBLE_SLASH&quot; &nbsp; &nbsp; &nbsp; = &quot;The service path contains double inverted slashes. &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;UNC Network paths or paths containing double inverted slashes are not supported.&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;RELATIVE_PATH&quot; &nbsp; &nbsp; &nbsp;= &quot;The service path is relative. &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Only absolute paths are supported.&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;FOWARD_SLASH&quot; &nbsp; &nbsp; &nbsp; = &quot;The service path contains a foward slash. &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Only paths containing an inverted slash are supported.&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;REPARSE_POINT&quot; &nbsp; &nbsp; &nbsp;= &quot;The service path contains a reparse point. &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Paths containing a reparse point are not supported.&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;UNRECOGNIZED_PATH&quot; &nbsp;= &quot;Unable to check the path. &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Expecting the ImagePath for the service to be a .dll or .exe&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;SPACE_IN_PATH&quot; &nbsp; &nbsp; &nbsp;= &quot;The service path contains spaces, &quot; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;the whole path needs to be enclosed using double quotes&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } The failure INVALID_CHARS can occur due to the following type of characters $InvalidChars &nbsp; = @{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;*&#42;&quot; &nbsp; = &quot;DOUBLE_SLASH&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;<em>..</em>&quot; = &quot;RELATIVE_PATH&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;<em>.</em>&quot; &nbsp;= &quot;RELATIVE_PATH&quot;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;<em>/</em>&quot; &nbsp; &nbsp;= &quot;FOWARD_SLASH&quot;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } Display the service info function PrintServiceInfo([System.Management.ManagementObject] $Service, [string] $Header,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[string] $Reason, [string] $Color){&nbsp; &nbsp;$Name &nbsp; &nbsp;= $Service.Name&nbsp; &nbsp;$Caption = $Service.Caption&nbsp; &nbsp;$Path &nbsp; &nbsp;= $Service.PathName&nbsp; &nbsp;$Info &nbsp; &nbsp;= $FailureReasons.Item($Reason)&nbsp; &nbsp;if ($Color -eq &quot;&quot;)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;$Color = &quot;White&quot;&nbsp; &nbsp;}&nbsp; &nbsp;Write-Host &quot;$Header<code>n &amp;nbsp; &amp;nbsp; Service Name &amp;nbsp; &amp;nbsp;: $Name</code>n &nbsp; &nbsp;Service Caption : $Caption<code>n &amp;nbsp; &amp;nbsp;Registry key &amp;nbsp; &amp;nbsp;: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetservices$NameImagePath</code>n &nbsp; &nbsp;Value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : $Path<code>n &amp;nbsp; &amp;nbsp;Reason &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: $Info</code>n n" -ForeGroundColor "$Color"} For verbose mode, print extra info for every path function PrintStatus([Boolean] $IsBadPath){   if ($Verbose -eq $true)   {       if ($IsBadPath -eq $true)       {           Write-Host "ERROR" -ForeGroundColor Red;       }       else       {           Write-Host "OK" -ForeGroundColor Green;       }   }} This is a core function that fetches the service path given the input from registryIt expects the service path to be a .dll or .exe function GetActualPathFromServiceImagePath([string] $ServiceImagePath){   $ActualPathName = $null   $IndexForPath   = $null   $ExeIndex = $ServiceImagePath.ToLower().IndexOf(".exe");   $DllIndex = $ServiceImagePath.ToLower().IndexOf(".dll");   ##   ## NOTE: Assumption is that the Service Path Always ends in dll or exe   ##   if(($ExeIndex -eq -1) -and ($DllIndex -eq -1))   {       return $null   }   ##   ## If the path contains both Dll And Exe then we should use the One that Comes First   ##   if(($ExeIndex -ne -1) -and ($DllIndex -ne -1))   {       if($ExeIndex -gt $DllIndex)       {           $IndexForPath = $DllIndex +4;       }       else       {           $IndexForPath = $ExeIndex +4;       }   }   else   {       if($ExeIndex -eq -1)       {           $IndexForPath = $DllIndex +4;       }       else       {           $IndexForPath = $ExeIndex +4;       }   }   $ActualPathName = $ServiceImagePath.Substring(0,$IndexForPath)   $Quote = "`""   if($ActualPathName.StartsWith($Quote))   {       $ActualPathName = $ActualPathName.Remove(0,1);   }   if ($ActualPathName.StartsWith("?") -or $ActualPathName.StartsWith("."))   {       $ActualPathName = $ActualPathName.Substring(4);   }   return $ActualPathName}##################################################################################                                 Main                                        ##################################################################################$Services         = Get-WmiObject Win32_Service$BadServices      = $null$Reasons          = $null$UnrecognizedPath = 0for ($i = 0; $i -lt $Services.Count; $i++){   ##   ## Get the actual Exe Path   ##   $ActualPathName = GetActualPathFromServiceImagePath $Services[$i].PathName   if($ActualPathName -eq $null)   {       $Path             = $Services[$i].PathName       $Name             = $Services[$i].Name       $UnrecognizedPath = 1;       PrintServiceInfo $Services[$i] "WARNING:" "UNRECOGNIZED_PATH" "Yellow"       continue;   }########new#######make sure all paths with spaces have double quotes around themif ( ($Services[$i].PathName -match "^[^x22].s..exe." ) -eq $true)  {   $BadServices += ,$Services[$i];   $Reasons += ,"SPACE_IN_PATH";   PrintStatus($true);   continue;  }   if ($Verbose -eq $true)   {       Write-Host -nonewline "Analyzing path '$ActualPathName' ..."   }   ##   ## Check for Attributes   ##   if((Test-Path -IsValid $ActualPathName) -eq $False)   {       $BadServices += ,$Services[$i] ;       $Reasons     += ,"INVALID_CHARS";       PrintStatus($true);       continue;   }   ##   ## Check for invalid chars   ##   foreach ($Key in $InvalidChars.Keys)   {       $Value = $InvalidChars.Item($Key);       if ($ActualPathName -like $Key)       {           $temp = $Key.Replace("","")           $BadServices += ,$Services[$i]           $Reasons     += ,$Value;       }   }   ##   ## The Start string must be in the below specified format   ##   if((($ActualPathName -match "^[a-z]:&quot;) -ne $true))   {       $BadServices += ,$Services[$i]       $Reasons     += ,"INVALID_FORMAT";       PrintStatus($true);       continue;   }   ##   ## Check for Reparse points   ##   $RootPath = [System.IO.Path]::GetPathRoot($ActualPathName)   $Path = $ActualPathName   $DoesPathExist = Test-Path -Path $ActualPathName   while(($DoesPathExist -eq $true) -and ($Path -ne $RootPath))   {        $h = [System.IO.File]::GetAttributes($Path);        if ($h.CompareTo([System.IO.FileAttributes]::ReparsePoint) -ge 0)        {           $BadServices += ,$Services[$i]           $Reasons     += ,"REPARSE_POINT";           PrintStatus($true);           break;        }        if ($Path.Contains("") -ne $true)        {            break;        }        $strPath = $Path.Substring(0,$Path.LastIndexOf(""));        $Path    = $strPath   }   PrintStatus($false);}if ($BadServices.Count -gt 0){   echo ""   echo "Following are the service(s) found to be reporting invalid paths."   echo ""   for ($i=0; $i-lt$BadServices.Count; $i++)   {       $Count   = $i + 1       PrintServiceInfo $BadServices[$i] "$Count." $Reasons[$i]   }}
  • Anonymous
    November 13, 2013
    The comment has been removed
  • Anonymous
    August 10, 2014
    I think this is the correct code you need to isolate the ReparsePoint bit:

    ##if ($h.CompareTo([System.IO.FileAttributes]::ReparsePoint) -ge 0)
    if (($h -band [System.IO.FileAttributes]::ReparsePoint) -ne 0)
  • Anonymous
    August 26, 2014
    The comment has been removed
  • Anonymous
    January 05, 2015
    The comment has been removed
  • Anonymous
    January 19, 2015
    I am facing issue with System State Backup.

    Error: System State Backup Fail with Event ID 517 and Error Code 2155347997.

    I followed the instruction but unable to get the proper way to solve it. Can anyone please drop me a email with step by step solution including the getinvalidimagepathv2 script. I am unable to find the registry entry even. This would be fine for me if anyone can extend the support for me and drop me a mail @ tilakdeb.debnath@gmail.com.
  • Anonymous
    January 28, 2019
    Where do you copy and paste from on the page? I cannot get it to run at all, can it be downloaded?