# Function to generate a random 5-letter string function Get-RandomString { param( [int]$length ) $characters = 'abcdefghijklmnopqrstuvwxyz' $random = New-Object Random $randomString = -join (1..$length | ForEach-Object { $characters[$random.Next(0, $characters.Length)] }) return $randomString } # Function to sanitize agent name function Sanitize-AgentName { param( [string]$name ) # Allow only A-Z, a-z characters $sanitizedName = $name -replace '[^A-Za-z]', '' return $sanitizedName } # Get and sanitize the computer name $agentName = Sanitize-AgentName -name $env:COMPUTERNAME # Generate a random 5-letter string and append it to the agent name $randomString = Get-RandomString -length 5 $agentNameWithRandom = $agentName + $randomString # Download and install Wazuh agent Invoke-WebRequest -Uri https://packages.wazuh.com/4.x/windows/wazuh-agent-4.7.0-1.msi -OutFile "${env:temp}\wazuh-agent" msiexec.exe /i "${env:temp}\wazuh-agent" /q WAZUH_MANAGER='41.111.227.139' WAZUH_AGENT_GROUP='default' WAZUH_AGENT_NAME="$agentNameWithRandom" WAZUH_REGISTRATION_SERVER='41.111.227.139' # Add a delay to allow installation to complete Start-Sleep -Seconds 10 # Get the service name $serviceName = Get-Service | Where-Object {$_.DisplayName -like '*Wazuh*'} | Select-Object -ExpandProperty ServiceName # Set service startup type and start service if ($serviceName -ne $null) { Set-Service -Name $serviceName -StartupType Automatic Start-Service -Name $serviceName } else { Write-Host "Wazuh service not found." }