Create Scheduled Task download ClamAV daily using Powershell - powershell

So I'm looking to download the ClamAV cvd definition files on a server so that I can keep my clients updated regularly.
In order to keep it separate from any login/admin account, I'd like to use a service account.
This is the powershell script that I have so far;
Start Microsoft-Edge:http://database.clamav.net/main.cvd
sleep 120
Get-Process -Name "*Edge*" | Stop-Process
Start Microsoft-Edge:http://database.clamav.net/daily.cvd
sleep 120
Get-Process -Name "*Edge*" | Stop-Process
Get-Item -Path "C:\Temp\*.cvd" | Move-Item -Destination "C:\Updates\ClamAV\*.cvd"

This powershell script works for me, and should run fine as a scheduled task under a different service account:
# Download both files using edge to bypass cloudflare
Start-Process msedge http://database.clamav.net/main.cvd
Start-Process msedge http://database.clamav.net/daily.cvd
sleep -Seconds 30
# close edge
Get-Process msedge | Stop-Process
# move files from default edge location to updates folder
Get-Item "$env:USERPROFILE\Downloads\*.cvd" | Move-Item -Destination "C:\Updates\ClamAV\*.cvd"
Alternatively, you can skip the move step by specifying a different download folder in edge. Run edge as your service account user > Settings > Downloads > Location > set the folder path you want

Related

How do I create a loop based on file size in power shell

I am working with intune and PowerShell and I basically want to run a exe file which downloads 15.2GB / 7932 files for the insulation off the autodesk website and then creates a text file so that intune knows that it's done as I want to delete all the install files with another script later.
The problem is the PowerShell script will run and close before it has finished downloading and intune thinks it is done and the next script tries to install what was downloaded but it is not fully downloaded so it fails.
I have tried to put a wait command but intune will just hang and you will have to restart windows which is something I don't want the users to do.
I am thinking to add a loop so ot checks the file size of the following folder:
C:\Autodesk\{E658F785-6D4D-4B7F-9BEB-C33C8E0027FA}
and once it reaches 15.2GB / 7932 files it goes to the next step and creates the text file.
Below is my current PowerShell script:
Start-Process -NoNewWindow -FilePath "\\arch-syd-fs\EM Setup\Autodesk Recap Custom Install 2023\Source 1 Download\Revit_2023.exe" -ArgumentList "--quiet' " -Wait
New-Item "C:\Temp\Revit 2023" -Type Directory
New-Item -Path "C:\Temp\Revit 2023\Download Done.txt"
Lets break this down into 3 questions
How do you check the size of a directory?
How do you check the count of files in a directory?
How do you make a script wait until these checks reach a certain value?
It turns out you can do the first 2 together
$dirStats = Get-ChildItem -Recurse -Force 'C:\path\to\whatever' | Measure-Object -Sum Length
$size = $dirStats.Sum
$fileCount = $dirStats.Count
Then you can wrap it in a do-until loop (with a delay to keep it from eating all the CPU) to make the script wait until those values reach a certan threshold
do {
Start-Sleep 5
$dirStats = Get-ChildItem -Recurse -Force 'C:\path\to\whatever' | Measure-Object -Sum Length
$size = $dirStats.Sum
$fileCount = $dirStats.Count
} until( ($size -ge 15.2*1024*1024*1024) -and ($fileCount -ge 7932) )
Note that $size is in bytes, and you might want to make that an -or condition rather than an -and depending on weather you want the script to return after either condition is met or wait for both.

Powershell Form watching wsusutil.exe until finished

This is my first form I'm making to accomplish a task, but also to learn how to do it. I'm making a simple front end for exporting and then importing WSUS data from a connected server to a disconnected server. I'm working on the export part now and I need to figure out how to start the export process, then once it is done then make the iso file.
Here is the button code I have so far, but not sure how to watch the WsusUtil.exe until it's done then proceed to the next task. I thought I could watch the process and when it's over move to the next step. But have not been able to make that work. I tried a do until, but it kept running the start-process over and over. Also when it starts it make a large black box with some message on it. I tried to use the -NoNewWindow but it did launched. WsusUtil.exe running
$btnStartExport.Add_Click({
$nicedate = get-date -UFormat %m-%d-%y #put in MM-DD-YY format
#progress bar for overall progress of steps 1 and 2 and individual progress bar for each steps
$WSUSUtilPath = "c:\Program Files\Update Services\Tools\"
$WSUSMetaDataPath = "c:\tools\wsusexport\"
$isotitle = "WSUS Offline Server update $nicedate"
$ProcessName = "WsusUtil" #process to watch until it's done
$isofilename = "WSUSSvrOffline-$nicedate.iso" #creates WSUS Offline Server update-11-14-2021.iso
#Step 1 Check if directory exists
Check-Path $WSUSMetaDataPath
#Step 1 - export the WSUS Metadata
Start-process -FilePath $WSUSUtilPath\WsusUtil.exe -ArgumentList #("export","$WSUSMetaDataPath\$nicedate-export.xml.gz","$WSUSMetaDataPath\$nicedate-export.log") -NoNewWindow -Wait
$wsusProcess = get-process WsusUtil -ErrorAction SilentyContinue
# Step 2 - create ISO
get-childitem "$WSUSMetaDataPath","$txtWSUSContentPath.text" | New-IsoFile -path $txtISOLocation.text+$isofilename -Force -Title $isotitle
#clean up medatadata directory
get-childitem -path $WSUSMetaDataPath -Include *.* -File -Recurse | foreach {$_.Delete()}
})
$frmMain.controls.add($btnStartExport)

Run all .exe in a folder

I'm playing with malware in a VM and every script I try gets stuck. Basically I need to run every .exe in a folder. Tried batch files using start, powershell, etc. The issue happens when AV moves some file to quarentine, or some process keep running then the script doesn't jump to the next one.
CMD start works but shows popups when doesn't find some file, then you have to keep clicking to jump to the next file.
These works but get stuck after a while:
Get-ChildItem 'C:\Users\LAB\Desktop\test' | ForEach-Object {
>> & $_.FullName
>> }
Same here:
for %%v in ("C:\Users\LAB\Desktop\test\*.exe") do start "" "%%~v"
and here:
for %%i in (C:\Users\LAB\Desktop\test\*.exe) do %%i
You need to provide some form of code to allow us to help you troubleshoot it; this is not a request a script page.
Anyways, you would be looking at something like this:
#Assuming the .exe's are located in C Root.
Get-ChildItem -Path C:\ | Where-Object {$_.Extension -like ".exe"}| Foreach {Start-Process $_.FullName}
#In Ps, we like to filter as far left as possible for faster results.
Get-ChildItem -Path C:\ -File "*.exe" | Foreach {Start-Process $_.FullName}
#Running the commands as jobs so it doesnt wait on any to finish before running the next.
Start-Job { Get-ChildItem -Path C:\ -File "*.exe" | Foreach {Start-Process $_.FullName} }
Start-Sleep 2
Get-Job | Remove-Job
Please refer to the following link: How to ask a question

PS1 script stops execution in the middle when run silently

I'm trying to set up a PS1 script to restart the Windows service on the remote machine. Script is supposed to be automatically run by PRTG platform (monitoring solution). Platform has built-in feature which allows you start script for you. Problem is that when PRTG runs the script it stops in a half way without error after the ‘Stop-Service’ cmdlet. When I remove it from the script then full script is run. I've tried different variations of the script but it's always like it stops when it finished execution of ‘Stop-Service’.
The script supposed to be run in silent mode by the PRTG. Do you have any idea what can be the cause of it or how to check it?
$service = Get-Service -ComputerName 123.123.123.123 -Name Tomcat
Stop-Service -InputObject $service -Force
Move-Item -Path "\\123.123.123.123\C$\Program Files (x86)\tomcat\logs\tomcat.log" -Destination "\\123.123.123.123\C$\Program Files (x86)\tomcat\logs\log_archieve"
Get-ChildItem "\\123.123.123.123\C$\Program Files (x86)\tomcat\logs\log_archieve\tomcat.log" | ForEach-Object {
Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')-$(Get-Date -Format "ddMMyyyy")_oldlog.log"
}
Start-Service -InputObject $service -Verbose

Powershell close multiple windows / end processes based on name

I have a pretty neat mess of batch/python scripts that install a program called MATRIS, followed by about 15 exe updates.
Around 11 of these updates open a window telling me the that the update was successful.
Now it would be really fun to run a batch or powershell script which closes all of these windows for me.
The last thing I tried was Get-Process | Where-Object {$_.Path -like "MatrisInstaller.APCIPLUS"} | Stop-Process -WhatIf
I wasn't sure if it was the name as read in task manager, or like the title of the window, but I tried both.
Please note that a couple of these are (32 bit) - I'm not sure if that would impact the script.
I was able to run tasklist followed by kill {PID} but PIDs change: I'm not sure how to script it.
Please reply if you need any clarification, I've historically been poor at wording my questions.
In your example, Path is pointing to the executable file on disk, so while possible to use (if it is consistent), it won't match the name you find in the processes tab of Task Manager. Typically, people will use the name as shown on the Details tab of Task manager. For example, with Outlook on my system, these three possibilities are:
Path: C:\Program Files\Microsoft Office\Office16\OUTLOOK.EXE
Processes tab: Microsoft Outlook
Details tab: outlook.exe
So, you need a command like this:
Get-Process | Where Name -eq 'Outlook' | Stop-Process
or, better:
Get-Process -Name 'Outlook' | Stop-Process
Note that PowerShell expects you to remove the '.exe' you see in Task manager.
EDIT: Additional technique
If you know the names of the processes, then you can simplify your script by doing something like this:
$processList = "Process1","Process2","Process3" # Add all process names to the list
$processList |
ForEach-Object {
Get-Process -Name $_ | Stop-Process
}
You were almost there, just need to change "Path" to "ProcessName" like so:
Get-Process | Where-Object {$_.ProcessName -like "MatrisInstaller.APCIPLUS"} | Stop-Process -WhatIf