none
Getting Scheduled Task Users RRS feed

  • Question

  • Hello,

    I have the below script which works with a single thing to search for however when I add in the or statement it just returns everything and I am not sure why? I have tried this as a where clause as well but get the same results. 

    My code is below:

    Get-ScheduledTask | ForEach-Object{
    
    $task = $_
    
    If($task.Principal.UserID -ne "SYSTEM" -or $task.Principal.UserID -ne "LOCAL SERVICE" -or $task.Principal.UserID -ne "NETWORK SERVICE" -or $task.Principal.UserID -ne ""){
    
    Write-Host $task.Principal.UserID
    
    	}
    }

    Once this works I am looking at getting it to do this on a list of computers.

    Which I may need assistance with.

    Thanks

    James


    Monday, September 11, 2017 12:53 PM

Answers

  • I always separate these like

    if(($example -match "test") -and ($example -notmatch "example")){

    }

    not sure though, always done it that way and not had issues :/

    let me know how it works for you.


    if (($task.principal.userid -ne "SYSTEM" ) -and ($task.principal.userid -ne "LOCAL SERVICE") -and ($task.principal.userid -ne "NETWORK SERVICE") -and ($task.principal.userid -ne "")){}




    Monday, September 11, 2017 12:56 PM

All replies

  • I always separate these like

    if(($example -match "test") -and ($example -notmatch "example")){

    }

    not sure though, always done it that way and not had issues :/

    let me know how it works for you.


    if (($task.principal.userid -ne "SYSTEM" ) -and ($task.principal.userid -ne "LOCAL SERVICE") -and ($task.principal.userid -ne "NETWORK SERVICE") -and ($task.principal.userid -ne "")){}




    Monday, September 11, 2017 12:56 PM
  • The problem may be the -or logic. Try -and instead. If the user id is System then it is not equal to Local Service so that would fulfill that part of the IF. You will end up getting everything just as you are. To check for a list of things where you do not want it to be ANY of them you have to say if it is not this AND it is not that.

    Try it and see.

    JRUSSELL97

    Monday, September 11, 2017 2:48 PM
  • Logic!  "OR" will always return true for a negative combination.

     If ($task.Principal.UserID -and $task.Principal.UserID -notmatch 'SYSTEM|LOCAL SERVICE|NETWORK SERVICE') {


    \_(ツ)_/

    Monday, September 11, 2017 3:03 PM
  • Hi Thank you all for your replies I will give them all a go tomorrow morning when I get back in the office. 

    @jrv sorry I am not familiar with  'SYSTEM|LOCAL SERVICE|NETWORK SERVICE' part of the code. 

    I thought the pipe was to place one command into another following through the previous request. I didn't think you could pipe strings through so they are separated? Could you please explain this a little more if possible?

    Thanks

    James

    Wednesday, September 13, 2017 7:15 PM