"Test-Path : Access is denied" Error Executing PowerShell Script From Task Sequence RRS feed

  • Question

  • Hey Guys -

    I wrote a new PowerShell script this weekend which basically copies files from the PC it's executed upon to server shares and records data to a specific registry key on the local PC.  The shares it copies them to are gathered via an accompanying txt file listing shares.  I've successfully executed the script locally on my workstation (and a couple of others), but when attempting to execute it within a task sequence, it fails.  Below are the details:

    Error in smsts.log During Execution

    • Test-Path : Access is denied
    • At C:\_SMSTaskSequence\Packages\AHC00067\CopyTest.ps1:103 char:19
    • +         If (-not (Test-Path $Target))
    • +                   ~~~~~~~~~~~~~~~~~
    •     + CategoryInfo          : PermissionDenied: (\\SCCMABC01.a...dtest\Speed
    •    Test:String) [Test-Path], UnauthorizedAccessException
    •     + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.Powe
    •    rShell.Commands.TestPathCommand

    Task Sequence Step Configuration (TS only has single step)

    • Step Type: Run PowerShell Script
    • Select package with script and txt file in root of package source 
    • Script name: CopyTest.ps1
    • Parameters: -Path (Get-Content shares.txt)
    • Policy: Bypass

    The only place I use "Test-Path" in the script is when checking the registry to see if a specific key exists and if not, it creates a the new custom one before writing the values.  I suppose I could use "-Force" to create the key instead of testing for it, but if I do so and it exists, won't it wipe all data previously in it?

    I initially thought this may be a permissions issue with the SCCM Service account, but I have many task sequence steps that write to the registry of the local PC.  If it's the local instance of PowerShell that doesn't have the rights, how do I change that in the TS / script?

    Any suggestions would be appreciated - Thanks!

    Ben K.

    Monday, January 29, 2018 4:36 PM

All replies

  • According to the above, Test-Path is in the script for more than a registry check:

    Line 103 contains "If (-not (Test-Path $Target))"

    and the path it's trying to get to is \\SCCMABC01.a...dtest\Speed which is not a registry path.

    Also, there is no such thing as a "SCCM Service account". ConfigMgr never uses service accounts so not sure what you are referring to there.

    Jason | | @jasonsandys

    Monday, January 29, 2018 7:38 PM
  • Hi,

    The error is very clear, you do not have permission to read the file.
    The script is executed as the system or computer account on the targeted client. This account has limited network access. Any access to remote systems and locations by the script must be provisioned accordingly.

    Please remember to mark the replies as answers if they help. If you have feedback for TechNet Subscriber Support, contact

    Tuesday, January 30, 2018 11:03 AM
  • You run a "Run PowerShell Script" step, so it will run as SYSTEM on the targeted computer. Unless you grant the computer account the appropriate permissions to the share it should write to, it won't work.

    Alternatively you can use a "Run Command Line" step and specify a user with sufficient permissions on the share under "Run this step as the following account".

    Wednesday, January 31, 2018 1:40 PM
  • Hello Ben K,

    I resolved the above issue by assigning the Test path to a variable and then checking the value of the variable using the JSON.

    Friday, July 5, 2019 5:26 AM