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

the*_*nja 4 powershell fully-qualified-naming windows-server-2012-r2

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

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

它失败了,出现以下错误:

&:AuthorizationManager检查失败.在行: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.这是怎么回事?

Fox*_*loy 5

看来你已经找到了解决方法(使用短名称和FQDN),所以我会尝试回答你为什么首先遇到这个问题.

在这篇博客文章中可以找到更多的内容 ; 实际上会发生这种情况,因为当您向服务器指定FQDN时,您将遇到PowerShell/Windows安全功能之一.即使您指定PowerShell应该绕过正常的执行策略,但从FQDN运行会使Windows认为此文件来自Web,因此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"):
Run Code Online (Sandbox Code Playgroud)

但它不能,因为你在NonInteractive模式下运行shell.


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

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

为单台计算机配置FQDN安全提示解决方案

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

用户配置,展开策略> Windows设置> Internet Explorer维护>安全3.双击安全区域和内容分级,然后选择导入当前安全区域和隐私设置.

有关组策略方法的更多信息,请参阅TechNet上的此主题.

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