Powershell Script

Handian Sudianto 6,071 Reputation points
2025-06-04T07:48:23.2833333+00:00

I have powershell script to get defender status for all servers using below command.

Below command was successfull if I run the script manually but if we run this script using schedule task the script is running but no any result. Anyone know why?

$remoteComputer = 'server01'
$session = New-PSSession -ComputerName $remoteComputer
Invoke-Command -Session $session -ScriptBlock {
$status = Get-MpComputerStatus
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,970 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,181 Reputation points
    2025-06-04T10:12:51.4766667+00:00

    When you run it manually, it runs as your account which has administrator access to server01. The most likely problem is that the account that you configured to run the task as does not have admin access. Also "System" will not work.

    Add a transcript to the script so that you can see what it does.

    Start-Transcript C:\YourLogFolder\MyTask.log
    $remoteComputer = 'server01'
    $session = New-PSSession -ComputerName $remoteComputer
    Invoke-Command -Session $session -ScriptBlock {
    $status = Get-MpComputerStatus
    ...
    Stop-Transcript
    
    

Your answer

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