Reputation: 2758
In my Windows directory
C:\Users\jholmes\pichak\analytics
I have run1.ps1 code. I run wsl.exe, now my pwd is
/mnt/c/WINDOWS/system32
How to point to the first path and execute the script?
Upvotes: 19
Views: 27758
Reputation: 3869
This worked for me. You need to have installed Powershell in WSL
I have installed Ubuntu WSL, follow this instructions: Installing PowerShell on Ubuntu
Converts Windows path to WSL path.
Examples
Get-WslPath "C:\Users\Megam\.CppLibs"
Get-WslPath "$PSScriptRoot"
Get-WslPath "$PSCommandPath"
Output
/mnt/c/Users/Megam/.CppLibs
/mnt/c/Users/Megam/Desktop/PsScript
/mnt/c/Users/Megam/Desktop/PsScript/MyScript.ps1
Calling script from Windows ./testcode.ps1 -Parameter1 10 -Parameter2 "HelloWorld"
[CmdletBinding()]
param (
[Parameter()]
[int]
$Parameter1,
[Parameter()]
[string]
$Parameter2
)
function Get-WslPath {
param (
[string]$Path
)
if ($Path -match '^([A-Za-z]):[\\\/]') {
$drive = $matches[1].ToLower()
$result = "/mnt/$drive" + ($Path -replace '^([A-Za-z]):[\\\/]', '/')
$result = $result.Replace("\", "/")
return $result
}
else {
throw "Invalid path '$Path'."
}
}
if ($IsWindows) {
$scriptParameters = @{
"Script" = (Get-WslPath -Path "$PSCommandPath")
"Parameter1" = $Parameter1
"Parameter2" = $Parameter2
}
Write-Host "█ Windows WSL - Code"
Write-Host "ScriptRoot: $PSScriptRoot"
Write-Host "CommandPath: $PSCommandPath"
Write-Warning "Incompatible platform: Windows. Using WSL."
& wsl pwsh -Command {
$params = $args[0]
Write-Host "Wsl User: " -NoNewline ; & whoami
& "$($params.Script)" -Parameter1 $params.Parameter1 -Parameter2 $params.Parameter2
} -args $scriptParameters
exit
}
## WSL, Linux, MacOS code here.
Write-Host "█ Linux WSL - Code"
Write-Host "ScriptRoot: $PSScriptRoot"
Write-Host "CommandPath: $PSCommandPath"
Write-Host "Parameter1: $Parameter1"
Write-Host "Parameter2: $Parameter2"
Output
█ Windows WSL - Code
ScriptRoot: C:\Users\Megam\Desktop
CommandPath: C:\Users\Megam\Desktop\testcode.ps1
WARNING: Incompatible platform: Windows. Using WSL.
Wsl User: x
█ Linux WSL - Code
ScriptRoot: /mnt/c/Users/Megam/Desktop
CommandPath: /mnt/c/Users/Megam/Desktop/testcode.ps1
Parameter1: 10
Parameter2: HelloWorld
Upvotes: 1
Reputation: 288
None of these answers are correct. powershell.exe
is a Windows program so expects a Windows path as an argument.
powershell.exe -File C:\\Users\\jason\\Documents\\WindowsPowerShell\\Scripts\\Write-EventLog-1777.ps1
As the backslash character is used for escaping in Linux, you need to escape the backslash with another backslash.
Upvotes: 3
Reputation: 569
For me this answer was almost the good one but when running for instance:
powershell.exe -File /mnt/c/path/to/script/script.ps1
I got following error:
The argument '/mnt/c/path/to/script/script.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
I then have to:
cd /mnt/c/path/to/script
powershell.exe -File script.ps1
Upvotes: 2
Reputation: 3
Here is the evidence, that with the WSL2, our software can make the Powershell script encrypted and protected: See this picture:
And it can detect and kill the process that uses fanotify because we think it can be used by attackers to hide critical file change (it also can find and kill dtrace/strace/stap/bpftrace/debuggers and process that opens /dev/kmem, /dev/mem, /proc/kcore, and the protected process' /proc/pid/mem etc): This picture shows the encrypted PowerShell script now can detect and kill possible attackers' processes
Upvotes: 0
Reputation: 27872
In WSL2, using the -File
option worked for me:
powershell.exe -File path/to/script.ps1
Upvotes: 17
Reputation: 91
There are two ways to go about this:
cd /mnt/c/
.D:\Ubuntu
. To avoid changing the working directory every time you open WSL, you can modify the shell profile file (bashrc, zshrc etc.) to load the relevant directory at the end.cd /mnt/d/Ubuntu/
at the end of your ~/.zshrc
file if you use zsh or the relevant profile file otherwise.Upvotes: 3