0
votes

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?

1
The account that is used to execute the script doesn't have permission to read the file at \\server1.us.o...\Image.log. - Lieven Keersmaekers
But I have access domain admin access to user accounts - SNair
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$. - Kory Gill
it says nt authority\system!!! I am not sure how this account t is being used to run the script - SNair
Add a Valid Credential to your Test-Connection - Lieven Keersmaekers

1 Answers

2
votes

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