hot to make cyclic execution into powershell

Ilya Bokov 165 Reputation points
2024-01-09T09:54:37.41+00:00

hi! how a can make cyclic execution into powershell script to add it on domain computers, and start by user logon, and every 3sec. may be someone have another script.

$Path = "C:\ps\screenshots"

# Проверяем, что каталог для хранения скриншотов создан, если нет - создаем его

If (!(test-path $path)) {

    New-Item -ItemType Directory -Force -Path $path

}

Add-Type -AssemblyName System.Windows.Forms

$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds

# Получаем разрешение экрана

$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height)

# Создаем графический объект

$graphic = [System.Drawing.Graphics]::FromImage($image)

$point = New-Object System.Drawing.Point(0, 0)

$graphic.CopyFromScreen($point, $point, $image.Size);

$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position, [System.Windows.Forms.Cursor]::Current.Size)

# Получаем скриншот экрана 

[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds)

$screen_file = "$Path\" + $env:computername + "_" + $env:username + "_" + "$((get-date).tostring('yyyy.MM.dd-HH.mm.ss')).png"

# Сохранить скриншот в файл

$image.Save($screen_file, [System.Drawing.Imaging.ImageFormat]::Png)


Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Exchange | Exchange Server | Development
Exchange | Exchange Server | Management
Exchange | Exchange Server | Management
The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.
Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Rich Matheisen 48,036 Reputation points
    2024-01-09T15:54:02.6233333+00:00

    In order to take a screenshot from a scheduled task the scheduled task would have to run only when the user was logged on and would have to be run by the user. Without a desktop (as would be the case if the scheduled task were run using, say, the System account) there's no screen to capture.

    You might find these useful:

    https://stackoverflow.com/questions/2969321/how-can-i-do-a-screen-capture-in-windows-powershell

    https://stackoverflow.com/questions/57140437/automated-screenshots-using-powershell-and-task-scheduler

    There are plenty of other question and answers to be found with a search like "powershell take a screen shot from scheduled task".


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.