【发布时间】:2019-06-25 19:06:36
【问题描述】:
我是 PowerShell 新手。我有一段代码可以检查文件夹“ilalog”是否存在。当我第一次运行这个脚本时,它正在检查文件夹“ilalog”,如果它不存在,它正在创建。当我第二次运行脚本时。我收到以下错误:
具有指定名称 D:\Temp\ilalog 的项目已存在 “完全限定错误 ID: DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand”。
如何处理这个异常
我尝试过使用 try 和 Catch 块
$rgflder="ilalog"
[bool]$checkrg=Test-Path D:\Gdump\$rgfolder -PathType Any
if ( $checkrg -eq $False)
{
try{
New-Item -Path "D:\Gdump" -Name "$rgflder" -ItemType "directory"
}
catch [System.IO.IOException]
{
if ($_.CategoryInfo.Category -eq $resExistErr) {Write-host "Dir Exist"}
}
}
else
{
Write-Output "Directory Exists"
}
【问题讨论】:
标签: powershell exception