Need assitance in writing script

Hi Guys,

I need assitnace in writing new script whcih need to look for stage number and start executing from that stage. 

I Understand run key location would be right place for script path. Please correct me if i am wrong. 

Expected script 

define global variables(envirnoment varibles)


Stage 1:

Restart function(After restart it need to start from stage 2) 

Stage 2:

Stage 3:

End of script

Can anyone please help me with script. 

Thanks in advance




  • Hi All,

    Please let me know if i need to post more information because i dnt have any clue how do i do this. Please assit



  • I doubt anyone can write a script for you based on such a vague request.

    Besides, that's not the point of this forum anyway. We are here to help you with specific problems with scripts that you have written yourself.

    If you want a script written for you, you can always fill in a request at the Microsoft Script Centre or try hire someone to write the script for you.

  • I know the importance of forum dude and i give lot of respect for ppl and their time...i posted this for input how to approach for this kind of scenario and i didnt say "write a script for me" . I posted as my expected script.

    We have a old kix script which was written as stages so i am re writing those scripts in powershell. Hope i make sense now.


  • Ok cool. If you have any specific problems with writing your script, please feel free to post them then.

  • There are so many possible ways to do something like this, it's not even funny.  Probably the #1 question I have after reading this is, what is the trigger?  In other words, when do you look to examine what phase you're in?  Is it on system boot-up or some other event?

    If it's on boot-up, that would be fairly easy to implement using the task scheduler. 

    • You would create a task with a trigger of "At startup".
    • At that point I would have it run a "master" script, e.g., C:\scripts\master.ps1.
    • The master script would read a checkpoint file to see what it should do next.  For example, if C:\scripts\checkpoint.txt contained ...
      • 0 = no action pending, exit the master script. Have a nice day.
      • 1 = Run the C:\scripts\Phase1.ps1 script (which also updates the checkpoint file)
      • 2 = Run the C:\scripts\Phase2.ps1 script (which also updates the checkpoint file)
      • 3 ... well you get the point
    • Any data that needed to be persisted between phases should be written to a file, e.g., C:\scripts\master.txt.  This could be a text file, CSV, xml, whatever.  If you were clever, you could make if part of the checkpoint file.

    The bottom line is, we really don't know what you're trying to accomplish from the very limited description you gave.

  • Hi Bob,

    Thanks for your reply.

    Currently we have server 2008 whenever i roll up new server, i need to do set of task such as server rename, joinning domain and other tasks 

    My idea was to have a master.ps1 which contains different stages like stage 1: server rename , stage 2 : joinning domain, stage 3 .. and goes on.

    Trigger i am planning to have a text file which stores stage number whenever server reboots it need to check for text file and stage number and start executing respective stage from master.ps1 file.



    Stage desc:


    Master. ps1

    Checking text_file.txt

    #stage code 


    #code server rename code.

    #write stage 2 number to text file 

    Reboot system.(After reboot script should execut from stage 2)


    #joining domain code.

    #write stage 3 number to text file 

    reboot system.(After reboot script should execut from stage 3)

    Stage 3:

    #other code

    #write stage 4 number to text file 


    #end of script

    i completed my individual code for each stage. Now i am clue less how can make this into single script.

    Please let me know if this can be done in better way. 

  • Have you looked at the free Microsoft Deployment Toolkit? It's designed for doing exactly what you are describing.

  • I've done something similar in the past, with checkpoints.

    The code i've used looks like this

    ## Basic checkpointing script ##
    $Path = "C:\temp\checkpoint.txt"
    $CP = Get-Content $Path
    $Complete = "1"

    if($CP -eq $null)
    ## Creating Checkpoint file ##
    New-Item -Path "C:\temp\" -Name "Checkpoint.txt" -ItemType file
    Set-Content -Path $Path -Value "1"

    if($CP -eq "1")
    ## Rename servers ##
    Write-Host "Stage 1 achieved"
    Set-Content -Path $Path -Value "2"

    if($CP -eq "2")
    ## Join domain ##
    Write-Host "Stage 2 achieved"
    Set-Content -Path $Path -Value "3"

    if($CP -eq "3")
    ## Other checkpoint ##
    Write-Host "Stage 3 achieved"
    Set-Content -Path $Path -Value "4"

    if($CP -eq "4")
    ## Other Checkpoint ##
    Write-Host "Stage 4 achieved"
    Set-Content -Path $Path -Value "5"

    if($cp -eq "5")
    ## Break ##
    $Complete = "0"

    }while($Complete -eq "1")

    This works since the script creates and updates a text file in C:\temp and after finishing the last step, setting a value that breaks the loop.

    Hope you find it useful!
  • Thanks Optical9090..!!

    I modified some of your code to adjust my script. I used Switch case for stage and while condition for loop purpose. 

    Thanks again for your idea.

  • Glad you found my input helpful

    Also if you want to add the parts of the code that you altered here that would be cool, so that others might use it as well for similar scenarios :)

  • Javascript is interpreted like HTML, so you can use any text software and save it as HTML. But software like Notepad++ is useful because the keyword will be highlighted.
    Javascript language format is like C/C++ style.
    Because it is interpreted, and i used Notepad++, i can't do some syntax checking, i checking it manually.
    If you need syntax checking, you should search the software on internet.

    Example codes, save it with extension .html

    X = confirm("Ready for Javascript ?");
    if (X==1)
    document.write("Happy learning Javascript");
    document.write("Maybe Next Time");

    or copy it to your browser and play it :

    javascript:X = confirm("Ready for Javascript ?");if (X==1) document.write("Happy learning Javascript"); else document.write("Maybe Next Time");

    best accounting essay writer