Baj*_*jan 4 windows powershell
我正在尝试编写执行以下操作的powershell脚本:
我已搜索但无法找到解决我看似简单问题的方法,请参阅下面的代码:
$computers=Get-Content C:\pcs.txt
$source="\\RemoteShare\RemoteFolder"
$dest="C$\Program Files\Destination"
foreach ($computer in $computers) {
If (Test-Path \\$computer\$dest){
Remove-Item \\$computer\$dest -Force -Recurse
}
Copy-Item $source \\$computer\$dest -recurse -force -erroraction silentlycontinue
If (!$error)
{Write-Output $computer | out-file -append -filepath "C:\logs\success.log"}
Else
{Write-Output $computer | out-file -append -filepath "C:\logs\failed.log"}
}
Run Code Online (Sandbox Code Playgroud)
目前,当脚本运行时,无论是否失败,都会将所有内容放入failed.log文件中.
在运行for循环时,如何正确处理PowerShell中的错误?
小智 5
这是一个例子.
$array = @(3,0,1,2)
foreach ($item in $array)
{
try
{
1/$item | Out-Null
Write-Host "$item is okay"
}
catch
{
Write-Host "There was an error! Can't divide by $item!"
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
40995 次 |
最近记录: |