Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

verify the presence of a service and if no, exit the script

Hello,

How can I modifiy this script for verify the presence of a service and if no, exit the script
#Definition des variables
$fullPathIncFileName=$MyInvocation.MyCommand.path
$dir_name=Split-Path $MyInvocation.MyCommand.path
$array_str=$fullPathIncFileName.split(".")
[int] $nb_str=@($array_str).count
$_SNAPID=@($array_str)[$nb_str-2]
$SPOOL_FILE=$dir_name+"\t_schemas.log"
$SPOOL_FILE1=$dir_name+"\t_schemas.$_SNAPID"
$_HOST   = $env:computername
$ORACLE_HOME = gci HKLM:Software\Oracle\KEY*,HKLM:Software\Oracle\HOME* |select @{n="Oracle_Home"; e={$_.GetValue("Oracle_Home")}}
$_DBLIST = get-service -Name "OracleService*" | Where-Object {$_.status -eq "Running"} | ForEach {$_.name.Replace("OracleService","")}
$st=0
if($_DBLIST.count -eq 1) {
  echo "" 
exit 0
}
Thanks
bibi
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi

You could include the following test to see if the service exists

$services = get-service
 if ( (($services  | where-object {$_.servicename -like "OracleService"}) | measure).count -ne $null) { "service found"}

I hope this helps
Joe
ASKER CERTIFIED SOLUTION
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial