How to tell if an optical drive has ejected - powershell

I am trying to eject an optical disc on a Windows PC connected to a robotic disc changer. I have the following powershell script which works most of the time:
$path=$args[0]
$sh = New-Object -Comobject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.Type -eq "Removable Disc" } |
foreach {
if($_.Path -eq $path) { $_.InvokeVerb("Eject") }
}
However, sometimes it doesn't work, presumably because Windows thinks the drive is still in use.
Is there any way I can get a response code from this to tell if it has successfully ejected the disc? Or otherwise tell if the disc is still mounted?
Alternatively, does anyone know a nice C++ way of doing it, which would be even better as I could avoid the need to call a separate script...

Related

How do I use a PowerShell script to eject all mass storage drives?

I've been using a USB hub to copy files to many flash drives. Afterwards, they need to be ejected. I don't want to eject them manually, one at a time.
How can I write a PowerShell script to eject all mass storage drives, without having to input the drive letters?
I searched all over and was not able to find a satisfactory answer that put it all together.
Here is the script that I came out with.
$volList = get-wmiobject -Class Win32_Volume | where{$_.drivetype -eq '2'}
if ($volList){
ForEach ($vol in $volList){
$volLetter = $vol.DriveLetter
echo "Ejecting drive $volLetter"
$Eject = New-Object -comObject Shell.Application
$Eject.NameSpace(17).ParseName($volLetter).InvokeVerb("Eject")
}
}
else {
echo "No removable drive found"
}
This worked very well!

Remotely Enable Wakeup from Shutdown

I am trying to get 400 pcs setup to wake on lan.
everything is enabled on all the pcs but one check box.
Under the advanced tab on the nic, "wakeup from Shutdown" is disabled
and I need to enable it on all pcs.
I have a working script to change the boxes under the power management tab but those are already checked. but I cant seem to find any info about anything under the advanced tab. properties on nic, configure, advanced)
Any help in figuring out if this is possible would be a big help.
Could I edit that code to just enable what I need? or simpler to write a new one?
I haven't tried anything cause I cant seem to find any info as to if this is even possible.
im thinking its just a different wmi object (probably wrong name) but in my code that works I found the lines that go to each check box and am wondering if there is a class for "wakeup from shutdown"
*** line im curious about
foreach ($NIC in $NICs) {
$Errors = $false
Write-Host "NIC:"$NIC.Name
#Allow the computer to turn off this device
Write-Host "Allow the computer to turn off this device....." -NoNewline
***$NICPowerManage = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | Where-Object { $_.instancename -match [regex]::escape ($nic.PNPDeviceID) }
If ($NICPowerManage.Enable -ne $TurnOffDevice) {
$NICPowerManage.Enable = $TurnOffDevice
$HideOutput = $NICPowerManage.psbase.Put()
}
If ($NICPowerManage.Enable -eq $TurnOffDevice) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed" -ForegroundColor Red
$Errors = $true
OTHER LINES
Get-WmiObject MSNdis_DeviceWakeOnMagicPacketOnly -Namespace root\wmi | Where-Object { $_.instancename -match [regex]::escape($nic.PNPDeviceID) }
I use this little tool called WMIExplorer.exe to navigate WMI. I think MSPower_DeviceWakeEnable is the class you are looking for.
After a few searches, I stumbled upon this. Perhaps that is similar to your problem.
https://www.itinsights.org/Enable-wake-on-lan-WOL-with-PowerShell/

PowerShell Script to pop up at logoff

I have a basic understanding of PowerShell.
I would like to get my hands on a PowerShell script that will run at logoff.
This script needs to warn a user that their USB storage device is still plugged in before they sign out and they have to acknowledge the fact that it is and then select ok and then proceed to sign out
I know that the script needs to placed in an accessible location with the correct permissions and that a GPO can be used to enforce this. The script part is what I need help with..
If anyone out there in the interwebs please help?
Environment OS: Windows 10
AD not in use. Novell system used.
After you checked out what Franco stated, you can try something like the following. But still need to figure out how to make it work properly:
$usbs = GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType –eq ‘USB’ }
$devices = #()
foreach($usb in $usbs){
$devices += $usb.Model + ". "
}
$input = [System.Windows.MessageBox]::Show("There are USB devices connected, $($devices | Out-String) Would you like to proceed logging off?","Warning","YesNoCancel","Error")
if($input -eq "Yes"){
shutdown -L
}elseif($input -eq "No"){
shutdown -A
}else{
break
}
You will need to find a way to make the user input visible before the logoff screen.

Trying to kill a process by NAME and CPU Time

What I'm trying is to close a windows of explorer.exe without killing the whole process, because that will restart the whole taskbar. So far I can only identify it by CPU Time which separated from the windows explorer.exe because it start after.
Is there a PowerShell script or command out there that could help me achieve this?
Take a closer look at the Shell.Application COM Object.
The method 'Windows()' returns an object foreach open explorer-window.
You can call the the method 'Quit()' of these Objects to close the windows, here is a example:
$explorerPathYouWantToKill = "C:\"
$shellApp = New-Object -COM 'Shell.Application'
($shellApp.Windows() | Where-Object {$_.LocationURL -eq "file:///$($explorerPathYouWantToKill.Replace('\','/'))"}) | Foreach {
$_.Quit()
}

COMAdmin - force refresh of applications after install of proxy

I'm programmatically installing a COM+ proxy component via Powershell, using msiexec on the msi, and then using the COMAdmin.COMAdminCatalog object to set the remote server on the proxy.
The problem is that it takes a while for the newly installed proxy to be available in the "Applications" collection of COMAdminCatalog. Is there some way to force a refresh of the catalog before getting the application list?
Essentially, what I do is this:
msiexec /q /i $appName.msi
use the COMAdmin.COMAdminCatalog to enumerate the apps.
function Set-Remote-Server-For-Complus-Application($appName, $remoteServer) {
$comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog
$apps = $comAdmin.GetCollection("Applications")
$apps.Populate();
$app = $apps | Where-Object {$_.Name -eq $appName}
if ($app -eq $null) {
Write-Warning "Unable to find COM+ app ""$appName""."
Return
}
$app.Value("ApplicationProxyServerName") = $remoteServer
$result = $apps.SaveChanges()
$apps = $null
if ($result -eq 1) {
Write-Output "Successfully set complus remote server ""$remoteServer"" on ""$appName"""
}
}
The problem is that the application is not Found. If I add a Start-Sleep -Seconds 2 between the calls, it works. But, sleeping is not good, because sometimes it might take longer than 2 seconds, sometimes it might only take 200 milliseconds, so the wait is unnecessarily long.
Is there any way to make sure that the COMAdmin.COMAdminCatalog is actually updated before I try to enuerate the applications, without resorting to sleeping and just hoping for the best?