Solved

Powershell sequencing

Posted on 2014-03-06
5
383 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 69

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 34

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 79

Accepted Solution

by:
David Johnson, CD, MVP earned 500 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 69

Expert Comment

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

810 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