Jump to content
farsidetech

Maintenance Script not working

Recommended Posts

Hi,

I created a script to basically inform the customer (they have to click ok to start it) then perform a bunch of maintenance things once per week. I tried it but none of it seems to be working, even my pop-up command doesnt seem to be working when i run a script with just that, ideas?

#inform customer and enable pulseway maintenance mode
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('FarSide Tech Repairs - Automated Monthly Maintenance will begin. Your system may restart. Please save any work and leave computer idle. Press OK to continue.')

Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor" -Name "MaintenanceMode" -Value "1"
exit 0


#1. Create System Restore Point
Checkpoint-Computer -Description PulsewayRestore

#2. Clean up temp files
$folders = @("C:\Windows\Temp\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*", "C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*", "C:\Windows\SoftwareDistribution\Download", "C:\Windows\System32\FNTCACHE.DAT")
foreach ($folder in $folders) {Remove-Item $folder -force -recurse -ErrorAction SilentlyContinue}
exit 0

#3. Recycle bin emptied
Clear-RecycleBin -Force

#4. shkdsk scan
chkdsk /scan | Out-File -FilePath C:\Temp\CHKDSK.txt

#5. Scan windows files and repair if needed
dism /online /cleanup-image /RestoreHealth
sfc /scannow

#6. AV Updates TBD

#disable pulseway maintenance mode and advise customer that computer may restart due to windows updates
Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor" -Name "MaintenanceMode" -Value "0"
exit 0

#7. Check for and install windows updates
Write-Host "Checking for Windows updates"
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session

$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$results = $searcher.search("Type='software' AND IsInstalled = 0 AND IsHidden = 0 AND AutoSelectOnWebSites = 1")

# Install Update
if ($results.Updates.Count -eq 0) {
    Write-Host "No updates found"
    # no updates.
} else {
    # setup update collection
    foreach ($update in $results.Updates){
        $UpdateCollection.Add($update) | out-null
    }

    # download update items
    Write-Host "Downloading updates"
    $Downloader = $Session.CreateUpdateDownloader()
    $Downloader.Updates = $UpdateCollection
    $Downloader.Download()

    # install update items
    Write-Host "Installing updates"
    $Installer = New-Object -ComObject Microsoft.Update.Installer
    $Installer.Updates = $UpdateCollection
    $InstallationResult = $Installer.Install()
    # Check Result
    if ($InstallationResult.ResultCode -eq 2){
        Write-Host "Updates installed successfully"
    } else {
        Write-Host "Some updates could not be installed"
    }
    if ($InstallationResult.RebootRequired){
        Write-Host "System needs to reboot"
        # uncomment the following line to automatically reboot the system if a reboot is required after the updates are installed
        #shutdown -r -f -t 300 -c "Rebooting in 5 minutes to apply Windows updates"
    }
}

#advise customer of completion
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('FarSide Tech Repairs - Maintenance is now complete.')

 

Share this post


Link to post
Share on other sites

Hi @farsidetech,

By default all scripts in Pulseway are executed from the System account in session zero without GUI, therefore if you want to interact with user session, then you will need to use PsExec. For more details regarding this, please check out the following post.

Quote

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...