Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

amend windows hosts files says access denied?

Trying to amend a windows hosts file to add an entry as not all host files are the same at the moment

I can edit the file manually on the PC's and then created a batch file to amend the file.

However when I run it It fails with an access denied error. I am running it thorough PDQ deploy with the same credentials as when I do it manually

 

Any suggestions please? 

 

 

 

 

0

Comments

11 comments
Date Votes
  • Have you tried running this step in PDQ Deploy as "Local System"?

    0
  • Also post some screenshots of your package setup.

    0
  • I have tried running as system and hat also failed. Pretty sure it is the permissions that the accounts have as it is system file but strange it works when logged in normally (Including running the batch file).

     

    No screen shots to show really just fails and when I check the output file says access is denied.

    batch is just this

    Echo >> c:\windows\system32\drivers\etc\hosts
    Echo 10.80.x.x collabnhl.xxxxxx.gov.uk
    Exit

    xxx just so I do not give out addresses etc.

    0
  • 0
  • Try a Powershell step. It will execute Powershell with the RunAs administrator option, giving you access to the C:\windows directory et. al.

     

    Use this as the code, just edit it so the ip and hostname are correct. Check output.log after deployment, it should return the contents of the host file in full:

    #Create backup of hosts file
    Copy-Item -Path C:\windows\system32\drivers\etc\hosts -Destination C:\windows\system32\drivers\etc\hosts.bak

    #write data to file, utf8 format as its a hosts file
    Write-Output '10.80.x.x collabnhl.xxxxxx.gov.uk' | Out-File -NoClobber C:\Windows\System32\drivers\etc\hosts -Append -Force utf8


    Write-Output $(Get-Content 'C:\windows\system32\drivers\etc\hosts')
    1
  • I used a File Copy Step.  Just put the Host File with the correct changes on the DFS Share then copied to the correct location with the Overwrite Existing.. 

    0
  • Dan,

     Can't do a file copy as different hosts on different PC's so need to append it.

     

    Stephen,

    That looks like a possible solution. I will try that

     

    Many Thanks to you all

    0
  • Powershell seems to work but hosts file is not updated when I checked it on some PC's

     

    Strange

    0
  • Works on everything I've tested on. I've got Windows 8.1 everywhere, and Powershell v4, but the code I wrote should be good all the way back to v2.

    0
  • have you checked the log files on the machines the script failed on?

    0
  •  

    Think the server it was running from was version 1.0 still so I have upgraded it and I will test.

     

     

     

    Many thanks for your efforts they are much appreciated

     

     

     

    0