简体   繁体   English

Powershell 脚本在文件资源管理器的控制台 window 中报告执行策略错误

[英]Powershell script reports execution-policy error when in a console window from File Explorer

My script runs fine in ISE however not in a PowerShell console window.我的脚本在 ISE 中运行良好,但在 PowerShell 控制台 window 中运行不正常。

I have tried to replace any " with ' (just in case it was an encoding error but I am not sure).我试图用'替换任何" (以防万一这是一个编码错误,但我不确定)。

EDIT: The script works when copy and pasted into the console but not when opened with the console.编辑:脚本在复制并粘贴到控制台时有效,但在使用控制台打开时无效。

Here is the script, ZAOCC.ps1 :这是脚本ZAOCC.ps1

$path = '\\auisasvc2k801\safe\Information Technology\Support\Powershell\ZAOCC\Excel'
$path2 = '\\auisasvc2k801\safe\Information Technology\Support\Powershell\ZAOCC\PDF'
$xlFixedFormat = 'Microsoft.Office.Interop.Excel.xlFixedFormatType' -as [type]
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
$date = Get-Date -Format 'dd.MM.yyyy'

foreach($workbook in $excelFiles)
{
    $filepath = Join-Path -Path $path2 -ChildPath ('Mine Control Record - ' + $date + '.pdf')
    $workbook = $objExcel.workbooks.open($workbook.fullname, 3)
    $workbook.Saved = $true
    'Saving $filepath'
    $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
    $objExcel.Workbooks.close()
}



$objExcel.Quit()

Here is the error message:这是错误消息:

Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by
a policy defined at a more specific scope.  Due to the override, your shell will retain its current effective
execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more
information please see "Get-Help Set-ExecutionPolicy".
At line:1 char:46
+ ...  -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & "\ ...
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

The error message suggests that you're running your script from File Explorer, using the shortcut menu's Run with PowerShell command (which uses Windows PowerShell ; note that PowerShell [Core] (v6+) no longer offers this command - see middle section). The error message suggests that you're running your script from File Explorer, using the shortcut menu's Run with PowerShell command (which uses Windows PowerShell ; note that PowerShell [Core] (v6+) no longer offers this command - see middle section).

Note that doing so will automatically close the console window that is opened to run the script when the script exits .请注意,这样做会在脚本退出时自动关闭为运行脚本而打开的控制台 window。

To avoid that, open a Windows PowerShell console window first, and then run your script from there - this will also make your problem go away.为避免这种情况,请先打开 Windows PowerShell 控制台 window,然后从那里运行您的脚本 - 这也会使您的问题 Z34D1F9F91FB2E5A14BA66

(Another option is to modify the shortcut-menu command definition, as shown below.) (另一种选择是修改快捷菜单命令定义,如下所示。)


The reason for the error is that your execution policy is set by either a machine-level or a user-level Group Policy , which takes precedence over the attempt to set the execution policy at the process level .错误的原因是您的执行策略是由机器级别或用户级别的组策略设置的,这优先于在进程级别设置执行策略的尝试
In fact, you cannot override an execution policy set via Group Policy from user code , neither with a Set-ExecutionPolicy call nor via the -ExecutionPolicy CLI parameter.事实上,您不能通过用户代码中的组策略覆盖执行策略集,无论是使用Set-ExecutionPolicy调用还是通过-ExecutionPolicy CLI 参数。

The attempt to set the execution at the process level - Set-ExecutionPolicy -Scope Process Bypass - is part of the command line that is used by the shortcut-menu command definition , and it is it - not your script - that triggers the error.尝试在进程级别设置执行 - Set-ExecutionPolicy -Scope Process Bypass - 是快捷菜单命令定义使用的命令行的一部分,并且触发错误的是它 - 而不是您的脚本。

Therefore, the error message will appear on any system where the execution policy is defined via Group Policy , every time a script is directly invoked from File Explorer.因此,每次直接从文件资源管理器调用脚本时,错误消息都会出现在通过组策略定义执行策略的任何系统上。

Note that the error message is confusing in this case, claiming that the command "updated your execution policy successfully", when in reality it had no effect .请注意,在这种情况下,错误消息令人困惑,声称命令“成功更新了您的执行策略”,而实际上它没有效果

Also note that the error is a statement -terminating error [1] , which means that overall execution still continues and your script is invoked, despite the error - assuming that the Group Policy-set execution policy allows it.另请注意,该错误是一个语句终止错误[1] ,这意味着尽管存在错误,但整体执行仍在继续并且您的脚本调用 - 假设组策略集执行策略允许它。

In general, especially given that the console window automatically closes when the script terminates (for whatever reason), consider not using this direct-invocation feature.一般来说,特别是考虑到控制台 window 会在脚本终止时自动关闭(无论出于何种原因),请考虑使用此直接调用功能。

In fact, PowerShell [Core] (v6+) doesn't even offer this shortcut-menu command anymore and instead offers commands at the directory / drive level to open an interactive session there .事实上, PowerShell [Core] (v6+) 甚至不再提供此快捷菜单命令,而是在目录/驱动器级别提供命令以在此处打开交互式session


If you still want to invoke your script via File Explorer and want to avoid the error message, you can modify the shortcut-menu command definition in the registry by simply removing the attempt to set the execution policy.如果您仍想通过文件资源管理器调用脚本并希望避免出现错误消息,您可以通过简单地删除设置执行策略的尝试来修改注册表中的快捷菜单命令定义

The definition is at HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command and modifying it requires elevation (running as admin);定义位于HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command并修改它需要提升(以管理员身份运行); however, it is possible to create a user-level definition as well.但是,也可以创建用户级定义。

The following command redefines the shortcut-menu command definition so that Set-ExecutionPolicy is no longer called:以下命令重新定义快捷菜单命令定义,以便不再调用Set-ExecutionPolicy

# NOTE: Requires elevation:
Set-ItemProperty registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command '(default)' @'
powershell.exe -NoLogo -File "%1"
'@

If you also want to keep the window open, add -NoExit as the first parameter.如果您还想保持 window 处于打开状态,请添加-NoExit作为第一个参数。


[1] Generally speaking, the message shouldn't be an error at all; [1] 一般来说,消息根本不应该是错误 it should be a warning , as suggested in this GitHub issue .这应该是一个警告,正如GitHub 问题中所建议的那样。 However, in the interest of backward compatibility it was decided to keep the current behavior.但是,为了向后兼容,决定保留当前行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM