Solved

Search for Text String

Posted on 2011-03-16
5
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I thought I closed this sorry for the delay.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

6 Experts available now in Live!

Get 1:1 Help Now