PowerShell and Task Scheduler - Only one part of Script is not Executing

S

Sritam35

I am having a very specific issue with scheduling a PowerShell script to run with Task Scheduler in Windows 2012 server. The script works as expected from the PowerShell ISE console. The script is checking the size of C:\ and a particular log folder size and if the log folder size is more than 20 GB then it clears the logs of the folder with the help of application.


The stop application stops 3 of the particular services and while starting the start application undergoes a interactive session with CMD and if we press y and enter , it will clear the logs and start those 3 services.


Then it will send the mail the output.

The issue is, if i start the script from powershell console it complete successfully as expected but if i do the same from task scheduler then only the start application is not executing and the task is getting completed with stop application and sending the mail. The middle part is not happening.


$cdrive = $NULL
$percentage = $NULL
$freespace = $NULL
$size = $NULL

$cdrive = gwmi win32_logicaldisk -Filter "DeviceId='C:'"
$size = [math]::Round($cdrive.Size/1GB,0)
$freespace = [math]::Round($cdrive.FreeSpace/1GB,0)
$percentage = [math]::Round($cdrive.FreeSpace/$cdrive.Size * 100,0)

if($percentage -gt 50)
{

exit

}
else

{
$logsize = $NULL

$logsize = [math]::Round((Get-ChildItem C:\HPE\HPSS\pa\tomcat\logs -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB,0)

if($logsize -lt 10)

{
$cdrive1 = $NULL
$percentage1 = $NULL
$size1 = $NULL
$freespace1 = $NULL
$ID1 = $NULL
Add-Type -AssemblyName 'System.Windows.Forms'
$ID1 = (Start-Process 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Hewlett Packard Enterprise\HPE XP7 Performance Advisor Management Station\Stop Services' -PassThru).Id
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::AppActivate([Int32]$ID1)
Sleep 90
[System.Windows.Forms.SendKeys]::SendWait("~")

sleep 20
this below start function only not executing.

#*****************************************************************************
$ID2 = $NULL
#Add-Type -AssemblyName 'System.Windows.Forms'
$ID2 = (Start-Process 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Hewlett Packard Enterprise\HPE XP7 Performance Advisor Management Station\Start Services' -PassThru).Id
sleep 10
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::AppActivate([Int32]$ID2)
sleep 10
[System.Windows.Forms.SendKeys]::SendWait("y~")
sleep 30
[System.Windows.Forms.SendKeys]::SendWait("~")
#*********************************************************************************

$cdrive1 = gwmi win32_logicaldisk -Filter "DeviceId='C:'"
$size1 = [math]::Round($cdrive1.Size/1GB,0)
$freespace1 = [math]::Round($cdrive1.FreeSpace/1GB,0)

$percentage1 = [math]::Round($cdrive1.FreeSpace/$cdrive1.Size * 100,0)

$SMTPSRV = "xyz.abc.net"
$EmailFrom = "abc@.com"
$EmailTo ="abc@.com"
$MyReport = @"

Script has cleared the HP performance unused logs from the path C:\HPE\HPSS\pa\tomcat\logs and below is the output of C: drive.

Before log deletion
**************
C: Total size in GB = $size
C: Freespace in GB = $freespace
C: Freespace in % = $percentage


After Log Deletion
**************
C: Total size in GB = $size1
C: Freespace in GB = $freespace1
C: Freespace in % = $percentage1
"@

Send-MailMessage -To $EmailTo -Subject "HP Performance advisor log status" -From $EmailFrom -Body $MyReport -SmtpServer $SMTPSRV


}
else

{
$cdrive2 = $NULL
$percentage2 = $NULL
$cdrive2 = gwmi win32_logicaldisk -Filter "DeviceId='C:'"

$percentage2 = [math]::Round($cdrive2.FreeSpace/$cdrive2.Size * 100,0)
$logsize1 = $NULL

$logsize1 = [math]::Round((Get-ChildItem C:\HPE\HPSS\pa\tomcat\logs -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB,0)
$SMTPSRV = "xyz.abc.net"
$EmailFrom = "xyz.abc.net"
$EmailTo ="xyz.abc.net"
$MyReport = @"

C: drive freespace is < 50% but the HP Performance log folder "C:\HPE\HPSS\pa\tomcat\logs" is not occupied more than 10GB.

Current size of the log folder is $logsize1 MB.

"@

Send-MailMessage -To $EmailTo -Subject "HP Performance advisor log status" -From $EmailFrom -Body $MyReport -SmtpServer $SMTPSRV
}

}



this is the exception i am getting while executing manually but script works as usual.
1292152.png1292156.png

this below service only is not executing at all which is the issue during task scheduler.

1292157.png1292158.png

Task Scheduler information tried:

Progrm/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments : -ExecutionPolicy RemoteSigned/ByPass/Unrestricted -File D:\ClearDBlog.ps1

Continue reading...
 
Back
Top Bottom