Solved

Powershell sequencing

Posted on 2014-03-06
5
378 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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
Brute-force approach:
Start-Sleep -s 3600

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

HTH,
Dan
0
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
http:#a39910022 explains why that makes a difference.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
Synchronize a new Active Directory domain with an existing Office 365 tenant
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now