0

In my PowerShell script - one function's output is another function's input.
For Eg: Function CreateReport($x) cannot run until unless the Function ParseXml($x) runs. What if a user directly runs the 2nd function before running the 1st.
How can I check if 1st function is already run to continue with 2nd, i.e, first run the 1st function (generate the txt file) then run the 2nd? if first func is already run do not re-run it.
For Eg: Suppose I have a TestFunc.ps1 file having 2 functions as below

$X = "C:\XmlPath\file1.xml"
Function ParseXml($X)
{
#Read xml and output contents in a txt file    
}

#This function should execute only after the function Parsexml($X) and if Pasrsexml() has run before and generated the output, it shouldnot be allowed to re-run here
Function CreateReport($T) 
{
#from the txtfile Create csv
}

3 Answers 3

2

According to this and your other question How to alias a parameterized function as a flag in powershell script? you are trying to implement a so called build script. Instead of inventing a wheel (implementing task dependencies, watching tasks to be run once, etc.) take a look at some already implemented tools like psake or Invoke-Build. They are designed for PowerShell and they do exactly what you want (run specified task sets, maintain task dependencies, run tasks once, etc.). These tools require a little bit of learning, of course, but in a long run they are worth to be learned.

1

If ParseXml function output a file, you can, in the CreateReport function, test for the existence of this file with Test-Path cmdlet:

if exists continue with CreateReport function else call the ParseXml function before continue.

1
  • This works.. Thanks a lot Christian!!! @VonPryz - I second your suggestion. I did a bit of learning and got it working :)
    – ashish g
    Commented Oct 17, 2012 at 4:31
0

Use a flag. Set the flag in ParseXml function and check it in the CreateReport function. If the flag isn't set, print an error and exit, othervise run the reporting code. Remember to clear the flag when the process is complete.

You can use a flag variable. For more persistent flags, consider using flag files or setting the flag in a database.

1
  • 1
    Try to work something out first. Ask for help on specific task if you get stuck.
    – vonPryz
    Commented Oct 16, 2012 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.