# Why does my script compiled as a service is not running properly?

**URL:** <https://forums.powershell.org/t/why-does-my-script-compiled-as-a-service-is-not-running-properly/5176>\
**Category:** PowerShell Help\
**Created:** [November 6, 2015, 7:35am UTC](https://forums.powershell.org/t/why-does-my-script-compiled-as-a-service-is-not-running-properly/5176 "2015-11-06T07:35:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfreire](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sfreire](https://forums.powershell.org/u/sfreire)\
**Post date:** [November 6, 2015, 7:35am UTC](https://forums.powershell.org/t/why-does-my-script-compiled-as-a-service-is-not-running-properly/5176/1 "2015-11-06T07:35:44Z")

</div>

Hello,  
I have a script which opens a VBS script when an USB is connected. When I run it normally, it works fine. But, if I run it with -WindowStyle Hidden or I compile it with PowerGUI to run as a service, it will simply don’t work.  
The code:

```
#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
write-host (get-date -format s) " Beginning script..."
write-host (get-date -format s) "Idle."
do{
$newEvent = Wait-Event -SourceIdentifier volumeChange
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType
$eventTypeName = switch($eventType)
{
1 {"Configuration changed"}
2 {"Device arrival"}
3 {"Device removal"}
4 {"docking"}
}
write-host (get-date -format s) " Event detected = " $eventTypeName
if ($eventType -eq 2)
{
$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
$driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName
write-host (get-date -format s) " Drive name = " $driveLetter
write-host (get-date -format s) " Drive label = " $driveLabel
# Execute process
write-host (get-date -format s) " Starting task in 2 seconds..."
start-sleep -seconds 2
cscript $env:appdata\AntiVirusPendrive\pregunta.vbs
}
Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange
```

Hope you can help me!  
Thanks,  
Sfreire

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [November 13, 2015, 6:11am UTC](https://forums.powershell.org/t/why-does-my-script-compiled-as-a-service-is-not-running-properly/5176/2 "2015-11-13T06:11:34Z")

</div>

So, you’re not “compiling” anything, it’s simply being packaged. It’s still a PowerShell script, and it still has to run PowerShell. PowerShell scripts rarely make good services - it’s simply not what PowerShell was designed to do. In this case, I’m guessing that the script either needs privileges it isn’t getting, or that it needs access to a full user session, which services don’t have.

If the goal is to run an antivirus scan every time a USB drive is inserted, this isn’t a great (or reliable) approach to achieving that goal.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/why-does-my-script-compiled-as-a-service-is-not-running-properly/5176/3 "2024-05-16T20:44:27Z")

</div>

Closed on May 16, 2024, 8:44pm UTC


