Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Search for Text String

Posted on 2011-03-16
5
Medium Priority
?
365 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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

610 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