Powershell Query

I am trying to run following powershell code for a disk cleanup and while each line in the script is marked as completed, the task itself fails - any ideas on how to get a log of this or maybe fix the script ?

// Enter your action script here

action uses wow64 redirection {not x64 of operating system}
delete __createfile
createfile until END_OF_FILE

Set-ExecutionPolicy Bypass

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path “C:\ProgramData\Delprof2 1.6.0\output.txt” -append

// Clean Up Actions

$CleanUp = @(
“C:\Windows\System32\config\systemprofile\TOSHIBA\eSTUDIOX\UNIDRV*”,“C:\ProgramData\TOSHIBA\eSTUDIOX\UNIDRV\Cache*”)

Remove-Item $CleanUp -Recurse -Force -EV RemoveErrors -EA SilentlyContinue

foreach ($_ in $(Get-ChildItem C:\Users -Recurse -include “.bin",".bmc”)) {{del $_.Fullname -verbose}}

Stop-Service -name “FontCache”

foreach ($_ in $(Get-ChildItem C:\Windows\ServiceProfiles\LocalService\AppData\Local -Recurse -Filter “*.DAT”)) {{del $_.Fullname -verbose}}

Start-Service -name “FontCache”

// Run DelProf

$runpath = "C:\ProgramData\Delprof2 1.6.0\DelProf2.exe"
Start-Process -filepath -wait $runpath -Verb runAs

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate(‘DelProf2.exe’)
Sleep 1.5
$wshell.SendKeys(‘Y’)

Stop-Transcript

END_OF_FILE

Ok, there may be a good bit to unwind here…

Did you make this a Fixlet or a Task? By default, a Task is marked completed if all lines of the script run; a Fixlet is only marked as Fixed if the relevance evaluation toggles to ‘false’, otherwise it is ‘failed’. You can change the default behavior by editing, amd on the Actions tab select the ‘Custom success criteria’ button.

Next, you generally can’t use the powershell script itself to change the execution policy (because you’d already have to be executing the script, which is blocked by execution policy). Instead, you’d have to specify that option on the powershell.exe command line…

Which is missing from your actionscript. It looks like you are building the powershell script on the fly with the createfile until command, but after creating the file you need to copy it to a .ps1 and execute it. After your END_OF_FILE marker, try adding

delete cleanup.ps1
copy __createfile cleanup.ps1
waithidden powershell.exe -ExecutionPolicy Bypass -File cleanup.ps1

The last obstacle you may have, is that the process is running under the System account and can’t interact with the desktop; so I don’t know whether the last bit about launching a gui and using SendKeys to trigger the ‘Y’ response is going to work. I’m not sure what that utility does - is there a command-line parameter you could use instead of clicking a ‘Y’ button?

1 Like

thanks Jason , thanks for explanation on the action being completed & fixed.

my bad as I missed copying on end of script
delete cleanup.ps1
copy __createfile cleanup.ps1
waithidden powershell.exe -ExecutionPolicy Bypass -File cleanup.ps1

regarding selecting “Y” to continue after launching the application ( delprof.exe) - and this is where its getting stuck - I just found I can use /q switch will give that a go

1 Like