Hi I need to get a script that will do the following:
- Check if a service exists
- If the service doesn't exist run my script
- If the service exists do nothing
Here is what I have but it's not working for me:
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
if($service.Status -eq $NULL)
{
$CLID = $inclid
New-Item -Path "c:\" -Name "folder" -ItemType "directory"
Invoke-WebRequest -Uri https://something.com\setup.exe -OutFile c:\folder\swibm#$CLID#101518#.exe
$installer = "swibm#$CLID#101518#.exe"
Start-Process -FilePath $installer -WorkingDirectory "C:\folder"
}
else
{
Write-Host "Client Already Installed"
}
If I run $service.Status
alone I get an "OK" returned. Under this condition I would need the script to stop and run the else section. I only want this script to run if $service.Status
returns nothing. Where am I going wrong here?