locked
Task Scheduler - PS 2.0 Issue RRS feed

  • Question

  • Alright, I've spent a lot of time reading posts about scheduling tasks and how an interactive script works fine while a task scheduler script fails ... this is my issue and question:

    I've narrowed down the issue to the substring command.  The rest of the script executes fine, except for this line:

    $AllocID = $myRow.Substring(0,3)

    My question ... can I import a module into the script to allow this function to work under "system" on Windows 2008 R2 server, PS 2.0 ...

    Thanks,
    --- gmsc

    Monday, June 9, 2014 1:40 PM

Answers

  • Thanks guys ... the Get-Content was the problem.  Trying to run the report from the 3rd party software shell was not working under the "system" security group.  I ended up creating a new user account with job batch priv. and that seemed to resolve it.

    I appreciate your time.

    ---- gmsc

    • Marked as answer by Bill_Stewart Monday, June 9, 2014 6:39 PM
    Monday, June 9, 2014 6:35 PM

All replies

  • What?

    SubString() is built in.  Why do you say it won't work.  What is $AllocID?

    There is no Task or job scheduling in PowerShell 2.0.

    SubString is a method (not a function) on string objects only.  You cannot substring a number.

    Note the error and how it cN be "stringified" to work.

    PS C:\scripts> $AllocId=12345
    PS C:\scripts> $AllocId.SubString(0,3)
    Method invocation failed because [System.Int32] does not contain a method named 'SubString'.
    At line:1 char:1
    + $AllocId.SubString(0,3)
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    
    PS C:\scripts> "$AllocId".SubString(0,3)
    123

    Only select objects can be meaningfully stringified.


    ¯\_(ツ)_/¯

    Monday, June 9, 2014 1:53 PM
  • Thanks for replying ...

    In my script I read a text file and I'm using the substring method to assign a string that contains an ID number or a third party application.  This works fine when I open up PS and run it.

    However, when I schedule the task (user system) this assignment does not work.  I thought that I might have to import a reference for it to recognize the substring method, however, I'm not really sure what the fully qualified path would be?

    ---- gmsc

    Monday, June 9, 2014 2:20 PM
  • Sorry but you are being way too vague.  I cannot guess at what you are doing.

    There is absolutely NO reason for a substring to not work if you are not getting errors first.

    I can say that you have an error in your script that is likely causing the object to be null.  This will cause a failure.  It is your script that is wrong and not PowerShell that is in need.

    Running under "System" many things will not work as you expect them to.


    ¯\_(ツ)_/¯

    Monday, June 9, 2014 2:34 PM
  • jrv,

    Let me try to explain this better ...  Here is a portion of the script (below):

    #Read the allocation listing into an array
    $AllocList = Get-Content $Log_AllocList
    #Read through the allocation listing array
    foreach ($myRow in $AllocList){
       #Debug: Writes to the log if the script makes it here ...
       Add-Content $Log_Activity ("  Debug: Line #50")

        #Define the allocation id, consumer, client
        $AllocID = $myRow.Substring(0,3)
        $AllocConsumer = $myRow.Substring(10,10)
        $AllocClient = $myRow.Substring(22,3)

        #Debug:  Write the value of $AllocID
        Add-Content $Log_Activity ("Debug: Line #58 " + $AllocID)
    }

    Here is a segment of the text file $log_AllocList that I'm trying to read:

    ALLOC    CONSUMER     CLIENT       RGROUP       RESOURCE     ALLOCATED ACTI    
    5        /Management* EGO_SERVICE* ManagementH* w51sgpp01    1         1       
    220      /IT_Testing* SD_SDK_/IT_* ManagementH* w51sgpp01    1         1       
    222      /IT_Testing* SSM_/IT_Tes*                                             
    265      /IT_Testing  SD_SDK_/IT_* ManagementH* w52sgpp02    1         1       
    266      /IT_Testing  SSM_/IT_Tes*                                             
    6        /Management* EGO_SERVICE* ManagementH* w51sgpp01    1         1      

    When I run the script in the PS command window, I just the expected result (like the output below):

    Debug: Line #50
    Debug: Line #58 ALL
    Debug: Line #50
    Debug: Line #58 5  
    Debug: Line #50
    Debug: Line #58 220
    Debug: Line #50
    Debug: Line #58 222
    Debug: Line #50
    Debug: Line #58 265
    Debug: Line #50
    Debug: Line #58 266
    Debug: Line #50
    Debug: Line #58 6

    When I run the script in task scheduler, the assignments are not being made so (like the output below):

    Debug: Line #50

    As only one line it output, I assumed an error so I revise the script to include a try/catch block around the assignment of $AllocID and the error returned is "You cannot call a method on a null-valued expression". 

    --- gmsc 

    Monday, June 9, 2014 3:34 PM
  • Of course, the error message means that the variable $myRow is null at that point in script execution.


    -- Bill Stewart [Bill_Stewart]

    Monday, June 9, 2014 3:41 PM
  • So you have your answer.   The $myRow is null. 

    It is likely that something you left out is the cause.

    One problem with your methos is that "foreach" enumerates once on a null array.  THis would give you this error.  Perhaps the get-content is failing.

    Try{
        if($AllocList=Get-Content .... -EA Stop){
             # results not null
        }else{
            #log null results and quit.
       }
    }
    Catch{
        # write your log and exit.
    }
    
    


    ¯\_(ツ)_/¯

    Monday, June 9, 2014 3:49 PM
  • Thanks guys ... the Get-Content was the problem.  Trying to run the report from the 3rd party software shell was not working under the "system" security group.  I ended up creating a new user account with job batch priv. and that seemed to resolve it.

    I appreciate your time.

    ---- gmsc

    • Marked as answer by Bill_Stewart Monday, June 9, 2014 6:39 PM
    Monday, June 9, 2014 6:35 PM
  • As jrv said, information not present in your question was the cause. Glad you figured it out.

    -- Bill Stewart [Bill_Stewart]

    Monday, June 9, 2014 6:40 PM