Getting PS script to run from Task Scheduler
i have ps script wrote invokes sql script queries sql db , updates records. then, using invoke-sqlcmd2 cmdlet, pulls name of changed records , uses move-item move the correlating file 1 folder another. it writes out various text files i'm checking make sure runs properly.
i want set script in task scheduler automatically runs once week i'm having trouble getting run properly.
when use command in 'run' results expected:
powershell -executionpolicy bypass -command "& 'c:\path\to\script.ps1' "
i , text files populated , went smoothly. when use task scheduler run same command, text files "wasitthere" , "isittherenow" empty instead of listing true or false each item.
i know module importing because text file called "imagename" populated correctly image names going move. i can't figure out problem is.
here code, please kind first attempt @ powershell! also, realize sql queries kind of poorly written have overcome fact using sql server 2005 doesn't have built-in ps modules. the sqltools module, while nice, has odd limitations.
import-module sqltools invoke-sqlcmd2 -serverinstance "sql1_server" -database docman -query "select doc_id dmm_view_doc_userfields str_value = 'branch daily deposit'" | select-object -expandproperty doc_id | out-file -filepath "c:\batch\powershell_scripts\beforerun.txt" invoke-sqlcmd2 -serverinstance "sql1_server" -inputfile "c:\batch\powershell_scripts\branchdailydeposit-to-dailydeposit.sql" start-sleep -second 2 $sourcepath = 'z:\imagearchive' $destpath = 'z:\privateimagearchive\dailydeposits' invoke-sqlcmd2 -serverinstance "sql1_server" -database docman -query "select top 10000 coalesce(dmm_view_documents.fname,cast(dmm_view_documents.unique_id varchar(255)) + '.tif') fname dmm_view_documents inner join dmm_view_doc_userfields on (dmm_view_doc_userfields.doc_id = dmm_view_documents.unique_id) str_value = 'branch daily deposit moved' , datediff(day,dmm_view_documents.scan_date,getdate())<14" | select-object -expandproperty fname | foreach-object {test-path (join-path 'z:\imagearchive' ($_.tostring()))} | out-file -filepath "c:\batch\powershell_scripts\wasitthere.txt" start-sleep -second 2 invoke-sqlcmd2 -serverinstance "sql1_server" -database docman -query "select top 10000 coalesce(dmm_view_documents.fname,cast(dmm_view_documents.unique_id varchar(255)) + '.tif') fname dmm_view_documents inner join dmm_view_doc_userfields on (dmm_view_doc_userfields.doc_id = dmm_view_documents.unique_id) str_value = 'branch daily deposit moved' , datediff(day,dmm_view_documents.scan_date,getdate())<14" | select-object -expandproperty fname | foreach-object {move-item (join-path 'z:\imagearchive' ($_.tostring())) (join-path 'z:\privateimagearchive\dailydeposits' ($_.tostring()))} invoke-sqlcmd2 -serverinstance "sql1_server" -database docman -query "select top 10000 coalesce(dmm_view_documents.fname,cast(dmm_view_documents.unique_id varchar(255)) + '.tif') fname dmm_view_documents inner join dmm_view_doc_userfields on (dmm_view_doc_userfields.doc_id = dmm_view_documents.unique_id) str_value = 'branch daily deposit moved' , datediff(day,dmm_view_documents.scan_date,getdate())<14" | select-object -expandproperty fname | out-file -filepath "c:\batch\powershell_scripts\imagename.txt" invoke-sqlcmd2 -serverinstance "sql1_server" -database docman -query "select top 10000 coalesce(dmm_view_documents.fname,cast(dmm_view_documents.unique_id varchar(255)) + '.tif') fname dmm_view_documents inner join dmm_view_doc_userfields on (dmm_view_doc_userfields.doc_id = dmm_view_documents.unique_id) str_value = 'branch daily deposit moved' , datediff(day,dmm_view_documents.scan_date,getdate())<14" | select-object -expandproperty fname | foreach-object {test-path (join-path 'z:\privateimagearchive\dailydeposits' ($_.tostring()))} | out-file -filepath "c:\batch\powershell_scripts\isittherenow.txt"
Windows Server > Windows PowerShell
Comments
Post a Comment