Solved

Search for Text String

Posted on 2011-03-16
5
358 Views
Last Modified: 2012-05-11
I have the following script
==============================================================
Clear
Function check-remotefile {
    PROCESS {
    If (Test-Connection -ComputerName "$_" -Quiet){
$file = "\\$_\\c$\pos\bin\javaenv_version.bat"

If (Get-Content $file -erroraction silentlycontinue|  Where-Object { $_ -like 'set javaenv_version=2.9' })        
                  {          
                        echo "$_,ONLINE,Ingenico Firmware 2.9 Found"
                  }
              Else
                  {
                        echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"
                  }
                  }
           
    Else{
            $_ + ",OFFLINE ,Firmware Version Not Checked"
        }
        }    
   }
Get-Content  c:\ingver.txt | check-remotefile

==============================================================

Right now if the file javaenv_version.bat  is not  on the pc at all it reports it as "not found"because it can not locate the text . How can get it to tell me the file is not found as well ?
0
Comment
Question by:jfgray
  • 3
  • 2
5 Comments
 

Author Comment

by:jfgray
ID: 35150489
If I remove the -erroraction silentlycontinue

It will give me an error

Get-Content : Cannot find path '\\P**********\\c$\pos\bin\javaenv_version.bat' because it does not exist.
At C:\pshell\Ingenico_Version 2.ps1:7 char:16
+ If (Get-Content <<<<  $file |  Where-Object { $_ -like 'set javaenv_version=2.9' })        
    + CategoryInfo          : ObjectNotFound: (\\**********\\c...env_version.bat:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

I would like it to say "PCNAME,ONLINE, File Not Found"
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35150624
I would use the test-path cmdlet.
Clear
Function check-remotefile {
PROCESS {
    If (Test-Connection -ComputerName "$_" -Quiet){
        $file = "\\$_\\c$\pos\bin\javaenv_version.bat" 
        if (!(Test-Path -Path $file)){
            echo "$_,ONLINE,file not found"
        }
        elseif(Select-String -Path $file -Pattern "set\sjavaenv_version=2\.9"){
            echo "$_,ONLINE,Ingenico Firmware 2.9 Found"
        }
        else {
            echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"
        }
    Else {
        "$_,OFFLINE ,Firmware Version Not Checked"
    }
}    
}
Get-Content  c:\ingver.txt | check-remotefile

Open in new window

0
 

Author Comment

by:jfgray
ID: 35150715
I recieved the following error

The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At C:\Users\jg199\AppData\Local\Temp\ed7aba1e-cfdc-4875-8026-17b709a9e31b.ps1:15 char:9
+     Else <<<< 
    + CategoryInfo          : ObjectNotFound: (Else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Here is the script as I ran it

Clear
Function check-remotefile {
PROCESS {
    If (Test-Connection -ComputerName "$_" -Quiet){
        $file = "\\$_\\c$\pos\bin\javaenv_version.bat"
        if (!(Test-Path -Path $file)){
            echo "$_,ONLINE,file not found"
        }
        elseif(Select-String -Path $file -Pattern "set\sjavaenv_version=2\.9"){
            echo "$_,ONLINE,Ingenico Firmware 2.9 Found"
        }
        else {
            echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"
        }
    Else
      {
        "$_,OFFLINE ,Firmware Version Not Checked"
    }
}    
}
}
Get-Content  c:\ingver.txt | check-remotefile



0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 35150779
Sorry, one bracket is missing. Here is hopefully the correct one.
Clear  
Function check-remotefile {  
PROCESS {  
    If (Test-Connection -ComputerName "$_" -Quiet){  
        $file = "\\$_\\c$\pos\bin\javaenv_version.bat"   
        if (!(Test-Path -Path $file)){  
            echo "$_,ONLINE,file not found"  
        }  
        elseif(Select-String -Path $file -Pattern "set\sjavaenv_version=2\.9"){  
            echo "$_,ONLINE,Ingenico Firmware 2.9 Found"  
        }  
        else {  
            echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"  
        }
	}
    Else {  
        "$_,OFFLINE ,Firmware Version Not Checked"  
    }  
}      
}  
Get-Content  c:\ingver.txt | check-remotefile

Open in new window

0
 

Author Comment

by:jfgray
ID: 35283159
I thought I closed this sorry for the delay.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now