If you depend on using a particular version of PowerShell the "which powershell.exe || which pwsh" is always going to find PowerShell versions 5 or below. That would make "C:\WINDOWS\System32\WindowsPowerShell\v1.0" the value in POWERSHELL_PATH. The (default) access to that directory for members of the "Users" group is "Read & Execute", "List folder contents", and "Read". So there should be no problem with a Windows user running PowerShell. The "-z" is checking whether the "POWERSHELL__PATH" environment variable is set. Since it isn't, the problem appears to be that the "which powershell.exe || which pwsh" isn't finding a path in the environment variables. Have you verified that the check for the powershell.exe/pwsh is working as expected on your co-workers machine?
Powershell Access to the path is denied
Hi,
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.
- 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
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. 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.
I have also confirmed:
- She has powershell installed
- Her Get-ExecutionPolicy is the same as mine, set to "Bypass"
- 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).
2 answers
Sort by: Most helpful
-
-
MotoX80 35,621 Reputation points
2024-02-04T14:59:54.7466667+00:00 Mine starts at H:/, whereas hers starts at C:\Windows\System32
She likely launched Terminal with "Run as administrator". Do all refernces to any files include the drive letter or do they just referance the current drive or working directory?
so we concluded that the issue must be with the invocation of PowerShell in the pre-commit file.
I don't know what shell you are using, but if you can 'echo "PowerShell not found in PATH"', then add in additional echo statements to validate your environment.
echo "POWERSHELL_PATH" echo $POWERSHELL_PATH ls $POWERSHELL_PATH echo "HOOK_DIR" echo $HOOK_DIR ls $HOOK_DIR echo "Commit script" ls $HOOK_DIR/pre-commit.ps1
If you still get "Access to the path is denied.", then download and run Process Monitor.
https://learn.microsoft.com/en-us/sysinternals/downloads/procmon
Start the trace and stop it immediately after you get the error. Search the trace for "pre-commit.ps1" or "access denied". Is Powershell.exe getting launched? Can it read the script?
If the error is occurring within the PS script, then generate a transcript to trace what it's doing.