I am trying to run a powershell script from a batch file via SCCM client. Basically the batch file will copy a PS1 file to the c:\temp directory of the targeted workstation and call the PS file. However we can get the files copied to the c:\temp directory but the PS file never seems to be ran. I am including the scripts below. Does anyone see an reason why this would not run.
This is the batch file to copy the files as needed.
@echo off
xcopy "clearcache.ps1" "c:\Temp\" /i
powershell.exe set-executionpolicy unrestricted
powershell.exe -NoLogo -NonInteractive -NoProfile -command C:\Temp\clearcache.ps1
powershell.exe set-executionpolicy restricted
REM Return exit code to SCCM
exit
This is the powershell script.
$limit = (Get-Date).AddDays(-2) $path = "c:\windows\ccmcache" # Delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force -ErrorAction SilentlyContinue | out-file c:\temp\clearcache.txt # Delete any empty directories left behind after deleting the old files. Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue