运行PowerShell脚本时访问被拒绝错误 [英] Access denied error while running PowerShell script

查看:3096
本文介绍了运行PowerShell脚本时访问被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本,并且设定了一个条件,即如果服务器要搜索下面的文件并读取文件内容.但它的给出错误如下所示:

I have written a script and i have put a condition that if server is pining search for below file and read the file content. but its giving error like below

if ((Test-Connection -ComputerName $fqdn -Count 1 -Quiet) -eq 1)
{
    $Value = "Host is pinging"

    $searchtext = "Imaging Completed"
    $file = "\\$fqdn\C$\Image.log"

    if (Test-Path $file)
    {
        if (Get-Content $file | Select-String $searchtext -quiet)
        {
            Write-Host "$fqdn Imaging Completed"

错误:

Test-Path : Access is denied
At C:\Windows\TEMP\hudson4530962683016292267.ps1:65 char:5
+ if (Test-Path $file)
+     ~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (\\server1.us.o...\Image.log:String) [Test-Path], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand

有人可以在这里纠正我的问题吗?

Can anyone correct me here to fix the issue?

推荐答案

正如其他评论所指出的那样,您必须使用有效的凭据才能访问有问题的文件.

As the other comments already pointed out you have to use valid credentials to be able to access the file in question.

如前所述,脚本的帐户名在服务下运行(怀疑是詹金斯).如果您遵循手册将Jenkins安装为Windows服务,并查看您的服务配置,您可能会发现该服务实际上以LocalSystem的身份运行,它将解释观察到的用户名.这也将与使用相同帐户的手册中的屏幕截图相匹配:

As you mentioned, the account name of the script is run under a service (Jenkins was suspected). If you followed the manual Install Jenkins as a Windows service and have a look at your service configuration, you might find, that the service actually runs as LocalSystem which would explain the observed user name. This would also match with the screenshot from the manual where the same account is used:

为了解决这种情况,您可以选择将此服务配置为在具有足够特权(尤其是NetworkAccess)的其他帐户下运行(其中您提到的具有域管理员访问权限"的帐户应足够). 但是请记住,建议始终使用特权最少的帐户.)

In order to remedy the situation, you can choose to configure this service to run under a different account with sufficient (especially NetworkAccess) privileges (where the account you mentioned which has 'domain admin access' should be sufficient). However keep in mind, it is advised to always use an account with privileges as little as possible).

这篇关于运行PowerShell脚本时访问被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆