Thursday, January 4, 2018

Call a command line command and process the output


Powershell scripts can call standard command line commands and then process the output.

$FSUoutput = fsutil.exe 8dot3name query c:
$DIRXoutput = cmd.exe /c dir C:\ /x

foreach ($line in $DIRXoutput)
    {
        if ($line -like "*progra~1*") {$8DOT3NamesExist=1}
    }

if ($8DOT3NamesExist -ne 1) {$ERRORCODE+=1}


foreach ($line in $FSUoutput)
    {
        #Checks FSUtil output reports C: is 8dot3 enabled
        if ($line.StartsWith("The volume state is"))
            {
            #Checking for 0
            $line=$line.Remove(22)
            $line=$line.TrimStart("The volume state is: ")
        
            $line = $line/1
            
            $CVSTATE = $line
            
            if ($CVSTATE -ne 0) {$ERRORCODE+=$CVSTATE*10}
            
            }
            
        #Checks fSUtil output reports what the system registry setting is
        if ($line -isnot [int] -and $line.StartsWith("The registry state is: "))
        
        
            {
            $line=$line.Remove(24)
            $line=$line.TrimStart("The registry state is: ")
            
            $line = $line/1
            
            $RSTATE = $line
            

            if ($RSTATE -ne 2)
                {
                if ($RSTATE -eq 0) {$ERRORCODE+=100}
                if ($RSTATE -eq 1) {$ERRORCODE+=200}
                if ($RSTATE -eq 3) {$ERRORCODE+=300}
                }
            }        
                 

    }

exit $ERRORCODE