?
Solved

Search for Text String

Posted on 2011-03-16
5
Medium Priority
?
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

752 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