Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell为没有访问权限的文件夹获取Acl_Powershell_Permissions_Active Directory_Credentials_Folder Permissions - Fatal编程技术网

Powershell为没有访问权限的文件夹获取Acl

Powershell为没有访问权限的文件夹获取Acl,powershell,permissions,active-directory,credentials,folder-permissions,Powershell,Permissions,Active Directory,Credentials,Folder Permissions,我编写了一个脚本,它为我提供了用户/组的文件夹+子文件夹的所有权限。但是,仅当我的用户至少对所有这些文件夹具有读取权限时,脚本才起作用。如果他没有权限,则拒绝获取acl。 有没有办法解决这个问题,因为我不想每次执行这个脚本时都手动切换我的用户 我可以使用其他用户执行powershell脚本吗?如果是,怎么做 事先谢谢你,科林你有几个选择我可以考虑: 备选案文1: 使用要运行的实际代码创建一个帮助文件,并将其称为script.ps1,例如: [array]$users = "user1",

我编写了一个脚本,它为我提供了用户/组的文件夹+子文件夹的所有权限。但是,仅当我的用户至少对所有这些文件夹具有读取权限时,脚本才起作用。如果他没有权限,则拒绝获取acl。 有没有办法解决这个问题,因为我不想每次执行这个脚本时都手动切换我的用户

我可以使用其他用户执行powershell脚本吗?如果是,怎么做


事先谢谢你,科林你有几个选择我可以考虑:

备选案文1: 使用要运行的实际代码创建一个帮助文件,并将其称为script.ps1,例如:

    [array]$users = "user1","user2","user3"

    foreach($user in $users){
        $creds = Get-Credential -UserName $user -Message "Enter the Users Password"
        $Session = New-PSSession -Credential $creds
        Invoke-Command -Session $Session -FilePath C:\Path\to\some\script.ps1
    }
选项2:为每个用户运行作业。完成每个任务后,将询问新的用户凭据。只需将代码添加到脚本块

[array]$users = "user1","user2","user3"

foreach($user in $users){
    $creds = Get-Credential -UserName $user -Message "Enter the Users Password"
    $target = $user
    $job = Start-Job -scriptblock {
    param ($username)
        Get-Acl C:\Users\$user #Bla bla the rest of your script
    } -Args $user -credential $creds
    do{
        #Wait for the job to finish
    }until($job.State -ne "Running")
    Write-Host "Job finished with state $($job.State)"
}
希望这有帮助

请注意,如果您不希望一直键入,creds对象也可以自动执行。未考虑安全原则

$users = @()
$users += @{
    username = "User1"
    password = "Pass123!"
}
$users += @{
    username = "User2"
    password = "Pass123!"
}

foreach($user in $users){
    $creds = New-Object System.Management.Automation.PSCredential($user.username,($user.password | ConvertTo-SecureString -AsPlainText -Force))
    #Add the rest of the script from the chosen option
}

实现这一点有很多选择。查看这篇文章:是的,但我希望在不需要任何其他用户操作的情况下立即执行脚本。你说的立即是什么意思?如果你发布你的代码和你的意思,它会更容易提供帮助。就我现在的理解,您应该使用try-catch来确定用户是否具有权限。因此,当访问被拒绝时,您可以捕获。我无法向您显示任何代码。但我的意思是:1个脚本被执行2个请求用户的凭据,你想执行脚本,3个执行脚本的其余部分。我不想打开新的Powershell窗口。如果可能的话。非常感谢!选项1可能正是我想要的。我将进行尝试。我遇到以下错误:获取凭据:找不到与参数名称“UserName”以及:[localhost]匹配的参数,连接到远程服务器失败,错误消息如下:客户端无法连接到请求中指定的目标。验证目标上的服务是否正在运行并正在接受请求。使用get-credential-credential$user,我修复了第一个错误。但是,我仍然无法运行其他脚本您需要先启用PSRemoting才能启动PSSessions。查看此链接: