Hello
I created a script to check if the attached VHDX is disconnected from the system after logging out.
The script works, If I run the script over ISE. The script should generate en email alert if the VHDX wasn’t removed from the system.
As soon as I run the script via TaskScheduler, the script doesn’t work anymore.
The script gets started but not terminated. And also no alert is generated. This means that the powershell script does not run to the end.
[pre]
#Get all Users with a active and inactive rdp session
$queryResults = (qwinsta /server:mah-svr-101 | foreach { (($_.trim() -replace “\s+”,“,”))} | ConvertFrom-Csv)
$DisRDPSessions = $queryResults.SESSIONNAME
$ActivRDPSessions = $queryResults.USERNAME
#Removing admin and system session from the array
$a= Get-ADGroupMember RDS2016
$disRDSUsers = $a.SamAccountName | where {$DisRDPSessions -contains $psitem}
$activRDSUsers = $a.SamAccountName | where {$ActivRDPSessions -contains $psitem}
$connectedRDSUsers = $activRDSUsers + $disRDSUsers
#Get all connected Fslogix Disks
$Volumes = Get-WmiObject Win32_Volume
$VolumeLabel = $Volumes.Label
#Filterling for the Fslogix Disks
$O365DISKS = $VolumeLabel | Where-Object { $_ -like “O365-*” }
$activDISKS = $O365DISKS.trim(“O365-”)
#Check if a VHD is connected although the RDS session has ended
$DismountVHDs = $activDISKS | where {$connectedRDSUsers -notcontains $psitem}
#Sending an Alert
if ($DismountVHDs)
{
$cred = Import-CliXml C:\scripts\ForAlerting_by_Test\cred.clixml
##############################################################################
$From = “test@test.ch”
$To = “test@test.ch”
$Cc = “alerts@test.ch”
$Attachments = “C:\scripts\ForAlerting_by_Anish\MAH Workaround - Example.png”
$Subject = “ALERT: MAH-SVR-101 -> ODFC disk could not be removed.”
$Body = “The following Disks could not be removed from the server:
$DismountVHDs
Workaround: Log in to the RDS server and open Disk Manager: Selects the VHDX mentioned in the alert e-mail and disconnects it from the system. The name of the VHDX always starts with ODFC_ as shown in the example.”
$SMTPServer = “mail.test.ch”
$SMTPPort = “587”
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachments `
-Credential $cred
##############################################################################
}
[/pre]
What could be the problem here?
Thank you very much for your help.
Regards
Ainder
LOGS:
Event 201: Task Scheduler successfully completed task “\Alerting for ODFC” , instance “{6251e833-bc88-4447-a64f-b82ed3aed1e4}” , action “powershell.exe” with return code 2147942659.
Event 102: Task Scheduler successfully finished “{6251e833-bc88-4447-a64f-b82ed3aed1e4}” instance of the “\Alerting for ODFC” task for user “CLOUD\svc.RunTaskScheduler”.
Event 323: Task Scheduler stopped instance “{6251e833-bc88-4447-a64f-b82ed3aed1e4}” of task “\Alerting for ODFC” in order to launch new instance “{4f0d7b3f-8209-4bbd-8fc0-55ebf88ee420}” .
Event 107: Task Scheduler launched “{4f0d7b3f-8209-4bbd-8fc0-55ebf88ee420}” instance of task “\Alerting for ODFC” due to a time trigger condition.
Event 129: Task Scheduler launch task “\Alerting for ODFC” , instance “powershell.exe” with process ID 28480.
Event 100: Task Scheduler started “{4f0d7b3f-8209-4bbd-8fc0-55ebf88ee420}” instance of the “\Alerting for ODFC” task for user “CLOUD\svc.RunTaskScheduler”.
Event 200: Task Scheduler launched action “powershell.exe” in instance “{4f0d7b3f-8209-4bbd-8fc0-55ebf88ee420}” of task “\Alerting for ODFC”.