none
how to link powershell scripts and commands

    Question

  • Dear all,

    I had used codeplex site code for a fileserver - folder copy to Sharepoint 2010 document library.

    https://spfileupload.codeplex.com/releases/view/53408

    This works as a charm when I run it manually from fileserver folder to Sharepoint libary.

    When I run it on the console - I do the following things.

    1. Get-SPScripts.copy-filestosp

    2. Import-module

    3. running the command specified in the Docmanifest.xml -->  Copy-FilestoSP -LocalPath \\x.x.x.x\groups\IT\SOP -SiteUrl http://intranet/verticals/IT -Library "Projects" -ManifestFilePath "d:\Code\DocManifest.xml" - IncludeSubFolders -Overwrite

    My requirement:- I want to run this script and automatic sync/schedule should happen to SharePoint document library. i had tried creating the task for Get-Spscripts.copy-filestosp - but this is not working - as I need to do import-module and the command specified above.

    I am going through the powershell videos to understand it better.

    Let me know if you can provide inputs.

    Cheers

    Sathya

    Saturday, September 21, 2013 7:30 AM

Answers

  • Okay, I checked my batch job, and it does NOT run a "ps1" file; instead, it runs "in-line" powershell commands, within the "batch" script, one command, after the other:

    rem -----------------------------------------------------------------------------------------------------------------------
    rem - fixit.bat - removes TEMP attribute from all files
    rem - check start time and finish time
    rem - run this recursively against \\filesvr1\g$\Data
    rem
    rem - TO RUN this job & CREATE A LOG, set the below line in Task Scheduler:
    rem   C:\scripts\fixit filesvr1 > c:\scripts\fixit-filesvr1-LOG.txt
    rem   Above line runs THIS "fixit.bat" job against server named "filesvr1", creating log file in c:\scripts
    rem -----------------------------------------------------------------------------------------------------------------------
    @echo on
    echo.
    echo "Start time: " %date% " " %time%
    echo.
    :filesvr1
     rem - will be "g:\Data\..." like g:\Data\jsmith
     set fld=g:\Data
    rem - main proc
    powershell set-executionpolicy unrestricted
    powershell -command "& {Get-childitem ... {write-output $_}}"
    powershell -command "& {Get-childitem %fld% -recurse ...}"
    echo.
    echo "End time: " %date% " " %time%
    :fin

    Remember, in setting up scheduled tasks, you can create log files to help you troubleshoot. Also, remember that, in the above example, this part of the scheduled job is a "separate" section, placed in the "parameters" area of the scheduled job: " filesvr1 > c:\scripts\fixit-filesvr1-LOG.txt" - if you are NOT needing a log file, you can leave that out, but I suggest having a log file to help you further troubleshoot.

    So, the above batch job uses the "-command" construct to tell it to execute ("&") certain powershell commands - now it's all coming back to me!

    powershell -command "& {Get-childitem}"



    tnjman

    So your job might look like this:

    rem - spscopy.bat (to be placed in scheduler)
    rem - main proc
    powershell set-executionpolicy unrestricted
    powershell -command "& {Get-SPScripts.copy-filestosp}"
    powershell -command "& {Import-module}"
    rem - running the command specified in the Docmanifest.xml --> 
    powershell -command "& {Copy-FilestoSP -LocalPath \\x.x.x.x\groups\IT\SOP -SiteUrl http://intranet/verticals/IT -Library "Projects" -ManifestFilePath "d:\Code\DocManifest.xml" - IncludeSubFolders -Overwrite
    }"
    
    rem - you may have to tweak a bit, but this should work
    Please also remember to click the VOTE button, if you find my info helpful
    And, of course, if my info eventually helps you resolve the issue, please Mark as Answer. Thanks!!!




    Saturday, September 21, 2013 9:19 PM

All replies

  • Make a "wrapper" batch file (call it "runtask.bat") to run your commands - save your commands in a ".ps1" file

    Then follow my other post in this thread that says: "Okay, I checked my batch job..."

    "So your job might look like this:"

    -command & "{...}" 
    REFER TO MY ANSWER IN MY OTHER POST IN THIS THREAD. Thanks!

    • Edited by TNJMAN Saturday, September 21, 2013 9:37 PM clarify that answer is in my other post
    Saturday, September 21, 2013 7:52 AM
  • Dear TNJMAN MDHA,

    Many thanks for your prompt reply.

    It is not working.

    I also tried this way also in run-command.ps1

    Import-module.\Get-SPScripts.copy-filestosp.ps1

    Next line:-Copy-filesto-sp -LocalPath "C:\Codingtesting -SiteUrl "http://sp2010/sites/training" -Library "juji" ManifestFilePath "c:\Users\administrator\desktop\DocManifest.xml" -IncludeSubFolders -Overwrite

    and saved as .ps1 file

    Warm Regards,

    Sathya

    Saturday, September 21, 2013 5:55 PM
  • hi

    Beacause manually it's working , but don't on the created task

    I  think that you scheduled task isnt' correct. Please provide more details about that

    It's enough to create a new .ps1 file wich contain all 3 steps ( which works manually ) and then put the command line in scheduled task

    powershell "your_new_powershell_script.psx1"


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Saturday, September 21, 2013 6:59 PM
  • You're very welcome.
    Please also remember to click the VOTE button, if you find my info helpful
    And, of course, if my info eventually helps you resolve the issue, please Mark as Answer

    SEE my other post, using "in-line" powershell - I think it holds your answer. Thanks.

    -command & "{...}" 

    My other post has the actual working code to get the job done - it is what I am currently using in my own task scheduled jobs.



    • Edited by TNJMAN Saturday, September 21, 2013 9:45 PM cosmetic
    Saturday, September 21, 2013 8:54 PM
  • Okay, I checked my batch job, and it does NOT run a "ps1" file; instead, it runs "in-line" powershell commands, within the "batch" script, one command, after the other:

    rem -----------------------------------------------------------------------------------------------------------------------
    rem - fixit.bat - removes TEMP attribute from all files
    rem - check start time and finish time
    rem - run this recursively against \\filesvr1\g$\Data
    rem
    rem - TO RUN this job & CREATE A LOG, set the below line in Task Scheduler:
    rem   C:\scripts\fixit filesvr1 > c:\scripts\fixit-filesvr1-LOG.txt
    rem   Above line runs THIS "fixit.bat" job against server named "filesvr1", creating log file in c:\scripts
    rem -----------------------------------------------------------------------------------------------------------------------
    @echo on
    echo.
    echo "Start time: " %date% " " %time%
    echo.
    :filesvr1
     rem - will be "g:\Data\..." like g:\Data\jsmith
     set fld=g:\Data
    rem - main proc
    powershell set-executionpolicy unrestricted
    powershell -command "& {Get-childitem ... {write-output $_}}"
    powershell -command "& {Get-childitem %fld% -recurse ...}"
    echo.
    echo "End time: " %date% " " %time%
    :fin

    Remember, in setting up scheduled tasks, you can create log files to help you troubleshoot. Also, remember that, in the above example, this part of the scheduled job is a "separate" section, placed in the "parameters" area of the scheduled job: " filesvr1 > c:\scripts\fixit-filesvr1-LOG.txt" - if you are NOT needing a log file, you can leave that out, but I suggest having a log file to help you further troubleshoot.

    So, the above batch job uses the "-command" construct to tell it to execute ("&") certain powershell commands - now it's all coming back to me!

    powershell -command "& {Get-childitem}"



    tnjman

    So your job might look like this:

    rem - spscopy.bat (to be placed in scheduler)
    rem - main proc
    powershell set-executionpolicy unrestricted
    powershell -command "& {Get-SPScripts.copy-filestosp}"
    powershell -command "& {Import-module}"
    rem - running the command specified in the Docmanifest.xml --> 
    powershell -command "& {Copy-FilestoSP -LocalPath \\x.x.x.x\groups\IT\SOP -SiteUrl http://intranet/verticals/IT -Library "Projects" -ManifestFilePath "d:\Code\DocManifest.xml" - IncludeSubFolders -Overwrite
    }"
    
    rem - you may have to tweak a bit, but this should work
    Please also remember to click the VOTE button, if you find my info helpful
    And, of course, if my info eventually helps you resolve the issue, please Mark as Answer. Thanks!!!




    Saturday, September 21, 2013 9:19 PM