【问题标题】:How to verify whether the share has write access?如何验证共享是否有写权限?
【发布时间】:2014-08-08 01:40:41
【问题描述】:

我正在共享具有写访问权限。我正在编写一个 powershell 脚本来在该共享中写入一个日志文件。

我想在写入之前检查我是否拥有对此共享的写入权限。

如何使用 powershell 检查写入权限/完全控制?

我已尝试使用 Get-ACL cmdlet。

 $Sharing= GEt-ACL "\\Myshare\foldername
    If ($Sharing.IsReadOnly) { "REadonly access" , you can't write" }

它有 Isreadonly 属性,但是有什么方法可以确保用户具有完全控制权限?

【问题讨论】:

  • 你试过了吗?为解决您的问题付出一点努力,社区会为您提供帮助。
  • @Christian:伙计,我已经更新了我在这个问题上的努力。
  • 您可以使用来自 Sysinternals 的AccessChk。另一种选择是使用 Win32 函数 GetEffectiveRightsFromAcl,它调用与使用文件/文件夹属性的有效权限选项卡时调用的 API 相同的 API。然而,这在 PowerShell 中实现起来要复杂得多。

标签: powershell powershell-2.0


【解决方案1】:

这与 @Christian 的 C# 做同样的事情,只是不编译 C#。

function Test-Write {
    [CmdletBinding()]
    param (
        [parameter()] [ValidateScript({[IO.Directory]::Exists($_.FullName)})]
        [IO.DirectoryInfo] $Path
    )
    try {
        $testPath = Join-Path $Path ([IO.Path]::GetRandomFileName())
        [IO.File]::Create($testPath, 1, 'DeleteOnClose') > $null
        # Or...
        <# New-Item -Path $testPath -ItemType File -ErrorAction Stop > $null #>
        return $true
    } catch {
        return $false
    } finally {
        Remove-Item $testPath -ErrorAction SilentlyContinue
    }
}
Test-Write '\\server\share'

我想研究在 PowerShell 中实现 GetEffectiveRightsFromAcl,因为这样可以更好地回答这个问题......

【讨论】:

  • 仍然是您使用 .net 类而不仅仅是 cmdlet 的事实;)
  • 那么:[ValidateScript({[IO.Directory]: :Exists($_.FullName)})] [IO.DirectoryInfo] $path。 ?
  • @Christian 我建议远离嵌入式 C#(如果可能的话)的原因是:1)它不能用 ISE/PowerGUI 调试 2)没有语法高亮/代码完成 3)有不同语法规则比 PowerShell。我只建议嵌入 C# 来实现 PowerShell 本身无法实现的功能(cmdlet、.NET 类),例如 bit-shiftingP/Invoking
  • @Christian 哦,这只是参数块的一部分,它可以替换为[ValidateScript({Test-Path $_.FullName})]。它只是为了确保输入有效。
  • 我尊重您的意见,但只是观点!无论如何,Powershell 在 .net 上都很受欢迎!
【解决方案2】:

我用这种方式来检查当前用户是否对路径有写权限:

# add this type in powershell

add-type @"
using System;
using System.IO;

public class CheckFolderAccess {

 public static string HasAccessToWrite(string path)
        {
            try
            {
                using  (FileStream fs = File.Create(Path.Combine(path, "Testing.txt"), 1, FileOptions.DeleteOnClose))
                { }
                return "Allowed";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

}
"@

# use it in this way:

if ([checkfolderaccess]::HasAccessToWrite( "\\server\share" ) -eq "Allowed") { ..do this stuff } else { ..do this other stuff.. }

代码不检查 ACL,但如果可以在路径中写入文件,则返回字符串“允许”,否则返回异常的消息错误。

【讨论】:

  • 为什么不使用 C# 将其变成 PowerShell 函数?
  • 在我的框中,类型是由 $profile 中加载的自定义模块添加的。可以使用 --IgnoreWarnings 添加一个函数,以防重新加载
  • 只是想在纯 powershell 中制作函数。没有什么真正需要 C#。
  • 好的!误解了.. 是的,但对于答案,我只是复制并粘贴我的代码:)
  • @AndyArismendi 我希望看到你对这个问题的纯 powershell 解决方案,因为我一个人无法弄清楚:-)
【解决方案3】:

这是我构建的一个非常简单的函数。它返回“Read”、“Write”、“ReadWrite”和“”(表示无法访问):

function Test-Access()
{
    param([String]$Path)
    $guid = [System.Guid]::NewGuid()
    $d = dir $Path -ea SilentlyContinue -ev result
    if ($result.Count -eq 0){
        $access += "Read"
    }   
    Set-Content $Path\$guid -Value $null -ea SilentlyContinue -ev result
    if ($result.Count -eq 0){
        $access += "Write";
        Remove-Item -Force $Path\$guid
    }   
    $access
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2018-07-11
    相关资源
    最近更新 更多