Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell sequencing

Posted on 2014-03-06
5
Medium Priority
?
405 Views
Last Modified: 2014-03-07
Hello All - I have a powershell script that runs fine in the ISE, but when I run it from the right click prompt or a scheduled job, the sequencing is wrong.

What it does is the following:
1) copies template access file to production spot
2) runs a dtsx package to populate the access file - dtsx takes about 45 minutes, results in an access file about a gig in size.
3) zips the access file to a publicly available share with a date time name - resulting in about a 300 meg zip file.
4) deletes any zip files older than 3 days.

When I run it in the ISE, from the (RDP) console, it runs perfectly. 1 2 3 4 boom done.
When I run it by right-clicking and selecting "Run with Powershell", the sequencing is off, it runs 1 3 2 4.  The same happens when I run it from a scheduled task.

What gives?

Here is the script:

clear-host

# Allow script to read / write / execute
Set-ExecutionPolicy UnRestricted

# Copy template db to build spot
set-location "DiskLocation:\Templates"
if (@(get-childitem).count -gt 0)
    {Copy-Item "DiskLocation:\Templates\empty.mdb" "DiskLocation:\Production\Workarea\workingfile.mdb"}
   else
   {"No files found."}

# Run dtsx package to populate archive

# Run DTExec
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "DiskLocation:\PathTo\DTExec.exe"
$pinfo.FileName  # Output the DTExec path and filename
$DTExecArgs = "/f ""DiskLocation:\DTSXStore\createAccess.dtsx"""
$DTExecArgs      # Output the DTExecArgs variable

# The next few lines are required to make sure the process waits for
# the package execution to finish - Not sure if this helps
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $DTExecArgs
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$output = $p.StandardOutput.ReadToEnd()
$p.WaitForExit()
$DTExecExitCode = $p.ExitCode
$output
# DTExec Finished

# set date for zip file
$Datestr = get-date -format "yyyyMMdd"
# add Copy-ToZip module for zipping easy
Import-Module PowershellPack
# zip file
Copy-ToZip -File "DiskLocation:\Production\Workarea\workingfile.mdb" -ZipFile "DiskLocation:\PublicShare\workingfile_$Datestr.zip" -Force
# Delete files older than 3 days
$limit = (Get-Date).AddDays(-3)
$path = "DiskLocation:\PublicShare"
Get-ChildItem -Path $path -Recurse -Force -exclude *.config | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
0
Comment
Question by:enjama
5 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 39910022
This comment is wrong:
# Allow script to read / write / execute
Set-ExecutionPolicy UnRestricted

Open in new window

The command allows to execute scripts from any source and location, with no need to have it digitally signed. You should not set it to Unrestricted, use RemoteSigned instead, so downloaded scripts do not work without further work. That prevents trojan code to run without you knowing.

The difference between the powershell and ISE is that the latter runs in STA (single threaded appartment), which means no multi-threading. ISE will hence wait for results when powershell does not. Obviously your waiting for dtsexec to finish does not work. I'll have to think more about that part ...
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39910053
Brute-force approach:
Start-Sleep -s 3600

Just have the script wait for on hour before going to step 3.

HTH,
Dan
0
 
LVL 84

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 39910510
powershell -sta scriptname.ps1   -- this forces single threading mode

Other command line options are located http://technet.microsoft.com/en-us/magazine/ff629472.aspx
0
 

Author Closing Comment

by:enjama
ID: 39911948
Thanks Dave - This did the trick.  This seems like something that would be mentioned fairly prominently in the documentation, but maybe I missed it.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39912050
http:#a39910022 explains why that makes a difference.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Screencast - Getting to Know the Pipeline

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question