Powershell Access to Path is Denied

64 views Asked by At

I have developed a git pre-commit hook for my workplace. The way it operates is as follows:

There is a pre-commit file that invokes a pre-commit.ps1 file in the same directory. Both are withing the .git/hooks/ folder

The pre-commit.ps1 file is used to identify some regular expressions to ensure that any files committed do not have any identifiers specific to my workplace.

The setup works perfectly for me. However, I have tried testing on a colleagues laptop and she is getting the following error when trying to commit:

Access to the path is denied. + CategoryInfo : ObjectNotFound: (:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CommandNotFoundException

Here is the code in th pre-commit file:

#!/bin/sh

# Get the directory where the script is located

HOOK_DIR=$(dirname "$0")

# Use 'which' to find the PowerShell executable in the PATH

POWERSHELL_PATH=$(which powershell.exe || which pwsh)

# Check if PowerShell was found

if [ -z "$POWERSHELL_PATH" ]; then

    echo "PowerShell not found in PATH"

    exit 1

fi

# Invoke the PowerShell script using the dynamically found path

"$POWERSHELL_PATH" -ExecutionPolicy ByPass -NoProfile -File "$HOOK_DIR/pre-commit.ps1"

I'm not really sure what the issue is, specially since I can run the setup perfectly well, but my colleague cannot.

For testing, we simplified the pre-commit.ps1 file to only print a statement such as:

Write-Host "Hello"

However, she is still getting the same error - so we concluded that the issue must be with the invocation of PowerShell in the pre-commit file.

I have also confirmed:

  1. She has powershell installed
  2. Her Get-ExecutionPolicy is the same as mine, set to "Bypass"
  3. However, her starting directory on a PowerShell terminal is different to mine. Mine starts at H:, whereas hers starts at C:\Windows\System32\ (not sure if this relevant or I'm missing something).
0

There are 0 answers