Solved

Powershell sequencing

Posted on 2014-03-06
5
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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 80

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

756 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