Windows Vista Forums

Finer control over variable execution

  1. #1


    =?Utf-8?B?U0RM?= Guest

    Finer control over variable execution

    Greetings,

    I often need to generate images of various versions of Microsoft Windows
    containing the latest updates slipstreamed into the media, so when the
    company I work for deploys images to machines, they are already 100%
    up-to-date and secure, allowing for immediate use by the user with no
    downtime while the machines updates through Windows/Microsoft Update or the
    local WSUS Server.

    I had some spare time a few days ago, and am attempting to create a
    PowerShell script that automatically generates these images, so I only need
    dump the update executable into the respective directory, and it is
    automatically added to the latest image. However, I am having trouble parsing
    arguments to the image program through the script. The following is an
    excerpt:

    "Preparing to generate image..."
    $IGS_CDImage_Path = "..\Tools\cdimage.exe "
    $IGS_CDImage_VolumeLabel = "-lWXP-PRO-JUN06"
    $IGS_CDImage_TimeStamp_Raw = get-date -uformat "%m/%d/%Y/%H/%M/%S "
    $IGS_CDImage_TimeStamp = "-t" + $IGS_CDImage_TimeStamp_Raw
    $IGS_CDImage_BootSector = "-b `"..\src\bootsector.bin`" -p0 -e "
    $IGS_CDImage_CmdOptions = "-g -h -j1 -x -o -w1 -m "
    $IGS_CDImage_SourceRoot = "`"..\src`" "
    $IGS_CDImage_TargetFile = "`"$IGS_Dir" + "\" + "$IGS_File`""
    $IGS_CDImage_Execute = $IGS_CDImage_Path + $IGS_CDImage_VolumeLabel +
    $IGS_CDImage_TimeStamp + $IGS_CDImage_BootSector + $IGS_CDImage_CmdOptions +
    $IGS_CDImage_SourceRoot + $IGS_CDImage_TargetFile
    ""
    "Initiating image creation..."
    $IGS_CDImage_Execute
    & $IGS_CDImage_Execute

    However, this always results in an error:
    '..\Tools\cdimage.exe -lWXP_PRO_JUN06 -t08/26/2006/20/41/15 -b
    "..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    XP Pro - June 2006 Update.iso"' is not recognized as a cmdlet, function,
    operable program, or script file. At X:\Scripts\Windows XP Image
    Generation.ps1:
    44 char:2
    + & <<<< $IGS_CDImage_Execute

    I am at something of a loss as to the cause of this error, but I suspect I
    am making some errors in not specifying with enough detail how to execute the
    output. However, the contents of the $IGS_CDImage_Execute variable at
    execution appear fine:
    ...\Tools\cdimage.exe -lWXP_PRO_JUN06 -t08/26/2006/20/41/15 -b
    "..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    XP Pro - June 2006 Update.iso"

    Ant help would be greatly appreciated,

    -SDL

      My System SpecsSystem Spec

  2. #2


    Jouko Kynsijärvi Guest

    Re: Finer control over variable execution

    SDL wrote:
    > & $IGS_CDImage_Execute


    > However, this always results in an error:
    > '..\Tools\cdimage.exe -lWXP_PRO_JUN06 -t08/26/2006/20/41/15 -b
    > "..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src"
    > "C:\\Windows XP Pro - June 2006 Update.iso"' is not recognized as a
    > cmdlet, function, operable program, or script file.


    Try this instead of "& $IGS_CDImage_Execute":

    Invoke-Expression $IGS_CDImage_Execute

    # here PS is interpreting the _whole string_ as a cmdlet/function etc. name:
    $x = 'notepad a.txt'; &$x
    'notepad a.txt' is not recognized as a cmdlet, function, operable program,
    or script file.

    # with invoke-expression PS is actually parsing the string:
    $x = 'notepad a.txt'; invoke-expression $x



      My System SpecsSystem Spec

  3. #3


    Jeffrey Snover [MSFT] Guest

    Re: Finer control over variable execution

    Jouko has it right.
    & says "use the next token as the NAME of the thing to execute"

    --
    Jeffrey Snover [MSFT]
    Windows PowerShell/Aspen Architect
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, no confers rights.
    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx



      My System SpecsSystem Spec

  4. #4


    =?Utf-8?B?U0RM?= Guest

    Re: Finer control over variable execution

    Greetings again,

    Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    exactly what I was looking for. However, I'm still having issues with the
    command string being interpreted incorrectly. For example:

    '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    XP Pro - June 2006 Update.iso"'
    Invoke-Expression : You must provide a value expression on the right-hand
    side of the '-' operator. At X:\Scripts\Windows XP Image
    Generation.ps1:
    44 char:18
    + invoke-expression <<<< $IGS_CDImage_Execute

    This points to the '.' in the cdimage.exe part of the string. Simply put,
    all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    executable, and the rest of the string be passed "raw" and unmodified
    directly to the cdimage executable for it to interpret itself. Thanks again
    for the help provided.

    -SDL
    "Jeffrey Snover [MSFT]" wrote:

    > Jouko has it right.
    > & says "use the next token as the NAME of the thing to execute"
    >
    > --
    > Jeffrey Snover [MSFT]
    > Windows PowerShell/Aspen Architect
    > Microsoft Corporation
    > This posting is provided "AS IS" with no warranties, no confers rights.
    > Visit the Windows PowerShell Team blog at:
    > http://blogs.msdn.com/PowerShell
    > Visit the Windows PowerShell ScriptCenter at:
    > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    Re: Finer control over variable execution

    I did not try this out, but may be this will work:

    ###############################################
    $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    $StartInfo.FileName = '..\Tools\cdimage.exe'
    $StartInfo.Arguments = '-lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    XP Pro - June 2006 Update.iso"'
    $StartInfo.WorkingDirectory = 'C:\' #fill in the correct directory here
    $StartInfo.LoadUserProfile = $true
    $StartInfo.UseShellExecute = $true
    [System.Diagnostics.Process]::Start($StartInfo)
    ###############################################

    Hope that helps.

    BTW: It might be easier to use only absolute path names and (if feasible)
    fewer arguments first, so that you can eliminate possible sources of error.
    This might help to localize the cause of failure.

    --
    greetings
    dreeschkind

    "SDL" wrote:

    > Greetings again,
    >
    > Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    > exactly what I was looking for. However, I'm still having issues with the
    > command string being interpreted incorrectly. For example:
    >
    > '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > XP Pro - June 2006 Update.iso"'
    > Invoke-Expression : You must provide a value expression on the right-hand
    > side of the '-' operator. At X:\Scripts\Windows XP Image
    > Generation.ps1:
    > 44 char:18
    > + invoke-expression <<<< $IGS_CDImage_Execute
    >
    > This points to the '.' in the cdimage.exe part of the string. Simply put,
    > all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    > executable, and the rest of the string be passed "raw" and unmodified
    > directly to the cdimage executable for it to interpret itself. Thanks again
    > for the help provided.
    >
    > -SDL
    > "Jeffrey Snover [MSFT]" wrote:
    >
    > > Jouko has it right.
    > > & says "use the next token as the NAME of the thing to execute"
    > >
    > > --
    > > Jeffrey Snover [MSFT]
    > > Windows PowerShell/Aspen Architect
    > > Microsoft Corporation
    > > This posting is provided "AS IS" with no warranties, no confers rights.
    > > Visit the Windows PowerShell Team blog at:
    > > http://blogs.msdn.com/PowerShell
    > > Visit the Windows PowerShell ScriptCenter at:
    > > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    > >
    > >
    > >


      My System SpecsSystem Spec

  6. #6


    =?Utf-8?B?U0RM?= Guest

    Re: Finer control over variable execution

    Hello again dreeschkind

    Your method does indeed work, there are no longer any problems with execution!

    However, I have one last minor issue, purely aesthetic, but using this
    method the process is launched in a new window (cmd.exe) and instantly closes
    upon completion. I guess there are two possibilities here:

    1. My first preference would be a method to simply run the process inside
    the PowerShell window(process), or failing that, background the cdimage.exe
    process but redirect all of it's output to the PowerShell process, which is
    almost just as good. I took a look at the relevant references in the MSDN
    library:
    http://msdn2.microsoft.com/en-us/lib...roperties.aspx.
    Would the Redirect* objects be of use here? Sorry, but I'm not a programmer

    2. Failing that, I guess the next best option would be to simply have the
    process window automatically close on the completion of the process, this
    could be done through the "pause" command, but I'm not sure how or if that
    could be parsed with the process execution to ensure it runs immediately
    following the completion of the cdimage.exe process.

    Thanks for your help dreeschkind, it is very much appreciated.

    -SDL

    "dreeschkind" wrote:

    > I did not try this out, but may be this will work:
    >
    > ###############################################
    > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > $StartInfo.FileName = '..\Tools\cdimage.exe'
    > $StartInfo.Arguments = '-lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > XP Pro - June 2006 Update.iso"'
    > $StartInfo.WorkingDirectory = 'C:\' #fill in the correct directory here
    > $StartInfo.LoadUserProfile = $true
    > $StartInfo.UseShellExecute = $true
    > [System.Diagnostics.Process]::Start($StartInfo)
    > ###############################################
    >
    > Hope that helps.
    >
    > BTW: It might be easier to use only absolute path names and (if feasible)
    > fewer arguments first, so that you can eliminate possible sources of error.
    > This might help to localize the cause of failure.
    >
    > --
    > greetings
    > dreeschkind
    >
    > "SDL" wrote:
    >
    > > Greetings again,
    > >
    > > Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    > > exactly what I was looking for. However, I'm still having issues with the
    > > command string being interpreted incorrectly. For example:
    > >
    > > '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > XP Pro - June 2006 Update.iso"'
    > > Invoke-Expression : You must provide a value expression on the right-hand
    > > side of the '-' operator. At X:\Scripts\Windows XP Image
    > > Generation.ps1:
    > > 44 char:18
    > > + invoke-expression <<<< $IGS_CDImage_Execute
    > >
    > > This points to the '.' in the cdimage.exe part of the string. Simply put,
    > > all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    > > executable, and the rest of the string be passed "raw" and unmodified
    > > directly to the cdimage executable for it to interpret itself. Thanks again
    > > for the help provided.
    > >
    > > -SDL
    > > "Jeffrey Snover [MSFT]" wrote:
    > >
    > > > Jouko has it right.
    > > > & says "use the next token as the NAME of the thing to execute"
    > > >
    > > > --
    > > > Jeffrey Snover [MSFT]
    > > > Windows PowerShell/Aspen Architect
    > > > Microsoft Corporation
    > > > This posting is provided "AS IS" with no warranties, no confers rights.
    > > > Visit the Windows PowerShell Team blog at:
    > > > http://blogs.msdn.com/PowerShell
    > > > Visit the Windows PowerShell ScriptCenter at:
    > > > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    > > >
    > > >
    > > >


      My System SpecsSystem Spec

  7. #7


    =?Utf-8?B?U0RM?= Guest

    Re: Finer control over variable execution

    On another thought, if Option 1 is not possible or too difficult, and Option
    2 could be used, a nice use for the PowerShell window would be to
    consistently poll the cdimage process and display it's CPU and MEM Usage
    every few seconds. I'd be pretty sure that would be possible through the
    get-process cmdlet, might start looking at that in the meantime as well

    -SDL

    "SDL" wrote:

    > Hello again dreeschkind
    >
    > Your method does indeed work, there are no longer any problems with execution!
    >
    > However, I have one last minor issue, purely aesthetic, but using this
    > method the process is launched in a new window (cmd.exe) and instantly closes
    > upon completion. I guess there are two possibilities here:
    >
    > 1. My first preference would be a method to simply run the process inside
    > the PowerShell window(process), or failing that, background the cdimage.exe
    > process but redirect all of it's output to the PowerShell process, which is
    > almost just as good. I took a look at the relevant references in the MSDN
    > library:
    > http://msdn2.microsoft.com/en-us/lib...roperties.aspx.
    > Would the Redirect* objects be of use here? Sorry, but I'm not a programmer
    >
    > 2. Failing that, I guess the next best option would be to simply have the
    > process window automatically close on the completion of the process, this
    > could be done through the "pause" command, but I'm not sure how or if that
    > could be parsed with the process execution to ensure it runs immediately
    > following the completion of the cdimage.exe process.
    >
    > Thanks for your help dreeschkind, it is very much appreciated.
    >
    > -SDL
    >
    > "dreeschkind" wrote:
    >
    > > I did not try this out, but may be this will work:
    > >
    > > ###############################################
    > > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > > $StartInfo.FileName = '..\Tools\cdimage.exe'
    > > $StartInfo.Arguments = '-lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > XP Pro - June 2006 Update.iso"'
    > > $StartInfo.WorkingDirectory = 'C:\' #fill in the correct directory here
    > > $StartInfo.LoadUserProfile = $true
    > > $StartInfo.UseShellExecute = $true
    > > [System.Diagnostics.Process]::Start($StartInfo)
    > > ###############################################
    > >
    > > Hope that helps.
    > >
    > > BTW: It might be easier to use only absolute path names and (if feasible)
    > > fewer arguments first, so that you can eliminate possible sources of error.
    > > This might help to localize the cause of failure.
    > >
    > > --
    > > greetings
    > > dreeschkind
    > >
    > > "SDL" wrote:
    > >
    > > > Greetings again,
    > > >
    > > > Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    > > > exactly what I was looking for. However, I'm still having issues with the
    > > > command string being interpreted incorrectly. For example:
    > > >
    > > > '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > > XP Pro - June 2006 Update.iso"'
    > > > Invoke-Expression : You must provide a value expression on the right-hand
    > > > side of the '-' operator. At X:\Scripts\Windows XP Image
    > > > Generation.ps1:
    > > > 44 char:18
    > > > + invoke-expression <<<< $IGS_CDImage_Execute
    > > >
    > > > This points to the '.' in the cdimage.exe part of the string. Simply put,
    > > > all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    > > > executable, and the rest of the string be passed "raw" and unmodified
    > > > directly to the cdimage executable for it to interpret itself. Thanks again
    > > > for the help provided.
    > > >
    > > > -SDL
    > > > "Jeffrey Snover [MSFT]" wrote:
    > > >
    > > > > Jouko has it right.
    > > > > & says "use the next token as the NAME of the thing to execute"
    > > > >
    > > > > --
    > > > > Jeffrey Snover [MSFT]
    > > > > Windows PowerShell/Aspen Architect
    > > > > Microsoft Corporation
    > > > > This posting is provided "AS IS" with no warranties, no confers rights.
    > > > > Visit the Windows PowerShell Team blog at:
    > > > > http://blogs.msdn.com/PowerShell
    > > > > Visit the Windows PowerShell ScriptCenter at:
    > > > > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    > > > >
    > > > >
    > > > >


      My System SpecsSystem Spec

  8. #8


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    Re: Finer control over variable execution

    "SDL" wrote:

    > Hello again dreeschkind
    >
    > Your method does indeed work, there are no longer any problems with execution!
    >
    > However, I have one last minor issue, purely aesthetic, but using this
    > method the process is launched in a new window (cmd.exe) and instantly closes
    > upon completion. I guess there are two possibilities here:
    >
    > 1. My first preference would be a method to simply run the process inside
    > the PowerShell window(process), or failing that, background the cdimage.exe
    > process but redirect all of it's output to the PowerShell process, which is
    > almost just as good. I took a look at the relevant references in the MSDN
    > library:
    > http://msdn2.microsoft.com/en-us/lib...roperties.aspx.
    > Would the Redirect* objects be of use here? Sorry, but I'm not a programmer


    Setting the property UseShellExecute to $false instead of $true before
    starting your app should make it run inside the PowerShell window.

    <...>
    $StartInfo.UseShellExecute = $false
    [System.Diagnostics.Process]::Start($StartInfo)


    > 2. Failing that, I guess the next best option would be to simply have the
    > process window automatically close on the completion of the process, this
    > could be done through the "pause" command, but I'm not sure how or if that
    > could be parsed with the process execution to ensure it runs immediately
    > following the completion of the cdimage.exe process.


    You can use this to wait for a specific process to finish:

    #'cdimage' is the name as it appears in get-process (fill in the actual name
    here)
    $processToWatch = get-process cdimage
    $processToWatch.WaitForExit()

    Concerning your second post, you could monitor your cdimage.exe process
    using something like this:

    ###############################################
    $processToWatch = get-process cdimage
    $oldPos = $host.UI.RawUI.CursorPosition
    while ($processToWatch.HasExited -eq $false) {
    $host.UI.RawUI.CursorPosition = $oldPos
    $processToWatch | Format-Table Name, Id, CPU, VM
    Start-Sleep 3
    }
    ###############################################

    --
    greetings
    dreeschkind

    > Thanks for your help dreeschkind, it is very much appreciated.
    >
    > -SDL
    >
    > "dreeschkind" wrote:
    >
    > > I did not try this out, but may be this will work:
    > >
    > > ###############################################
    > > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > > $StartInfo.FileName = '..\Tools\cdimage.exe'
    > > $StartInfo.Arguments = '-lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > XP Pro - June 2006 Update.iso"'
    > > $StartInfo.WorkingDirectory = 'C:\' #fill in the correct directory here
    > > $StartInfo.LoadUserProfile = $true
    > > $StartInfo.UseShellExecute = $true
    > > [System.Diagnostics.Process]::Start($StartInfo)
    > > ###############################################
    > >
    > > Hope that helps.
    > >
    > > BTW: It might be easier to use only absolute path names and (if feasible)
    > > fewer arguments first, so that you can eliminate possible sources of error.
    > > This might help to localize the cause of failure.
    > >
    > > --
    > > greetings
    > > dreeschkind
    > >
    > > "SDL" wrote:
    > >
    > > > Greetings again,
    > > >
    > > > Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    > > > exactly what I was looking for. However, I'm still having issues with the
    > > > command string being interpreted incorrectly. For example:
    > > >
    > > > '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > > XP Pro - June 2006 Update.iso"'
    > > > Invoke-Expression : You must provide a value expression on the right-hand
    > > > side of the '-' operator. At X:\Scripts\Windows XP Image
    > > > Generation.ps1:
    > > > 44 char:18
    > > > + invoke-expression <<<< $IGS_CDImage_Execute
    > > >
    > > > This points to the '.' in the cdimage.exe part of the string. Simply put,
    > > > all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    > > > executable, and the rest of the string be passed "raw" and unmodified
    > > > directly to the cdimage executable for it to interpret itself. Thanks again
    > > > for the help provided.
    > > >
    > > > -SDL
    > > > "Jeffrey Snover [MSFT]" wrote:
    > > >
    > > > > Jouko has it right.
    > > > > & says "use the next token as the NAME of the thing to execute"
    > > > >
    > > > > --
    > > > > Jeffrey Snover [MSFT]
    > > > > Windows PowerShell/Aspen Architect
    > > > > Microsoft Corporation
    > > > > This posting is provided "AS IS" with no warranties, no confers rights.
    > > > > Visit the Windows PowerShell Team blog at:
    > > > > http://blogs.msdn.com/PowerShell
    > > > > Visit the Windows PowerShell ScriptCenter at:
    > > > > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    > > > >
    > > > >
    > > > >


      My System SpecsSystem Spec

  9. #9


    =?Utf-8?B?U0RM?= Guest

    Re: Finer control over variable execution

    Greetings and thanks for all of your help dreeschkind

    Your advice on the two methods of execution worked perfectly on both counts,
    and the script is 99% complete. However, I am having what I think is a fairly
    odd problem with your Process polling sample script. When run inside the main
    script with everything else, the following error appears and the script
    terminates (although the cdimage process continues to run in a seperate
    window):
    out-lineoutput : Object of type
    "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not legal
    or not in the correct sequence. This is likely caused by a user specified
    "format-table" command which is conflicting with the default formatting.

    However, if I copy the _exact_ 6 lines of code into a seperate script and
    manually execute said script while the cdimage process is running, it works
    fine :/

    Any thoughts?

    -SDL
    "dreeschkind" wrote:

    > "SDL" wrote:
    >
    > > Hello again dreeschkind
    > >
    > > Your method does indeed work, there are no longer any problems with execution!
    > >
    > > However, I have one last minor issue, purely aesthetic, but using this
    > > method the process is launched in a new window (cmd.exe) and instantly closes
    > > upon completion. I guess there are two possibilities here:
    > >
    > > 1. My first preference would be a method to simply run the process inside
    > > the PowerShell window(process), or failing that, background the cdimage.exe
    > > process but redirect all of it's output to the PowerShell process, which is
    > > almost just as good. I took a look at the relevant references in the MSDN
    > > library:
    > > http://msdn2.microsoft.com/en-us/lib...roperties.aspx.
    > > Would the Redirect* objects be of use here? Sorry, but I'm not a programmer

    >
    > Setting the property UseShellExecute to $false instead of $true before
    > starting your app should make it run inside the PowerShell window.
    >
    > <...>
    > $StartInfo.UseShellExecute = $false
    > [System.Diagnostics.Process]::Start($StartInfo)
    >
    >
    > > 2. Failing that, I guess the next best option would be to simply have the
    > > process window automatically close on the completion of the process, this
    > > could be done through the "pause" command, but I'm not sure how or if that
    > > could be parsed with the process execution to ensure it runs immediately
    > > following the completion of the cdimage.exe process.

    >
    > You can use this to wait for a specific process to finish:
    >
    > #'cdimage' is the name as it appears in get-process (fill in the actual name
    > here)
    > $processToWatch = get-process cdimage
    > $processToWatch.WaitForExit()
    >
    > Concerning your second post, you could monitor your cdimage.exe process
    > using something like this:
    >
    > ###############################################
    > $processToWatch = get-process cdimage
    > $oldPos = $host.UI.RawUI.CursorPosition
    > while ($processToWatch.HasExited -eq $false) {
    > $host.UI.RawUI.CursorPosition = $oldPos
    > $processToWatch | Format-Table Name, Id, CPU, VM
    > Start-Sleep 3
    > }
    > ###############################################
    >
    > --
    > greetings
    > dreeschkind
    >
    > > Thanks for your help dreeschkind, it is very much appreciated.
    > >
    > > -SDL
    > >
    > > "dreeschkind" wrote:
    > >
    > > > I did not try this out, but may be this will work:
    > > >
    > > > ###############################################
    > > > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > > > $StartInfo.FileName = '..\Tools\cdimage.exe'
    > > > $StartInfo.Arguments = '-lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > > XP Pro - June 2006 Update.iso"'
    > > > $StartInfo.WorkingDirectory = 'C:\' #fill in the correct directory here
    > > > $StartInfo.LoadUserProfile = $true
    > > > $StartInfo.UseShellExecute = $true
    > > > [System.Diagnostics.Process]::Start($StartInfo)
    > > > ###############################################
    > > >
    > > > Hope that helps.
    > > >
    > > > BTW: It might be easier to use only absolute path names and (if feasible)
    > > > fewer arguments first, so that you can eliminate possible sources of error.
    > > > This might help to localize the cause of failure.
    > > >
    > > > --
    > > > greetings
    > > > dreeschkind
    > > >
    > > > "SDL" wrote:
    > > >
    > > > > Greetings again,
    > > > >
    > > > > Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    > > > > exactly what I was looking for. However, I'm still having issues with the
    > > > > command string being interpreted incorrectly. For example:
    > > > >
    > > > > '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > > > XP Pro - June 2006 Update.iso"'
    > > > > Invoke-Expression : You must provide a value expression on the right-hand
    > > > > side of the '-' operator. At X:\Scripts\Windows XP Image
    > > > > Generation.ps1:
    > > > > 44 char:18
    > > > > + invoke-expression <<<< $IGS_CDImage_Execute
    > > > >
    > > > > This points to the '.' in the cdimage.exe part of the string. Simply put,
    > > > > all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    > > > > executable, and the rest of the string be passed "raw" and unmodified
    > > > > directly to the cdimage executable for it to interpret itself. Thanks again
    > > > > for the help provided.
    > > > >
    > > > > -SDL
    > > > > "Jeffrey Snover [MSFT]" wrote:
    > > > >
    > > > > > Jouko has it right.
    > > > > > & says "use the next token as the NAME of the thing to execute"
    > > > > >
    > > > > > --
    > > > > > Jeffrey Snover [MSFT]
    > > > > > Windows PowerShell/Aspen Architect
    > > > > > Microsoft Corporation
    > > > > > This posting is provided "AS IS" with no warranties, no confers rights.
    > > > > > Visit the Windows PowerShell Team blog at:
    > > > > > http://blogs.msdn.com/PowerShell
    > > > > > Visit the Windows PowerShell ScriptCenter at:
    > > > > > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    > > > > >
    > > > > >
    > > > > >


      My System SpecsSystem Spec

  10. #10


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    Re: Finer control over variable execution

    "SDL" wrote:

    > Greetings and thanks for all of your help dreeschkind
    >
    > Your advice on the two methods of execution worked perfectly on both counts,
    > and the script is 99% complete. However, I am having what I think is a fairly
    > odd problem with your Process polling sample script. When run inside the main
    > script with everything else, the following error appears and the script
    > terminates (although the cdimage process continues to run in a seperate
    > window):
    > out-lineoutput : Object of type
    > "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not legal
    > or not in the correct sequence. This is likely caused by a user specified
    > "format-table" command which is conflicting with the default formatting.
    >
    > However, if I copy the _exact_ 6 lines of code into a seperate script and
    > manually execute said script while the cdimage process is running, it works
    > fine :/
    >
    > Any thoughts?


    Sorry, I guess this was my fault, I only tested my code in the shell as
    well. ;-)
    I tried to simplify the display of the properties by using the formatting
    capabilities of format-table. Unfortunately, using (different) format-*
    cmdlets inside ps1 script files causes the error you described, because
    switching the formatting within the pipeline is not possible (at the moment).
    Someone from the team might actually explain this a little bit better than me.
    However, I think it should be possible to workaround this issue by using
    your own code for displaying the values you are interested in.
    Try this code and tell me if it works now:

    $processToWatch = get-process cdimage
    $oldPos = $host.UI.RawUI.CursorPosition
    while ($processToWatch.HasExited -eq $false) {
    $host.UI.RawUI.CursorPosition = $oldPos

    #use write-host 3 times to display a table of the properties
    Write-Host "Name`t|`tId`t|`tCPU`t`t|`tVM"
    Write-Host $("-"*70)
    Write-Host
    "$($processToWatch.Name)`t|`t$($processToWatch.Id)`t|`t$($processToWatch.CPU)`t|`t$($processToWatch.VM)"

    Start-Sleep 3
    }

    --
    greetings
    dreeschkind

    > -SDL
    > "dreeschkind" wrote:
    >
    > > "SDL" wrote:
    > >
    > > > Hello again dreeschkind
    > > >
    > > > Your method does indeed work, there are no longer any problems with execution!
    > > >
    > > > However, I have one last minor issue, purely aesthetic, but using this
    > > > method the process is launched in a new window (cmd.exe) and instantly closes
    > > > upon completion. I guess there are two possibilities here:
    > > >
    > > > 1. My first preference would be a method to simply run the process inside
    > > > the PowerShell window(process), or failing that, background the cdimage.exe
    > > > process but redirect all of it's output to the PowerShell process, which is
    > > > almost just as good. I took a look at the relevant references in the MSDN
    > > > library:
    > > > http://msdn2.microsoft.com/en-us/lib...roperties.aspx.
    > > > Would the Redirect* objects be of use here? Sorry, but I'm not a programmer

    > >
    > > Setting the property UseShellExecute to $false instead of $true before
    > > starting your app should make it run inside the PowerShell window.
    > >
    > > <...>
    > > $StartInfo.UseShellExecute = $false
    > > [System.Diagnostics.Process]::Start($StartInfo)
    > >
    > >
    > > > 2. Failing that, I guess the next best option would be to simply have the
    > > > process window automatically close on the completion of the process, this
    > > > could be done through the "pause" command, but I'm not sure how or if that
    > > > could be parsed with the process execution to ensure it runs immediately
    > > > following the completion of the cdimage.exe process.

    > >
    > > You can use this to wait for a specific process to finish:
    > >
    > > #'cdimage' is the name as it appears in get-process (fill in the actual name
    > > here)
    > > $processToWatch = get-process cdimage
    > > $processToWatch.WaitForExit()
    > >
    > > Concerning your second post, you could monitor your cdimage.exe process
    > > using something like this:
    > >
    > > ###############################################
    > > $processToWatch = get-process cdimage
    > > $oldPos = $host.UI.RawUI.CursorPosition
    > > while ($processToWatch.HasExited -eq $false) {
    > > $host.UI.RawUI.CursorPosition = $oldPos
    > > $processToWatch | Format-Table Name, Id, CPU, VM
    > > Start-Sleep 3
    > > }
    > > ###############################################
    > >
    > > --
    > > greetings
    > > dreeschkind
    > >
    > > > Thanks for your help dreeschkind, it is very much appreciated.
    > > >
    > > > -SDL
    > > >
    > > > "dreeschkind" wrote:
    > > >
    > > > > I did not try this out, but may be this will work:
    > > > >
    > > > > ###############################################
    > > > > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > > > > $StartInfo.FileName = '..\Tools\cdimage.exe'
    > > > > $StartInfo.Arguments = '-lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > > > XP Pro - June 2006 Update.iso"'
    > > > > $StartInfo.WorkingDirectory = 'C:\' #fill in the correct directory here
    > > > > $StartInfo.LoadUserProfile = $true
    > > > > $StartInfo.UseShellExecute = $true
    > > > > [System.Diagnostics.Process]::Start($StartInfo)
    > > > > ###############################################
    > > > >
    > > > > Hope that helps.
    > > > >
    > > > > BTW: It might be easier to use only absolute path names and (if feasible)
    > > > > fewer arguments first, so that you can eliminate possible sources of error.
    > > > > This might help to localize the cause of failure.
    > > > >
    > > > > --
    > > > > greetings
    > > > > dreeschkind
    > > > >
    > > > > "SDL" wrote:
    > > > >
    > > > > > Greetings again,
    > > > > >
    > > > > > Thanks to both of you for the help, the 'invoke-expression' cmdlet is
    > > > > > exactly what I was looking for. However, I'm still having issues with the
    > > > > > command string being interpreted incorrectly. For example:
    > > > > >
    > > > > > '"..\Tools\cdimage.exe" -lWXP_PRO_JUN06 -t08/26/2006/20/41/15
    > > > > > -b"..\src\bootsector.bin" -p0 -e -g -h -j1 -x -o -w1 -m "..\src" "C:\\Windows
    > > > > > XP Pro - June 2006 Update.iso"'
    > > > > > Invoke-Expression : You must provide a value expression on the right-hand
    > > > > > side of the '-' operator. At X:\Scripts\Windows XP Image
    > > > > > Generation.ps1:
    > > > > > 44 char:18
    > > > > > + invoke-expression <<<< $IGS_CDImage_Execute
    > > > > >
    > > > > > This points to the '.' in the cdimage.exe part of the string. Simply put,
    > > > > > all I want is the "..\Tools\cdimage.exe" component to execute the cdimage
    > > > > > executable, and the rest of the string be passed "raw" and unmodified
    > > > > > directly to the cdimage executable for it to interpret itself. Thanks again
    > > > > > for the help provided.
    > > > > >
    > > > > > -SDL
    > > > > > "Jeffrey Snover [MSFT]" wrote:
    > > > > >
    > > > > > > Jouko has it right.
    > > > > > > & says "use the next token as the NAME of the thing to execute"
    > > > > > >
    > > > > > > --
    > > > > > > Jeffrey Snover [MSFT]
    > > > > > > Windows PowerShell/Aspen Architect
    > > > > > > Microsoft Corporation
    > > > > > > This posting is provided "AS IS" with no warranties, no confers rights.
    > > > > > > Visit the Windows PowerShell Team blog at:
    > > > > > > http://blogs.msdn.com/PowerShell
    > > > > > > Visit the Windows PowerShell ScriptCenter at:
    > > > > > > http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    > > > > > >
    > > > > > >
    > > > > > >


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast

Finer control over variable execution
Similar Threads
Thread Forum
Data Execution Vista General
Optimize Execution Vista General
Execution problem PowerShell
How to setup variable for directory execution PowerShell
How can I ensure that a variable is a built-in powershell variable? PowerShell