当脚本路径完全限定时,在删除服务器上执行 Powershell 失败

Executing Powershell on remove server fails when path to script is fully qualified

我在同一域中有两台服务器 运行 Windows Server 2012 R2,\tt-sql.perf.corp 和 \tt-file.perf.corp。文件服务器上的共享文件夹中有一个 Powershell 脚本,\tt-file.perf.corp\fileshare\helloworld.ps1。我在 sql 服务器上有一个应用程序执行以下命令:

powershell -NonInteractive -InputFormat None -ExecutionPolicy Bypass -Command "& '\tt-file.perf.corp\fileshare\helloworld.ps1'"

失败并出现以下错误:

& : AuthorizationManager check failed. At line:1 char:3 + & '\tt-file.perf.corp\fileshare\helloworld.ps1' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess

如果我更改路径以使用 IP 地址,它也会失败。

但是,当脚本的路径不是完全限定时,它会起作用: powershell -NonInteractive -InputFormat None -ExecutionPolicy Bypass -Command "& '\tt-file\fileshare\helloworld.ps1'"

Windows Management Instrumentation 服务在两台服务器上都是 运行。我也在两台服务器上 运行 Get-ExecutionPolicy,并且都设置为 Unrestricted。两台服务器上都禁用了 UAC。怎么回事?

您似乎已经找到了解决方法(使用短名称与 FQDN),因此我将首先尝试回答您遇到此问题的原因。

可以找到更大的in this blog post;实际上,这种情况正在发生,因为当您为服务器指定 FQDN 时,您 运行 违反了 PowerShell 中的一项/Windows 安全功能。即使您指定 PowerShell 应绕过正常执行策略,来自 FQDN 的 运行 也会使 Windows 相信此文件来自网络,因此 PowerShell 想要向您显示警告这个:

Run only scripts that you trust. While scripts from the Internet can be
useful, this script can potentially harm your computer. Do you want to run  
\tt-file.perf.corp\fileshare\helloworld.ps1?
[D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"):

但它不能,因为你是 运行 非交互模式下的 shell。


所以你有两个选择来解决这个问题,真的:

  1. 正如这篇博文 post 提到的,您可以通过将 UNC 路径设为 IE 中的受信任站点来解决问题,或者仅使用您所见的短名称(使用 \tt-file\ 而不是\tt-file.perf.corp) 代替。
  2. 您可以为本地 Intranet 区域使用组策略(或在 IE 中配置,如果这是一台一次性计算机)地址。如果这是一次性机器,请转至 Internet Explorer、工具、Internet 选项,然后转至安全选项卡。单击“本地 Intranet”、“高级”,然后在此处添加您的 FQDN,如下所示。

如果这是您想要全局配置的设置,请像我上面那样在组策略管理控制台中的以下位置指定路径:

User Configuration, expand Polices > Windows settings >Internet Explorer Maintenance >Security 3. Double click Security Zones and Content Ratings, then chose Import the current security zones and privacy settings.

有关组策略方法的更多信息,refer to this thread here on TechNet.

希望对您有所帮助!不幸的是,我想不出一个好的 PowerShell 方法来解决这个问题 :)。