我在这里有这个脚本:
Write-Host "Checking files"
#to make it more dynamical you can save on one file
#all the file names including extension in different lines.
#For example on path C:\FilesToWatch\watcher.txt
#$filestowatch=get-content C:\FilesToWatch\watcher.txt
#$filestowatch="felicio.txt","marcos.txt"
$userFiles=dir C:\G\user\less\
$adminfiles=dir C:\G\admin\less\
#Optionally instead of use this if approach you can
#$adminfiles=dir C:\GT\admin\src\admin\wwwroot\content\less|? {$filestowatch -contains $_.Name}
#$userFiles=dir C:\GT\user-staging\src\user-staging\wwwroot\content\less|? {$filestowatch -contains $_.Name}
#loading in the above manner the first if statement on code bellow can be removed because
#We make sure that $userFiles and $adminfiles only have correct file to monitor
foreach($userfile in $userFiles)
{
if($filestowatch -contains $userfile.Name)
{
$exactadminfile= $adminfiles | ? {$_.Name -eq $userfile.Name} |Select -First 1
#my suggestion is to validate if it got the file.
#By now because of my lazy i will not call the test-path to validate if it got the file
#I'm assuming all directory are exact copy of each other so it will find the file.
if($exactadminfile.LastWriteTime -gt $userfile.LastWriteTime)
{
Write-Verbose "Copying $exactadminfile.FullName to $userfile.FullName "
Copy-Item -Path $exactadminfile.FullName -Destination $userfile.FullName -Force
}
else
{
Write-Verbose "Copying $userfile.FullName to $exactadminfile.FullName "
Copy-Item -Path $userfile.FullName -Destination $exactadminfile.FullName -Force
}
}
}
我知道脚本可能有问题但是当它运行时我甚至看不到第一行的输出。谁能给我一些关于我如何调试它的想法?
以下是我如何运行(或尝试运行脚本):
PS C:\GT> .\watcher.ps1
当我尝试仅使用第一行的脚本时,它才能正常工作。有没有办法可以在调试模式下运行这个脚本,这样可以让我知道可能出现什么问题?
答案 0 :(得分:1)
您应该在ISE中打开此脚本,以便可以使用调试器。选择要在其处设置断点的行,然后按F9进行设置。继续运行脚本,一旦到达该行,它将进入调试器。从那里,您可以开始检查您的变量并逐步完成代码,以便找到问题。
一些ISE调试器快捷方式:
有关调试的更多信息,请参阅帮助文件。
Get-Help about_debuggers