I have the following powershell script, which I call from the command line:
param(
[Parameter(Mandatory = $true)]
[string]$Name,
[Parameter(Mandatory = $true)]
[ValidateScript(
{ $_ -in (Get-ChildItem "..\src\Services" -Directory | Where-Object { $_.Name -ne "obj" }).Name }
)]
[ArgumentCompleter(
{
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
$ValidValues = (Get-ChildItem "..\src\Services" -Directory | Where-Object { $_.Name -ne "obj" }).Name
return @($ValidValues) -like "$WordToComplete*"
}
)]
[string]$Project
)
& dotnet ef migrations add $Name `
--project "..\src\Services\$Project\$Project.Infrastructure" `
--startup-project "..\src\Services\$Project\$Project.WebApi"
As you can see, ..\src\Services is used in multiple places. The main issue is its use in ValidateScript and ArgumentCompiler.
I have tried solving this by passing in an optional param $PathPrefix = "..\src\Services", but this does not become available in the above-mentioned script decorators, so my argument validation and tab-completion don't work.
Is what I am trying to do possible?
Also is there a name for such functions which encompass an entire file (rather than being defined using function)?
$env:MyScriptPath = "..\src\Services"then your validation and completion use this variable otherwise a more complicated workaround might require defining your own class that takes the path as argument, see last example in stackoverflow.com/a/70061653/15339544ValidateScriptbut notArgumentCompleter- will need to try out the custom class approach