SNair
SNair

Reputation: 85

Access denied error while running PowerShell script

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"

Error:

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?

Upvotes: 0

Views: 8647

Answers (1)

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

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:

Install Jenkins as a Windows service

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).

Upvotes: 2

Related Questions