Powershell in RMM

Fred Claus 1 Reputation point
2022-10-29T01:42:55.35+00:00

I'm the IT person for a local church and we have about 11 computers I have to manage every week. I have a Powershell script to clean the system up and run cleanmgr.exe. The script works fine when I'm on the physical machine and I run it. The moment I put it in my Action1 RMM, the RMM rejects it based on a Param error.

Does anyone know what needs to be done so this script will run through an RMM?

param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string[]]$Section
)

$sections = @(
'Active Setup Temp Folders',
'BranchCache',
'Content Indexer Cleaner',
'Device Driver Packages',
'Downloaded Program Files',
'GameNewsFiles',
'GameStatisticsFiles',
'GameUpdateFiles',
'Internet Cache Files',
'Memory Dump Files',
'Offline Pages Files',
'Old ChkDsk Files',
'Previous Installations',
'Recycle Bin',
'Service Pack Cleanup',
'Setup Log Files',
'System error memory dump files',
'System error minidump files',
'Temporary Files',
'Temporary Setup Files',
'Temporary Sync Files',
'Thumbnail Cache',
'Update Cleanup',
'Upgrade Discarded Files',
'User file versions',
'Windows Defender',
'Windows Error Reporting Archive Files',
'Windows Error Reporting Queue Files',
'Windows Error Reporting System Archive Files',
'Windows Error Reporting System Queue Files',
'Windows ESD installation files',
'Windows Upgrade Log Files'
)

if ($PSBoundParameters.ContainsKey('Section')) {
if ($Section -notin $sections) {
throw "The section [$($Section)] is not available. Available options are: [$($Section -join ',')]."
}
} else {
$Section = $sections
}

Write-Verbose -Message 'Clearing CleanMgr.exe automation settings.'

$getItemParams = @{
Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches*'
Name = 'StateFlags0001'
ErrorAction = 'SilentlyContinue'
}
Get-ItemProperty @getItemParams | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue

Write-Verbose -Message 'Adding enabled disk cleanup sections...'
foreach ($keyName in $Section) {
$newItemParams = @{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$keyName"
Name = 'StateFlags0001'
Value = 1
PropertyType = 'DWord'
ErrorAction = 'SilentlyContinue'
}
$null = New-ItemProperty @newItemParams
}

Write-Verbose -Message 'Starting CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait

Write-Verbose -Message 'Waiting for CleanMgr and DismHost processes...'
Get-Process -Name cleanmgr, dismhost -ErrorAction SilentlyContinue | Wait-Process

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-10-29T13:10:56.333+00:00

    If it's an RMM error, then you will need to contact Action1.

    If it's a Powershell error, then I would add a transcript to the script and see if that provide more details about the commands that it is trying to execute.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1

    0 comments No comments

  2. Limitless Technology 44,751 Reputation points
    2022-11-03T09:20:47.037+00:00

    If the error is from PowerShell, you may find that you need to change some of the references to reflect that you're not running the script locally, but from a remote location.

    I hope this answers your question.

    -------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.