如何在Windows 10下阻止PowerShell中的Windows关闭

时间:2017-03-23 13:36:59

标签: windows powershell

我必须运行PowerShell脚本,这需要很长时间才能完成。

在此期间,我想阻止运行它的计算机被用户关闭。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

这样的事可能有用(未经测试):

$File = "$env:SystemRoot\system32\shutdown.exe"
$PreviousAcl = Get-Acl $File

$Principal = "domain\user"
$Right = "Read"
$Rule = New-Object System.Security.AccessControl.FileSystemAccessRule($Principal,$Right,"Deny")

Try {
    $Acl = Get-Acl $File
    $Acl.SetAccessRule($Rule)
    Set-Acl $File $Acl 
}
Catch {
    Write-Error "ACL failed to be set on: " $File
}


# yourscript here


Try {
    Set-Acl $File $PreviousAcl 
}
Catch {
    Write-Error "ACL failed to be set on: " $File
}

来源:https://www.reddit.com/r/sysadmin/comments/52x3s2/create_deny_permissions_on_folders_via_powershell/d7o17wu/