一次捕获多个错误 try/catch
Catching multiple errors in one try/catch
因此尝试做一个简单的 try
/catch
在 PowerShell 中更新服务器上文件夹的共享访问权限。
try {
Grant-SmbShareAccess -Name [FolderName] -AccountName [GroupToShare] -AccessRight Read -Force
Grant-SmbShareAccess -Name [FolderName] -AccountName [GroupToShare] -AccessRight Read -Force
} catch {
Write-Host "Error Granting one or more permission: $_" -ForegroundColor DarkMagenta
}
是否可以捕获特定的授权访问语句并打印失败的组名。
假设我有两组:
- NA\admin
- NA\dev
并且 Grant-SmbShareAccess
两个组都失败了,我可以捕捉并打印出来吗:
Permission Failed for: NA\admin
Permission Failed for: NA\dev
只使用一次 try catch?
遍历参数:
foreach($group in 'NA\admin','NA\dev'){
try {
Grant-SmbShareAccess -Name [FolderName] -AccountName $group -AccessRight Read -Force
}
catch {
Write-Host "Failed to grant permission to: $group"
}
}
因此尝试做一个简单的 try
/catch
在 PowerShell 中更新服务器上文件夹的共享访问权限。
try {
Grant-SmbShareAccess -Name [FolderName] -AccountName [GroupToShare] -AccessRight Read -Force
Grant-SmbShareAccess -Name [FolderName] -AccountName [GroupToShare] -AccessRight Read -Force
} catch {
Write-Host "Error Granting one or more permission: $_" -ForegroundColor DarkMagenta
}
是否可以捕获特定的授权访问语句并打印失败的组名。
假设我有两组:
- NA\admin
- NA\dev
并且 Grant-SmbShareAccess
两个组都失败了,我可以捕捉并打印出来吗:
Permission Failed for: NA\admin Permission Failed for: NA\dev
只使用一次 try catch?
遍历参数:
foreach($group in 'NA\admin','NA\dev'){
try {
Grant-SmbShareAccess -Name [FolderName] -AccountName $group -AccessRight Read -Force
}
catch {
Write-Host "Failed to grant permission to: $group"
}
}