PowerShell script on Centos fails to run via cron

TheRagnarok

New Member
Joined
Jun 22, 2021
Messages
4
Reaction score
0
Credits
40
Hello everyone

I have a PowerShell script that is working absolutely fine when we run manually on centos. (Enter PWSH shell and run the script)

But when I schedule cron for the vCheck script it fails and the script does not run.

Can anyone please suggest what can be done to schedule the vCheck script on Centos?(FYI this PowerShell script calls multiple other Powershell scripts )

Things I have tried so far.
Running the script & scheduling as /usr/bin/pwsh/ /path/to/vCheck.ps1 & this did not work
Setting the user's shell to pwsh and then scheduling the script but did not work.
Tried to mention environment variables in the vCheck script & it failed.

Here are my current environment variables that I have tried so far

[Server~]$ echo $PATH
/opt/microsoft/powershell/7:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
[Server~]$

[Server ~]$ echo $PATH
/opt/microsoft/powershell/7:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin/pwsh:/opt/microsoft/powershell/7/pwsh:/opt/microsoft/powershell/7/pwsh.dll:/opt/microsoft/powershell/7/pwsh.pdb:/opt/microsoft/powershell/7/pwsh.xml
[Server ~]$

Please suggest.

I'm using Alans vCheck script version 6.25 on centos 7.8 Powershell version 7.1.0

Thanks in advance
 


I have no experience with PowerShell on Linux, have you temporarily tried disabling(setenforce 0) selinux to see if that could be the cause? What errors do you get back from cron through mail when the PowerShell script fails in cron?
 
SELinux is disabled.

[Server~]$ getenforce
Disabled
[Server~]$

No errors the script simply exits and I guess it fails to execute other powershell scripts.

Tried strace getting no clue.
 
Also, the script fails to run as a normal command
/usr/bin/pwsh /path/to/script

I have other PowerShell scripts that run absolutely fine in the above manner.

It is Just this script that fails because this PowerShell script calls multiple other Powershell scripts.
I think this might be the reason.
 
I would go through your script and then then comment out all the parts after the first script it includes/executes and then run it. If it fails there the problems is probably with that included script and if not uncomment the next part and so on, that's all the advice I can give on PowerShell since I have no experience with it.
 
I find that even with the working directory in the cron entry pwsh tends to run elsewhere (I have not done this for a while, so I forget what location I have seen). Maybe add a ``Get-Location | Select-Object -ExpandProperty Path | Out-File ~/cronpath.txt`` or ``$PSScriptRoot | Out-File ~/cronpath.txt`` to see where the script is really running. If cron is the only thing running the script, then try to ``Push-Location`` to the full path at the top of the script. That should better allow child scripts to import successfully as well.
 

Members online


Top