我想知道如何在$中执行此操作?在powershell中
美元吗?值始终设置为true?在下面的示例中,它被设置为TRUE是因为我编写了我想知道如何在$中执行此操作?在powershell中,powershell,error-handling,Powershell,Error Handling,美元吗?值始终设置为true?在下面的示例中,它被设置为TRUE是因为我编写了silentlycontinue。在此脚本中,我管理文件的错误处理,以从用户输入获取内容:测试文件是否繁忙的脚本在再次访问它之前等待2秒,如中所示: $filecontent = get-content $filename -ea silentlycontinue **while (-not $?)** { $filecontent = get-content $filename -ea sile
silentlycontinue
。在此脚本中,我管理文件的错误处理,以从用户输入获取内容:测试文件是否繁忙的脚本在再次访问它之前等待2秒,如中所示:
$filecontent = get-content $filename -ea silentlycontinue
**while (-not $?)**
{
$filecontent = get-content $filename -ea silentlycontinue
start-sleep: -sec 2
}
美元?包含上次操作的执行状态。它包含
如果上次操作成功,则为TRUE;如果操作失败,则为FALSE
它的值与ErrorActionPreference设置无关:
PS>$ErrorActionPreference="continue"
PS>gc afile.txt
gc : Impossible de trouver le chemin d'accès « C:\Users\u1\afile.txt », car il n'existe pas.
Au caractère Ligne:1 : 1
+ gc afile.txt
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\u1\afile.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
PS>$?
False
PS>$ErrorActionPreference="silentlycontinue"
PS>gc afile.txt #no error message displayed
PS>$?
False
更好的处理方法是通过将$ErrorActionPreference设置为“stop”并使用try-catch语句来终止所有错误
function openFile{
try{
$file=[System.io.File]::Open('c:\windows\windowsupdate.log', 'Open', 'Read', 'None')
}
catch{
write-host "File is locked"
start-sleep 2
openFile
}
finaly{
return $file
}
}
$ErrorActionPreference="stop"
$file=openFile
美元?包含上次操作的执行状态。它包含
如果上次操作成功,则为TRUE;如果操作失败,则为FALSE
它的值与ErrorActionPreference设置无关:
PS>$ErrorActionPreference="continue"
PS>gc afile.txt
gc : Impossible de trouver le chemin d'accès « C:\Users\u1\afile.txt », car il n'existe pas.
Au caractère Ligne:1 : 1
+ gc afile.txt
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\u1\afile.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
PS>$?
False
PS>$ErrorActionPreference="silentlycontinue"
PS>gc afile.txt #no error message displayed
PS>$?
False
更好的处理方法是通过将$ErrorActionPreference设置为“stop”并使用try-catch语句来终止所有错误
function openFile{
try{
$file=[System.io.File]::Open('c:\windows\windowsupdate.log', 'Open', 'Read', 'None')
}
catch{
write-host "File is locked"
start-sleep 2
openFile
}
finaly{
return $file
}
}
$ErrorActionPreference="stop"
$file=openFile
美元?包含上次操作的执行状态。它包含
如果上次操作成功,则为TRUE;如果操作失败,则为FALSE
它的值与ErrorActionPreference设置无关:
PS>$ErrorActionPreference="continue"
PS>gc afile.txt
gc : Impossible de trouver le chemin d'accès « C:\Users\u1\afile.txt », car il n'existe pas.
Au caractère Ligne:1 : 1
+ gc afile.txt
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\u1\afile.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
PS>$?
False
PS>$ErrorActionPreference="silentlycontinue"
PS>gc afile.txt #no error message displayed
PS>$?
False
更好的处理方法是通过将$ErrorActionPreference设置为“stop”并使用try-catch语句来终止所有错误
function openFile{
try{
$file=[System.io.File]::Open('c:\windows\windowsupdate.log', 'Open', 'Read', 'None')
}
catch{
write-host "File is locked"
start-sleep 2
openFile
}
finaly{
return $file
}
}
$ErrorActionPreference="stop"
$file=openFile
美元?包含上次操作的执行状态。它包含
如果上次操作成功,则为TRUE;如果操作失败,则为FALSE
它的值与ErrorActionPreference设置无关:
PS>$ErrorActionPreference="continue"
PS>gc afile.txt
gc : Impossible de trouver le chemin d'accès « C:\Users\u1\afile.txt », car il n'existe pas.
Au caractère Ligne:1 : 1
+ gc afile.txt
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\u1\afile.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
PS>$?
False
PS>$ErrorActionPreference="silentlycontinue"
PS>gc afile.txt #no error message displayed
PS>$?
False
更好的处理方法是通过将$ErrorActionPreference设置为“stop”并使用try-catch语句来终止所有错误
function openFile{
try{
$file=[System.io.File]::Open('c:\windows\windowsupdate.log', 'Open', 'Read', 'None')
}
catch{
write-host "File is locked"
start-sleep 2
openFile
}
finaly{
return $file
}
}
$ErrorActionPreference="stop"
$file=openFile