powershell - Execution within Do-While loops, fails to show results after few attempts -


i trying create cui application in powershell script. example, showing around 7 options - each mapped numeral character. prompt presented, , admin can type respective numeral letter choose option.

i using do-while loop , switch statement accomplish this. each option, i've made function exe command , error trapping mechanism.

this script working fine out issue, problem executables use inside functions showing results 5 or 10 times beore no results shown, not error. condition continues exe files in same console after terminating script.

if close powershell console , open new one, executables work fine 5 10 times , issue starts again. not able identify causing issue.

#--------------------------------------------------------------------------- #as requested posting entire script #--------------------------------------------------------------------------  function show-headermessage { write-host ""  write-host "" write-host (" "*10 + (" ___" + ("_" * $script:padright)          + " ")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + (" ").padright($script:padright)  + "!")) -foregroundcolor "yellow"  write-host (" "*10 + ("!   "                     )) -foregroundcolor "yellow" -nonewline write-host (("gmol - new dfs migration tool (s drive)" ).padright($script:padright)   ) -foregroundcolor "green"  -nonewline write-host (                             "!") -foregroundcolor "yellow"  write-host (" "*10 + ("!   "                   )) -foregroundcolor "yellow" -nonewline write-host (("---------------------------------------" ).padright($script:padright)       ) -foregroundcolor "green"  -nonewline write-host (                   "!") -foregroundcolor "yellow"  write-host (" "*10 + ("!   " + (" ").padright($script:padright)    + "!")) -foregroundcolor "yellow"  write-host (" "*10 + ("!   "                     )) -foregroundcolor "yellow" -nonewline write-host (("version : 1.0"              ).padright($script:padright)        ) -foregroundcolor "green"  -nonewline write-host (                    "!") -foregroundcolor "yellow"  write-host (" "*10 + ("!   "                )) -foregroundcolor "yellow" -nonewline write-host (("date    : $script:strmdate" ).padright($script:padright)     ) -foregroundcolor "green"  -nonewline write-host (                         "!") -foregroundcolor "yellow"  write-host (" "*10 + ("!___" + ("_" * $script:padright)        + "!")) -foregroundcolor "yellow" write-host (" "*10 + (" ___" + ("_" * $script:padright)          + " ")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + (" ").padright($script:padright)        + "!")) -foregroundcolor "yellow" #return $null }  function show-footermessage { write-host (" "*10 + ("!   " + (" ").padright($script:padright)         + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!___" + ("_" * $script:padright)          + "!")) -foregroundcolor "yellow" write-host "" write-host (" "*10 + ("ao_id").padright(20) + " : " + $script:strmadmin          ) -foregroundcolor "yellow" write-host (" "*10 + ("domain name").padright(20) + " : " + $script:strmdomain     ) -foregroundcolor "yellow" write-host (" "*10 + ("dfs root").padright(20) + " : " + $script:dfsroot     ) -foregroundcolor "yellow" write-host (" "*10 + ("dfs virtual folder").padright(20) + " : " + $script:dfsvf      ) -foregroundcolor "yellow" write-host (" "*10 + ("target server").padright(20) + " : " + $script:targetserver      ) -foregroundcolor "yellow" write-host (" "*10 + ("sddl file location").padright(20) + " : " + $script:sddlfilefinalpath  ) -foregroundcolor "yellow" write-host (" "*10 + ("csv  file path").padright(20) + " : " + $script:csvfilefinalpath    ) -foregroundcolor "yellow" write-host (" "*10 + ("____" + ("_" * $script:padright)    + " ")) -foregroundcolor "yellow" write-host "" #return $null }  function show-mainmenu { clear-host show-headermessage write-host (" "*10 + ("!   " + ("main menu"   ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + ("---------"  ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + (" "    ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + ("t. test dfsroot shared_data"  ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + ("v. view dfsroot shared_data" ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + (" "      ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + ("1. set dfs virtual-folder name" ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + ("2. set target server name"   ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   "                                                                               )) -foregroundcolor "yellow" -nonewline write-host (("3. csv file options"  ).padright($script:padright-12)    + "[sub menu]  ") -foregroundcolor "cyan" -nonewline write-host (                                                                                             "!") -foregroundcolor "yellow"  write-host (" "*10 + ("!   "               )) -foregroundcolor "yellow" -nonewline write-host (("4. create dfslinks"   ).padright($script:padright-12)   + "[sub menu]  ") -foregroundcolor "cyan" -nonewline write-host (                                                                                             "!") -foregroundcolor "yellow"  write-host (" "*10 + ("!   "              )) -foregroundcolor "yellow"  -nonewline write-host (("5. configure dfslink permissions" ).padright($script:padright-12)   + "[sub menu]  ") -foregroundcolor "cyan"  -nonewline write-host (                                                                                             "!") -foregroundcolor "yellow"  #write-host (" "*10 + ("!   " + ("7. view csv file in html"  ).padright($script:padright)  + "!")) -foregroundcolor $color  write-host (" "*10 + ("!   " + (" "       ).padright($script:padright)  + "!")) -foregroundcolor "yellow" write-host (" "*10 + ("!   " + ("q. quit application"  ).padright($script:padright)  + "!")) -foregroundcolor "yellow"  show-footermessage write-host (" "*10 + "previous option      : " + $script:mmpreviousoption) write-host (" "*10 + "status               : " + $script:mmpreviousstatus) write-host "" write-host (" "*10 + "select option   : ") -foregroundcolor "yellow" -nonewline }   function confirm-option { $xkey = $host.ui.rawui.readkey("noecho,includekeydown") $option = [char]([int]$xkey.character) return $option }  function show-preparecsvthreadheader { param ( $headeroption )  write-host ("_____________________________________________________________") -foregroundcolor "yellow" write-host "" write-host (" "*5 + "prepare csv input file"                               ) -foregroundcolor "yellow" write-host (" "*5 + "----------------------"                               ) -foregroundcolor "yellow" write-host (" "*5 + $headeroption                                          ) write-host ("_____________________________________________________________") -foregroundcolor "yellow" write-host "" }  function start-preparecsvthread {  { show-preparecsvmenu $script:pcmchar = read-host switch($script:pcmchar) { 'v' { clear-host show-preparecsvthreadheader "option v : view target server share folder details" $script:pcmpreviousoption = "v. view target server share folder details" do_viewsharefolders write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 1 { clear-host show-preparecsvthreadheader "option 1 : create csv input file" $script:pcmpreviousoption = "1. create csv input file" do_createcsvfile write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 2 { clear-host show-preparecsvthreadheader "option 2 : set csv file path" $script:pcmpreviousoption = "2. set csv file path" do_setcsvfile write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 3 { clear-host show-preparecsvthreadheader "option 3 : view csv in html" $script:pcmpreviousoption = "3. view csv in html" do_viewcsvfile write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } default { if($script:pcmchar -ne 'q') { write-host "" write-host ("error : invalid option")  -foregroundcolor "red" write-host "" } } } }while(($script:pcmchar -ne 'q') -or ($script:pcmchar -ne 'q')) }  function do_settargetservername { param ( $headeroption ) write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + $headeroption                                        )  -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" if($script:dfsroot -ne "") { write-host "" $tsn = read-host -prompt "enter target server name " write-host ""  if($tsn -match "^[a-z]+[\w]+[a-z0-9]+$") { write-host "" write-host ("testing connection target server " + "`"$tsn`"" + ". please wait... ")  write-host "" if(test-connection -computername $tsn -quiet) { write-host "" write-host ("success : target server " + "`"$tsn`"" + " valid")  -foregroundcolor "green" write-host "" write-host "press 'y' set target server name..." -nonewline  if((confirm-option) -eq 'y') { $script:targetserver = $tsn if(create_folder ("c:\hamtramck\sddl_files\" + $script:targetserver)) { $script:sddlfilefinalpath = "c:\hamtramck\sddl_files\" + $script:targetserver write-host "" write-host "" write-host ("target server     : $script:targetserver")  -foregroundcolor "green" write-host ("sdl file location : $script:sddlfilefinalpath")  -foregroundcolor "green" write-host "" } else { write-host "" write-host "" write-host ("target server     : $script:targetserver")  -foregroundcolor "green" write-host ("sdl file location : c:\hamtramck\sddl_files\")  -foregroundcolor "green" write-host "" } write-host "" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + "set target server name - operation completed")           -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" $script:mmpreviousstatus = "completed" } else { write-host "_____________________________________________________________"  -foregroundcolor "red" write-host ""  write-host (" "*5 + "set target server name - operation cancelled")           -foregroundcolor "red" write-host "_____________________________________________________________"  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "cancelled" } } else { write-host "" write-host ("failed : unable connect target server " + "`"$tsn`"" + ". test connection failed")  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "error" } } else { write-host "" write-host ("error : target server name " + "`"$tsn`"" + " not valid format")  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "error" } }  }  function do_setdfsvirtualfolder { param ( $headeroption ) write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + $headeroption                                        )  -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" if($script:dfsroot -ne "") { write-host "" $vf = read-host -prompt "enter next dfs virtual folder name " write-host "" if($vf.length -eq 9) { if($vf -match "^group+[0-9]{4}$") { write-host "" write-host ("success : dfs virtual folder name " + "`"$vf`"" + " valid.")  -foregroundcolor "green" write-host ("note    : " + "`"$vf`"" + " virtual folder dfs links created.")  -foregroundcolor "green" write-host "" write-host "press 'y' set dfs virtual folder..." if((confirm-option) -eq 'y') { $script:dfsvf = $vf write-host "" write-host ("dfs virtual folder: $script:dfsvf")  -foregroundcolor "green" write-host "" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + "set dfs virtual-folder name operation completed")      -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" $script:mmpreviousstatus = "completed" } else { write-host "_____________________________________________________________"  -foregroundcolor "red" write-host ""  write-host (" "*5 + "set dfs virtual-folder name operation cancelled")      -foregroundcolor "red" write-host "_____________________________________________________________"  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "cancelled" } } else { write-host "" write-host ("error : dfs virtual folder name " + "`"$vf`"" + " have incorrect format")  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "error" } } else { write-host "" write-host ("error : length of dfs virtual folder name " + "`"$vf`"" + "is not correct")  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "error" } }  }  function do_viewdfsfolders { param ( $headeroption ) write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + $headeroption                                        )  -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" if($script:dfsroot -ne "") { write-host "" write-host "dfs root : $script:dfsroot" -foregroundcolor "yellow" write-host "" write-host "press 'y' start dfs enumeration..." if((confirm-option) -eq 'y') { write-host "" write-host "enumerating shared_data dfs root folder structure. please wait... " write-host "" $m1optvcmdstring1 = "c:\windows\system32\dfscmd.exe" $m1optvcmdstring2 = " /view " +$script:dfsroot $m1optvcmdstring =  $m1optvcmdstring1 + $m1optvcmdstring2 write-host $m1optvcmdstring write-host ""  invoke-expression "cmd.exe /c `"`"$m1optvcmdstring 2>&1`"`""  #dfscmd.exe /view $script:dfsroot | sort-object   write-host "_____________________________________________________________"    -foregroundcolor "yellow" write-host "" write-host (" "*5 + "view dfsroot 'shared_data' - operation completed")       -foregroundcolor "yellow" write-host "_____________________________________________________________"    -foregroundcolor "yellow" write-host "" $script:mmpreviousstatus = "completed" } else { write-host "_____________________________________________________________"   -foregroundcolor "red" write-host ""  write-host (" "*5 + "view dfsroot 'shared_data' - operation cancelled")      -foregroundcolor "red" write-host "_____________________________________________________________"   -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "cancelled" } } else { write-host "" write-host ("error : dfs root folder not set")  -foregroundcolor "red" write-host "" write-host "" $script:mmpreviousstatus = "error" } return $null  }  function do_testdfsroot { param ( $headeroption ) write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + $headeroption                                        )  -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" if($script:dfsroot -ne "") { write-host "" write-host "dfs root : $script:dfsroot" -foregroundcolor "yellow" write-host "" write-host "press 'y' begin testing..." if((confirm-option) -eq 'y') { write-host "" write-host "initiating dfsroot testing. please wait... " write-host "" $m1opttcmdstring1 = "c:\windows\system32\dfsdiag.exe /testdfsconfig" $m1opttcmdstring2 = " /dfsroot:" + $script:dfsroot $m1opttcmdstring =  $m1opttcmdstring1 + $m1opttcmdstring2  invoke-expression $m1opttcmdstring  write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" write-host (" "*5 + "testing dfsroot 'shared_root' - operation completed")  -foregroundcolor "yellow" write-host "_____________________________________________________________"  -foregroundcolor "yellow" write-host "" $script:mmpreviousstatus = "completed" } else { write-host "_____________________________________________________________"  -foregroundcolor "red" write-host "" write-host (" "*5 + "testing dfsroot 'shared_root' - operation cancelled")  -foregroundcolor "red" write-host "_____________________________________________________________"  -foregroundcolor "red" write-host "" $script:mmpreviousstatus = "cancelled" } } else { write-host "" write-host ("error : dfs root folder not set")  -foregroundcolor "red" write-host "" write-host "" $script:mmpreviousstatus = "error" } }   function start-application { $script:strmdate = (get-date).tolongdatestring() + " " + (get-date).tolongtimestring() $script:strmadmin = get-content env:username $script:strmdomain = get-content env:userdnsdomain $script:targetserver = "" $script:dfsvf = "" $script:enteredoption = 0 $script:padright = 70 if($script:strmdomain -ne "") { $script:dfsroot = "\\" + $script:strmdomain + "\" + "shared_data" { show-mainmenu $script:mmchar = read-host switch($script:mmchar) { 't' { clear-host $script:mmpreviousoption = "t. test dfsroot 'shared_data'" do_testdfsroot "main menu :: t. test dfsroot 'shared_data'" write-host "press key..." -nonewline                     $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 'v' { clear-host $script:mmpreviousoption = "v. view dfsroot 'shared_data'" do_viewdfsfolders "main menu :: v. view dfsroot 'shared_data'" write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 1 { clear-host $script:mmpreviousoption = "1. set dfs virtual-folder name" do_setdfsvirtualfolder "main menu :: 1. set dfs virtual-folder name" write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 2 { clear-host $script:mmpreviousoption = "2. set target server name" do_settargetservername "main menu :: 2. set target server name" write-host "press key..." -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } 3 { start-preparecsvthread } 4 { start-createdfslinkthread } 5 { start-aclconfigthread } 'q' { clear-history clear-host } default { if(($script:mmchar -ne 'q') -or ($script:mmchar -ne 'q')) { write-host "" write-host (" "*10 + "error : invalid option. press key...")  -foregroundcolor "red" -nonewline $dummykey = $host.ui.rawui.readkey("noecho,includekeydown") } } } }while(($script:mmchar -ne 'q') -or ($script:mmchar -ne 'q')) } else { write-host "application failed : unable identify local domain" write-host ""  } }  start-application 

i tried script on win7 machine. don't have dfscmd.exe present got error everytime script executed it. other script ran 30+ times no hint of trouble.

i suggest commenting out invoke expression of dfscmd.exe , see how works then. if works consistently add in, , watch powershell , overall system performance in task manager go through 5-10 invocations. under powershell i'd memory usage, handles, , threads. expect memory usage increase in powershell, not huge, , should creep down if ps left idle. each instance of dfscmd.exe should disappear taskmgr after exit it.

you use process explorer , more in depth. example procexp let @ process's threads' stacks. once "not working state", might able hint what's going on looking through ps's threads. need load symbols procexp show symbols on stack. think procexp's tell how that.

beyond i'd break out debugger ... let times roll


Comments