PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,082 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
closed.....
Try the following:
# Define variables
$RegistryKeyPath = "HKLM:\SOFTWARE\Test"
$RegistryValueName = "ProcessCount"
$TextFilePath = "C:\Temp\Test.txt"
# Function to check if registry key exists
Function Test-RegistryKey {
param (
[string]$Path
)
return (Test-Path $Path)
}
# Function to check if text file exists
Function Test-TextFile {
param (
[string]$Path
)
return (Test-Path $Path)
}
# Function to get process count
Function Get-ProcessCount {
return (Get-Process).Count
}
# Function to update registry value
Function Update-RegistryValue {
param (
[string]$Path,
[string]$Name,
[string]$Value
)
Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force
}
# Function to write to text file
Function Write-TextFile {
param (
[string]$Path,
[string]$Content
)
Add-Content -Path $Path -Value $Content
}
# Main script
if (!(Test-RegistryKey $RegistryKeyPath)) {
New-Item -Path $RegistryKeyPath -Force | Out-Null
}
if (!(Test-TextFile $TextFilePath)) {
New-Item -Path $TextFilePath -ItemType File -Force | Out-Null
}
# Loop for one minute
$EndTime = (Get-Date).AddMinutes(1)
while ((Get-Date) -lt $EndTime) {
# Get current process count
$ProcessCount = Get-ProcessCount
# Update registry value
Update-RegistryValue -Path $RegistryKeyPath -Name $RegistryValueName -Value $ProcessCount
# Write to text file
Write-TextFile -Path $TextFilePath -Content "$((Get-Date).ToString()): $ProcessCount"
# Wait for one second
Start-Sleep -Seconds 1
}
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin