none
cannot run powershell in scheduled task

    Question

  • HI 

    I have been trying to run the powershell move-item using a scheduled task and im having all sorts of problems 

    when i run the command via command line it works but when running via powershell the task completes but the file does not get moved 

    this is what i currenntly have in my task 

    powershell.exe  -command & ("{move-item -LiteralPath "\\serverA\c$\powershell\AD_users\AD_Users.csv" -destination "\\serverb\D$\Shared Folders\Flatfiles\IS" }"

    any help  would be useful

    thanks 

    Jason  

    Thursday, October 03, 2013 12:00 PM

Answers

  • too many unnecessary quotes too.

    powershell.exe  -command move-item -LiteralPath \\serverA\c$\powershell\AD_users\AD_Users.csv -destination \\serverb\D$\Shared Folders\Flatfiles\IS

    Everything AFTER -command is taken as the command

    Note the following:

    $ works
    powershell -command dir c:\

    #NOT
    powershell -command {dir c:\}

    #NOT
    powershell -command dir "c:\"

    #works
    powershell -command dir 'c:\'

    #works
    powershell -command dir 'c:\program files'

    #works
    powershell -command move-item 'c:\scripts\test.txt' 'c:\temp'

    There are other variations that can be applied under different conditions.  You have toidentify where the conflict is in order to know which one works.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, October 03, 2013 3:00 PM
    • Marked as answer by jasonjscott Thursday, October 03, 2013 3:39 PM
    Thursday, October 03, 2013 2:58 PM

All replies

  • Hi Jason,

    Does the account that the task is running as have access to the paths? Also, are you running it with highest privileges?

    (I'm also going to assume that the hanging ( after the call operator is a typo, not there on purpose).


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,000+ strong and growing)


    Thursday, October 03, 2013 1:18 PM
  • too many unnecessary quotes too.

    powershell.exe  -command move-item -LiteralPath \\serverA\c$\powershell\AD_users\AD_Users.csv -destination \\serverb\D$\Shared Folders\Flatfiles\IS

    Everything AFTER -command is taken as the command

    Note the following:

    $ works
    powershell -command dir c:\

    #NOT
    powershell -command {dir c:\}

    #NOT
    powershell -command dir "c:\"

    #works
    powershell -command dir 'c:\'

    #works
    powershell -command dir 'c:\program files'

    #works
    powershell -command move-item 'c:\scripts\test.txt' 'c:\temp'

    There are other variations that can be applied under different conditions.  You have toidentify where the conflict is in order to know which one works.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, October 03, 2013 3:00 PM
    • Marked as answer by jasonjscott Thursday, October 03, 2013 3:39 PM
    Thursday, October 03, 2013 2:58 PM
  • Adding to  JRV,

    Powershell  " & { dir c:\ } "   will also works .

    but 

    powershell  -command & "{ dir c:\ }"  won't works .


    MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    Thursday, October 03, 2013 3:26 PM
  • Adding to  JRV,

    Powershell  " & { dir c:\ } "   will also works .

    but 

    powershell  -command & "{ dir c:\ }"  won't works .


    MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    That does NOT work:

    PS C:\scripts> powershell  -command & "{ dir c:\ }"
    At line:1 char:22
    + powershell  -command & "{ dir c:\ }"
    +                      ~
    Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : AmpersandNotAllowed

    From CMD:

    C:\scripts>powershell  -command & "{ dir c:\ }"
    Cannot process the command because of a missing parameter. A command must follow -Command.


    ¯\_(ツ)_/¯

    Thursday, October 03, 2013 3:33 PM
  • Try to add the following to a batch file to call the ps1 script; then have the scheduled task call the batch file

    powershell.exe -executionpolicy bypass -file H:\Desktop\qfarm\Powershell\nameofscript.ps1

    If you want the results output to a file, use the following;

    powershell.exe -executionpolicy bypass -file H:\Desktop\qfarm\Powershell\nameofscript.ps1 > H:\Desktop\qfarm\Powershell\nameoffile.txt


    Lee Mossolle

    Thursday, October 03, 2013 3:40 PM
  • Try to add the following to a batch file to call the ps1 script; then have the scheduled task call the batch file

    powershell.exe -executionpolicy bypass -file H:\Desktop\qfarm\Powershell\nameofscript.ps1

    If you want the results output to a file, use the following;

    powershell.exe -executionpolicy bypass -file H:\Desktop\qfarm\Powershell\nameofscript.ps1 > H:\Desktop\qfarm\Powershell\nameoffile.txt


    Lee Mossolle

    It will work but it is the hard way and produces a lack of task independence.  It is useful if you need to change the script in the future.

    The following is an even more flexible solution:

    powershell.exe -file c:\test\myscript.ps1 -windowstyle hidden

    It is highly recommended that any scripts run under task manager be signed.


    ¯\_(ツ)_/¯

    Thursday, October 03, 2013 3:44 PM
  • Adding to  JRV,

    Powershell  " & { dir c:\ } "   will also works .

    but 

    powershell  -command & "{ dir c:\ }"  won't works .


    MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    That does NOT work:

    PS C:\scripts> powershell  -command & "{ dir c:\ }"
    At line:1 char:22
    + powershell  -command & "{ dir c:\ }"
    +                      ~
    Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : AmpersandNotAllowed

    From CMD:

    C:\scripts>powershell  -command & "{ dir c:\ }"
    Cannot process the command because of a missing parameter. A command must follow -Command.


    ¯\_(ツ)_/¯

    JRV it works , see below :

    from Powershell  v 2 

    PS C:\Users\administrator.DAG> powershell -command "& {dir}"


        Directory: C:\Users\administrator.DAG


    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d-r--         6/26/2013   9:11 PM            Contacts
    d-r--          9/1/2013   7:03 PM            Desktop
    d-r--         9/30/2013   5:36 PM            Documents
    d-r--         6/26/2013   9:11 PM            Downloads
    d-r--         6/26/2013   9:11 PM            Favorites
    d-r--         6/26/2013   9:11 PM            Links
    d-r--         6/26/2013   9:11 PM            Music
    d-r--         6/26/2013   9:11 PM            Pictures
    d-r--         6/26/2013   9:11 PM            Saved Games
    d-r--         6/26/2013   9:11 PM            Searches
    d-r--         6/26/2013   9:11 PM            Videos
    -a---         9/30/2013   5:58 PM          0 3436
    -a---         6/29/2013   5:00 PM        223 volshext.log

    In Cmd prompt :

    C:\Users\administrator.DAG>powershell -command "& {dir }"


        Directory: C:\Users\administrator.DAG


    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d-r--         6/26/2013   9:11 PM            Contacts
    d-r--          9/1/2013   7:03 PM            Desktop
    d-r--         9/30/2013   5:36 PM            Documents
    d-r--         6/26/2013   9:11 PM            Downloads
    d-r--         6/26/2013   9:11 PM            Favorites
    d-r--         6/26/2013   9:11 PM            Links
    d-r--         6/26/2013   9:11 PM            Music
    d-r--         6/26/2013   9:11 PM            Pictures
    d-r--         6/26/2013   9:11 PM            Saved Games
    d-r--         6/26/2013   9:11 PM            Searches
    d-r--         6/26/2013   9:11 PM            Videos
    -a---         9/30/2013   5:58 PM          0 3436
    -a---         10/3/2013  10:00 PM          0 powershell
    -a---         6/29/2013   5:00 PM        223 volshext.log

    In Powershell V 3 :

    PS C:\temp> powershell -command " & {dir }"


        Directory: C:\temp


    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d----        14-09-2013  AM 10:20            Scripts

    In CMD prompt :


    PS C:\temp> cmd
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\temp>powershell -command "& {dir}"


        Directory: C:\temp


    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d----        14-09-2013  AM 10:20            Scripts

    JRV  , I know you are genius  and you don't like to other people  make post in this forum .

    But , You can't do this .



    Thursday, October 03, 2013 4:35 PM