• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

Powershell to read text file

We use a different method to deploy patches. Everything is going well, I just need to audit the patches whether they are installed or not, so during the installation of the these patches it creates 2 files in C:\Installs\Patches folder. The file names of these files will be like this.

KBxxxxxx.RTInst.txt
KBxxxxxx.Verify.txt
KByyyyyy.RTInst.txt
KByyyyyy.Verify.txt

What I am looking is a PowerShell script, that will read the last 3rd line of KBxxxxxx.Verify.txt file.

I will provide a computers.txt with all hostnames and modules.txt with the KB article numbers, then the script should check whether the KBxxxxxx.RTInst.txt exists, if yes then should go ahead and check for KBxxxxxx.Verify.txt exists, if yes then it should read the last 3rd line of it and if the line says “Hotfix successfully installed” then write the output to a text or csv file in this fashion.

HOSTNAME,KBxxxxxx,Success
HOSTNAME,KByyyyyy,Fail
HOSTNAME,KBzzzzzz,NoRTInst
HOSTNAMe,KBaaaaaa,NoVerify
0
Dhiraj Mutha
Asked:
Dhiraj Mutha
  • 11
  • 7
1 Solution
 
soostibiCommented:
Could you attach a sample of the KB....txt files?
0
 
Dhiraj MuthaAuthor Commented:
It has some data stating whether the patch is installed or not, and the last 3rd line says 'Hotfix is sucessfully installed' or it will say 'Hotfix not installed'.
0
 
soostibiCommented:
Try this. I assume, that the modules.txt include only the numbers:

111111
222222

Not:

KB111111
KB222222

If it unclude the 'KB', then tell me, I'll change the script. I also assume, that the path can be accessed through the admin shares.
You can export the report to csv by adding to the last line:
    | export-csv c:\yourpath\pathreport.csv -notypeinformation
$modules = Get-Content C:\yourpath\modules.txt
$computers = Get-Content C:\yourpath\computers.txt
$root =  "C$\Installs\Patches"

$computers | %{
    $comp = $_
    $modules | %{
        $instexists = test-path "\\$comp\$root\kb$_.RTInst.txt" 
        $verifyexists = test-path "\\$comp\$root\kb$_.Verify.txt" 
        if($verifyexists){
            $text = Get-Content "\\$comp\$root\kb$_.Verify.txt" -ReadCount 0
            if($text[-3] -eq "Hotfix successfully installed"){$success = "Success"}
            else{$success = "Fail"}
        }
        New-Object -TypeName PSObject -Property @{
            Hostname = $comp
            KBNumber = "KB$_"
            Result = if(!$instexists){"NoRTInst"}elseif(!$verifyexists){"NoVerify"}else{$success}
        }
    }
} | Select-Object hostname, KBNumber, Result

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Dhiraj MuthaAuthor Commented:
yes it includes KB.
0
 
soostibiCommented:
Try this:
$modules = Get-Content C:\yourpath\modules.txt  
$computers = Get-Content C:\yourpath\computers.txt  
$root =  "C$\Installs\Patches"  
  
$computers | %{  
    $comp = $_  
    $modules | %{  
        $instexists = test-path "\\$comp\$root\$_.RTInst.txt"   
        $verifyexists = test-path "\\$comp\$root\$_.Verify.txt"   
        if($verifyexists){  
            $text = Get-Content "\\$comp\$root\$_.Verify.txt" -ReadCount 0  
            if($text[-3] -eq "Hotfix successfully installed"){$success = "Success"}  
            else{$success = "Fail"}  
        }  
        New-Object -TypeName PSObject -Property @{  
            Hostname = $comp  
            KBNumber = $_  
            Result = if(!$instexists){"NoRTInst"}elseif(!$verifyexists){"NoVerify"}else{$success}  
        }  
    }  
} | Select-Object hostname, KBNumber, Result

Open in new window

0
 
Dhiraj MuthaAuthor Commented:
Ok. Thanks. I will update you on monday.
0
 
Dhiraj MuthaAuthor Commented:
Its giving a error:

New-Object : A parameter cannot be found that matches parameter name 'Property'.
At C:\Documents and Settings\a012245-a\Desktop\ModuleAudit-Win7\ModuleAuditWin7.ps1:15 char:48
+         New-Object -TypeName PSObject -Property  <<<< @{
New-Object : A parameter cannot be found that matches parameter name 'Property'.
At C:\Documents and Settings\a012245-a\Desktop\ModuleAudit-Win7\ModuleAuditWin7.ps1:15 char:48
+         New-Object -TypeName PSObject -Property  <<<< @{


For the below attached code.
$modules = Get-Content .\modules.txt  
$computers = Get-Content .\devices.txt  
$root =  "C$\Installs\Logs"  
  
$computers | %{  
    $comp = $_  
    $modules | %{  
        $instexists = test-path "\\$comp\$root\$_.RTInst.txt"   
        $verifyexists = test-path "\\$comp\$root\$_.Verify.txt"   
        if($verifyexists){  
            $text = Get-Content "\\$comp\$root\$_.Verify.txt" -ReadCount 0  
            if($text[-3] -eq "Hotfix successfully installed"){$success = "Success"}  
            else{$success = "Fail"}  
        }  
        New-Object -TypeName PSObject -Property @{  
            Hostname = $comp  
            KBNumber = $_  
            Result = if(!$instexists){"NoRTInst"}elseif(!$verifyexists){"NoVerify"}else{$success}  
        }  
    }  
} | Select-Object hostname, KBNumber, Result |export-csv .\pathreport.csv -notypeinformation

Open in new window

0
 
soostibiCommented:
Strange. New-Object is a built in cmdlet, and has a Property parameter...
0
 
Dhiraj MuthaAuthor Commented:
does it create any issue if powershell is version 1?
0
 
soostibiCommented:
Yes, it can be the problem. Can you upgrade to v2.0?
If not, maybe this will work:
$modules = Get-Content .\modules.txt    
$computers = Get-Content .\devices.txt    
$root =  "C$\Installs\Logs"    
    
$computers | %{    
    $comp = $_    
    $modules | %{    
        $instexists = test-path "\\$comp\$root\$_.RTInst.txt"     
        $verifyexists = test-path "\\$comp\$root\$_.Verify.txt"     
        if($verifyexists){    
            $text = Get-Content "\\$comp\$root\$_.Verify.txt" -ReadCount 0    
            if($text[-3] -eq "Hotfix successfully installed"){$success = "Success"}    
            else{$success = "Fail"}    
        }    
        $mod = $_
        "" | Select-Object -Property @{    
            n="Hostname"; e={$comp}},
            @{n="KBNumber"; e={ = $mod}},
            @{n="Result";e = {if(!$instexists){"NoRTInst"}elseif(!$verifyexists){"NoVerify"}else{$success}}
        }    
    }    
} | Select-Object hostname, KBNumber, Result |export-csv .\pathreport.csv -notypeinformation

Open in new window

0
 
Dhiraj MuthaAuthor Commented:
Ok i will try this tomorrow and update you.
0
 
Dhiraj MuthaAuthor Commented:
Getting this error.

Select-Object : The term '=' is not recognized as a cmdlet, function, operable program, or script file. Verify the term
 and try again.
At C:\Documents and Settings\a012245-a\Desktop\ModuleAudit-Win7\ModuleAuditWin7.ps1:16 char:27
+         "" | Select-Object  <<<< -Property @{
Select-Object : The term '=' is not recognized as a cmdlet, function, operable program, or script file. Verify the term
 and try again.
At C:\Documents and Settings\a012245-a\Desktop\ModuleAudit-Win7\ModuleAuditWin7.ps1:16 char:27
+         "" | Select-Object  <<<< -Property @{
0
 
Dhiraj MuthaAuthor Commented:
Its working with v2.0

But the output is this:

"Hostname","KBNumber","Result"
"Computername",,"NoRTIInst"
"Computername",,"NoRTIInst"

This means its not taking the KB number. I manually checked it and found the hotfix was sucessful.
$modules = Get-Content .\modules.txt    
$computers = Get-Content .\devices.txt    
$root =  "C$\Installs\Logs"    
    
$computers | %{    
    $comp = $_    
    $modules | %{    
        $instexists = test-path "\\$comp\$root\$_.RTIInst.txt"     
	$verifyexists = test-path "\\$comp\$root\$_.Verify.txt"     
        if($verifyexists){    
            $text = Get-Content "\\$comp\$root\$_.Verify.txt" -ReadCount 0    
            if($text[-3] -eq "Hotfix successfully installed."){$success = "Success"}    
            else{$success = "Fail"}    
        }    
        $mod = $_
        "" | Select-Object -Property @{    
            n="Hostname"; e={$comp}},
            @{n="KBNumber"; e={ = $mod}},
            @{n="Result";e = {if(!$instexists){"NoRTIInst"}elseif(!$verifyexists){"NoVerify"}else{$success}}
        }    
    }    
} | Select-Object hostname, KBNumber, Result |export-csv .\pathreport.csv -notypeinformation

Open in new window

0
 
soostibiCommented:
Sorry there is a typo in line 18
$modules = Get-Content .\modules.txt      
$computers = Get-Content .\devices.txt      
$root =  "C$\Installs\Logs"      
      
$computers | %{      
    $comp = $_      
    $modules | %{      
        $instexists = test-path "\\$comp\$root\$_.RTIInst.txt"       
        $verifyexists = test-path "\\$comp\$root\$_.Verify.txt"       
        if($verifyexists){      
            $text = Get-Content "\\$comp\$root\$_.Verify.txt" -ReadCount 0      
            if($text[-3] -eq "Hotfix successfully installed."){$success = "Success"}      
            else{$success = "Fail"}      
        }      
        $mod = $_  
        "" | Select-Object -Property @{      
            n="Hostname"; e={$comp}},  
            @{n="KBNumber"; e={$mod}},  
            @{n="Result";e = {if(!$instexists){"NoRTIInst"}elseif(!$verifyexists){"NoVerify"}else{$success}}  
        }      
    }      
} | Select-Object hostname, KBNumber, Result |export-csv .\pathreport.csv -notypeinformation

Open in new window

0
 
Dhiraj MuthaAuthor Commented:
Great answer. Thanks a lot for the help. It worked nicely.
0
 
Dhiraj MuthaAuthor Commented:
Once smal change in this please, If the host is not reachable or out of network (can be check with a single ping), then it should say NoConnect in the CSV, please do the needful.
0
 
soostibiCommented:
Here you are.
$modules = Get-Content .\modules.txt        
$computers = Get-Content .\devices.txt        
$root =  "C$\Installs\Logs"        
        
$computers | %{        
    $comp = $_ 
    $accessible = Get-Item "\\$comp\c$" -ErrorAction silentlycontinue
    if($accessible){
        $modules | %{        
            $instexists = test-path "\\$comp\$root\$_.RTIInst.txt"         
            $verifyexists = test-path "\\$comp\$root\$_.Verify.txt"         
            if($verifyexists){        
                $text = Get-Content "\\$comp\$root\$_.Verify.txt" -ReadCount 0        
                if($text[-3] -eq "Hotfix successfully installed."){$success = "Success"}        
                else{$success = "Fail"}        
            }        
            $mod = $_    
            "" | Select-Object -Property @{        
                n="Hostname"; e={$comp}},    
                @{n="KBNumber"; e={$mod}},    
                @{n="Result";e = {if(!$instexists){"NoRTIInst"}elseif(!$verifyexists){"NoVerify"}else{$success}}    
            }
    	}
	}
    else {
        "" | Select-Object -Property @{        
            n="Hostname"; e={$comp}},    
            @{n="KBNumber"; e={""}},    
            @{n="Result";e = {"NoConnect"}    
        }       
    }        
} | Select-Object hostname, KBNumber, Result |export-csv .\pathreport.csv -notypeinformation

Open in new window

0
 
Dhiraj MuthaAuthor Commented:
Thanks a lot, it worked super fine. Thank u very much.
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now