2 Replies Latest reply on May 11, 2018 11:41 AM by t3chno

    SAM Powershell Script Not working

    t3chno

      The script works fine directly on the server, but not when run through SAM.

       

      My debugging has highlighted the $Process command is not being set, the only thing i can think of is the | [BAR] character is not being passed correctly?

       

      Any Thoughts and suggestions are welcome.

       

      Thanks

       

      David

       

      Function UpTime($ProcessTitle)
      {
      $Process = Get-Process | Where-Object {$_.MainWindowTitle -LIKE "*$ProcessTitle*" } -ErrorAction SilentlyContinue
      $Date = Get-Date

      Set-Content -Path "C:\MyTextFile.txt" -Value "DEBUG-Process:$Process " -Force

      If($Process -eq $null)
      {
      $Uptime = '0'
      }
      Else
      {
      If($Process.Count -eq $null)
      {
      $Uptime = ($Date.Subtract($Process.StartTime)).TotalMinutes
      }
      Else
      {
      $Sorted = $Process | Sort-Object StartTime
      $Process = $Sorted[0]
      $Uptime = ($Date.Subtract($Process.StartTime)).TotalMinutes
      }
      $Uptime = [int]$Uptime
      }
      Return $Uptime
      }

      $ProcessTitle = $args[0]
      $Uptime = UpTime $ProcessTitle
      Write-Host 'Message: The longest instance of process' $ProcessTitle 'has been running for (in minutes)'
      Write-Host 'Statistic: '$Uptime

        • Re: SAM Powershell Script Not working
          m-milligan

          I see that your script accesses the server's filesystem:

           

          Set-Content -Path "C:\MyTextFile.txt" -Value "DEBUG-Process:$Process " -Force

           

          Are you running the various SAM component services as LocalSystem or as a named user account? Services running as LocalSystem don't have the same access to the filesystem as an actual user. Try changing the services to run as a user that has access to C:\MyTextFile.txt (or to the directory it resides in).

          • Re: SAM Powershell Script Not working
            t3chno

            The text file creates just fine and is just for debugging.

             

            the script does not write the $Process to the text file, so the $Process is not getting set from:

            $Process = Get-Process | Where-Object {$_.MainWindowTitle -LIKE "*$ProcessTitle*" } -ErrorAction SilentlyContinue