Executar com o PowerShell s $myinvocation.line
é obtido da chave de registro correspondente. Por exemplo, no (meu) Windows 8.1 com PS versão 5.1 :
PS D:\PShell> $auxRegKey='\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell'script ouput here'
$auxRegKey='\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell==> "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'D:\PShell\SF\q866281.ps1'"
script ouput here
D:\PShell\SF\q866281.ps1: supposedly run via explorer right click
==>
\Command'
$auxRegVal=(get-itemproperty -literalpath HKLM:$auxRegKey).'(default)'
$auxRegCmd=$auxRegVal.Split(' ',3)[2].Replace('%1', $MyInvocation.MyCommand.Definition)
if ("'"$($myinvocation.Line)'"" -eq $auxRegCmd) {
$MyInvocation.MyCommand.Definition + ': supposedly run via explorer right click'
$x = Read-Host
} else {
$MyInvocation.MyCommand.Definition + ': run from CLI' # optional
}
\Command'
PS D:\PShell> (get-itemproperty -literalpath HKLM:$auxRegKey).'(default)'
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
PS D:\PShell>
O snippet de código a seguir pode ajudar:
PS D:\PShell> $auxRegKey='\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell'script ouput here'
$auxRegKey='\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell==> "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'D:\PShell\SF\q866281.ps1'"
script ouput here
D:\PShell\SF\q866281.ps1: supposedly run via explorer right click
==>
\Command'
$auxRegVal=(get-itemproperty -literalpath HKLM:$auxRegKey).'(default)'
$auxRegCmd=$auxRegVal.Split(' ',3)[2].Replace('%1', $MyInvocation.MyCommand.Definition)
if ("'"$($myinvocation.Line)'"" -eq $auxRegCmd) {
$MyInvocation.MyCommand.Definition + ': supposedly run via explorer right click'
$x = Read-Host
} else {
$MyInvocation.MyCommand.Definition + ': run from CLI' # optional
}
\Command'
PS D:\PShell> (get-itemproperty -literalpath HKLM:$auxRegKey).'(default)'
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
PS D:\PShell>
O script diz supostamente porque poderíamos imaginar o seguinte comando ( improvável ) a partir de uma janela cmd
aberta (ou até mesmo o seu equivalente PowerShell
prompt):