none
Powershell scheduling question RRS feed

  • Question

  • As part of the network engineering group I have to remote to several servers on our network, on a daily bases.  I use Remote Desktop Connection Manager to group servers by application and then list each server that application uses, including SQL and web servers.  Our company started implementing a policy that locks out our network ID if we stay logged on overnight, so I put this script together to go through all of the servers I have listed and then log my ID off if it is found on that server.  It works great if I manually run it, but when I run it as a scheduled task, although task scheduler shows that it ran successfully it don’t log me out of any server I forgot to logout of and doesn’t produce a log.  Any ideas?

    This is the first time I try to use a log, (from an example I found) so if anyone has any suggestions please feel free to make any recommendations.

    $myLog = "C:\Users\myNetworkID\Documents\myScripts\RDPServerLogoff.log" 
    
    #LogIt function
    function Logit ([string]$ServerName,
      [string]$message,
      [switch]$LogToFile,
      [string]$LogToFileName="PSLog.log")
      {
      If ($LogToFile) {
        If (Test-Path $LogToFileName) {
          (Get-Date).ToString() + "-[" + $ServerName + "]::" + $message | Out-File $LogToFileName -Append
        }
        Else {
          (Get-Date).ToString() + "-[" + $ServerName + "]::" + $message | Out-File $LogToFileName
        }
      }
    }
    
    #Set variables for progress bar
    $progress = 0
    $Username = "myNetworkID"
    $ServerNames = ""
    
    #Build list of servers in Remote Desktop Connection Manager
    $RDGFile = $null
    $RDGFile = [xml](Get-Content C:\Users\myNetworkID\Documents\NetworkEngineering.rdg)
    foreach ($RDGServer in $RDGFile.RDCMan.file.group.server.properties){
      $ServerNames = $ServerNames + $RDGServer.Name -split(',')
    }
    
    #Start main foreach loop, search processes on all computers
    LogIt $env:COMPUTERNAME "<= Searching for logged on user =>" -LogToFile -LogToFileName $myLog
    foreach ($Server in $ServerNames){
      $Reply = $null
      $Reply = test-connection $Server -count 1 -quiet
      if($Reply){
        $proc = Get-WmiObject win32_process -ErrorAction SilentlyContinue -computer $Server -Filter "Name = 'explorer.exe'"
        if([string]::IsNullOrEmpty($proc)){
          LogIt $Server "No connections on this server" -LogToFile -LogToFileName $myLog
        }
        else {	
          ForEach ($p in $proc) {				
            $temp = ($p.GetOwner()).User
            If ($temp -eq $Username){
              LogIt $Server "$Username was logged on" -LogToFile -LogToFileName $myLog
              $sessionId = ((quser /server:$Server | Where-Object { $_ -match $UserName }) -split ' +')[2]
              logoff $sessionId /server:$Server
              LogIt  $Server "$Username was logged off" -LogToFile -LogToFileName $myLog
            }
          }
        }
      }
      else {
        LogIt $Server "Unable to ping this server" -LogToFile -LogToFileName $myLog
      }
    }
    
    LogIt $env:COMPUTERNAME "<= Search complete! =>" -LogToFile -LogToFileName $myLog
    LogIt -LogToFile -LogToFileName $myLog
    

    Tuesday, August 6, 2019 7:45 PM

Answers

  • If the script runs manually but not from the scheduler, then you don't have a scripting question.

    The only thing I can offer is to suggest the way that I typically use the scheduler to run scripts:

    Command: %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe

    Parameters: -NoProfile -ExecutionPolicy Bypass -File "D:\Path\scriptname.ps1"

    (Of course, replace D:\Path\scriptname.ps1 with the path and filename of the script file you want to run.)


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by BOfH-666 Wednesday, August 7, 2019 12:48 AM
    • Marked as answer by OSPJR Wednesday, August 7, 2019 1:18 PM
    Tuesday, August 6, 2019 8:34 PM
    Moderator

All replies

  • If the script runs manually but not from the scheduler, then you don't have a scripting question.

    The only thing I can offer is to suggest the way that I typically use the scheduler to run scripts:

    Command: %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe

    Parameters: -NoProfile -ExecutionPolicy Bypass -File "D:\Path\scriptname.ps1"

    (Of course, replace D:\Path\scriptname.ps1 with the path and filename of the script file you want to run.)


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by BOfH-666 Wednesday, August 7, 2019 12:48 AM
    • Marked as answer by OSPJR Wednesday, August 7, 2019 1:18 PM
    Tuesday, August 6, 2019 8:34 PM
    Moderator
  • Thank you, that did the trick.

    Script ran and produced a log file.

    Again, thanks.

    Wednesday, August 7, 2019 1:19 PM