I believe the computer scans USB, ethernet, hdmi, etc., several times per second. I suppose it checks one or more interrupts. A new peripheral needs to know how to trigger an interrupt. Either that or it must know how to send a signal 'I'm here', immediately when the computer queries it.
Regarding the PCIe, it's a question whether the computer also looks at it periodically. If it does not, then your script idea might work. (Then it's a question whether the scripting language specifically accesses the PCIe.)
If the computer automatically looks at the PCIe periodically, then your job may be easier, because that implies there are routines in the OS which allow you to turn it on/off, etc.