Powershell script needed

Ibrahim AlHusari 151 Reputation points
2024-04-24T16:35:23.0566667+00:00

closed.....

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,082 questions
{count} votes

Accepted answer
  1. Marcin Policht 11,385 Reputation points MVP
    2024-04-24T17:20:51.0333333+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful