Solved

Powershell sequencing

Posted on 2014-03-06
5
395 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 70

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 82

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 70

Expert Comment

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

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
Suggested Courses

615 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