在Do-While循环中执行,在几次尝试后无法显示结果

时间:2013-07-11 16:11:16

标签: powershell

我正在尝试在powershell脚本中创建一个类似CUI的应用程序。例如,显示大约7个选项 - 每个选项映射到数字字符。显示提示,管理员可以键入相应的数字字母以选择选项。

我正在使用Do-While循环和Switch语句来完成此任务。对于每个选项,我已经使用Exe命令和错误捕获机制创建了一个函数。

这个脚本可以解决任何问题,但问题是我在函数内部使用的可执行文件只显示结果5或10次,没有显示结果,甚至没有错误。即使在从脚本终止之后,同一控制台中的所有exe文件也会继续这种情况。

如果我关闭powershell控制台并打开一个新的,可执行文件可以正常工作5到10次,然后重新开始。我无法确定导致此问题的原因。

#---------------------------------------------------------------------------
#As requested i am posting the 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
{

Do
{
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 any 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 any 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 any 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 any 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 to the Target server " + "`"$TSN`"" + ". Please Wait... ")

Write-Host ""
if(Test-Connection -ComputerName $TSN -Quiet)
{
Write-Host ""
Write-host ("Success : Target Server " + "`"$TSN`"" + " is Valid")  -ForegroundColor "Green"
Write-Host ""
Write-host "Press 'Y' to 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 to connect to Target Server " + "`"$TSN`"" + ". Test connection Failed")  -ForegroundColor "Red"
Write-Host ""
$Script:MMPreviousStatus = "Error"
}
}
Else
{
Write-Host ""
Write-host ("Error : Target Server Name " + "`"$TSN`"" + " is not a 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`"" + " is Valid.")  -ForegroundColor "Green"
Write-host ("Note    : " + "`"$Vf`"" + " will be the virtual folder for DFS links to be created.")  -ForegroundColor "Green"
Write-Host ""
Write-host "Press 'Y' to 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 an incorrect format")  -ForegroundColor "Red"
Write-Host ""
$Script:MMPreviousStatus = "Error"
}
}
Else
{
Write-Host ""
Write-host ("Error : Length of the 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' to 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' to 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"
Do
{
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 any 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 any 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 any 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 any 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 any 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 to Identify Local Domain"
Write-host ""

}
}

Start-Application

1 个答案:

答案 0 :(得分:0)

我在Win7机器上试过你的脚本。我没有DFSCMD.EXE存在,所以每次脚本执行它时我都会收到错误。除此之外,脚本运行了30多次,没有任何麻烦。

我建议注释掉DFSCMD.EXE的调用表达式,看看它是如何工作的。如果它一致地工作,我会将其添加回来,然后在进行5-10次调用时观察Powershell和任务管理器中的整体系统性能。在Powershell下,我会寻找内存使用,句柄和线程。我预计Powershell会增加一些内存使用量,但不会很大,如果PS闲置,它应该会回落。此外,DFSCMD.EXE的每个实例在退出后都应从taskmgr中消失。

您还可以使用Process Explorer并深入了解。例如,procexp将让您查看进程的线程堆栈。一旦你进入“不工作状态”,你可以通过查看PS的线程来获得关于发生了什么的提示。您需要加载符号以使procexp在堆栈上显示符号。我认为procexp的帮助告诉你如何做到这一点。

除此之外,我将打破一个调试器......让好时光滚动