Jump to content

Recommended Posts

Posted

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.')

 

  • Staff
Posted

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

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...