0

我正在尝试在 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
4

1 回答 1

0

我在我的 Win7 机器上试过你的脚本。我没有 DFSCMD.EXE,所以每次脚本执行时都会出错。除此之外,该脚本运行了 30 多次,没有任何问题。

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

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

除此之外,我会打破一个调试器......让美好时光滚滚而来

于 2013-07-12T01:42:56.700 回答