0

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?

5
  • 1
    The account that is used to execute the script doesn't have permission to read the file at \\server1.us.o...\Image.log. Commented Feb 28, 2017 at 6:24
  • But I have access domain admin access to user accounts Commented Feb 28, 2017 at 6:30
  • 8
    Looks like you are running this in Jenkins. What account is that using? add a "whoami" in your script to see for sure. You may need to create a read-only share for that user instead of c$. Commented Feb 28, 2017 at 7:05
  • it says nt authority\system!!! I am not sure how this account t is being used to run the script Commented Feb 28, 2017 at 11:58
  • Add a Valid Credential to your Test-Connection Commented Mar 7, 2017 at 7:12

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.