PowerShell受限语言模式在脚本中不生效的问题求助
关于AppLocker与PowerShell语言模式的异常问题
我配置了仅允许使用公司证书签名脚本的AppLocker策略,未创建默认脚本策略。在Windows 11机器上运行PowerShell 5.1或7控制台时,$ExecutionContext.SessionState.LanguageMode正确返回ConstrainedLanguage(PowerShell 7启动时直接显示该信息)。
我有一个非常简单的未签名脚本:
$ExecutionContext.SessionState.LanguageMode [System.Console]::WriteLine("Hello")
在控制台中直接输入第二行[System.Console]::WriteLine("Hello")时,得到预期错误:
Cannot invoke method. Method invocation is supported only on core types in this language mode. At line:1 char:1 + [System.Console]::WriteLine("Hello") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
但运行该脚本时,输出如下:
FullLanguage Hello
使用以下命令测试AppLocker规则:
Get-AppLockerPolicy -Effective | Test-AppLockerPolicy -Path C:\Tests\test.ps1 -User Install #("Install"是我的测试用户)
返回结果正确:
FilePath PolicyDecision MatchingRule -------- -------------- ------------ C:\Tests\test.ps1 DeniedByDefault
若添加一条拒绝所有人访问所有路径的AppLocker规则,返回结果正确:
FilePath PolicyDecision MatchingRule -------- -------------- ------------ C:\Tests\test.ps1 Denied *
但运行脚本时仍得到相同结果,脚本以FullLanguage模式运行。
编辑1
若在另一个脚本(甚至自身)中调用该脚本,尽管第一个脚本仍以FullLanguage模式执行,但脚本调用行为符合预期:
C:\Tests\test.ps1: C:\Tests\test.ps1:3 Line | 3 | C:\Tests\test.ps1 | ~~~~~~~~~~~~~~~~~ | The term 'C:\Tests\test.ps1' is not recognized as a name of a cmdlet, function, script file, or executable | program. Check the spelling of the name, or if a path was included, verify that the path is correct and try | again.
编辑2
若在保留AppLocker策略的同时应用Device Guard UMCI策略,脚本会正确以ConstrainedLanguage模式运行,这与微软说明不符(被阻止的脚本在事件日志中抛出错误8007但以受限模式运行)。脚本文件不在Device Guard UMCI策略范围内,仅签名者在策略中,且签名脚本也以ConstrainedLanguage模式运行……但它们本应以FullLanguage模式运行。
核心疑问:
- 为何未签名脚本未被签名却以FullLanguage模式运行?
- 为何添加UMCI策略后,签名脚本虽同时符合UMCI和AppLocker策略,却以ConstrainedLanguage模式运行?
内容的提问来源于stack exchange,提问作者CFou
相关产品推荐
相关产品推荐

