Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script echo current command in PowerShell

Tags:

powershell

Suppose I'm running a PowerShell script that takes several input parameters. The command looks like:

psScript.ps1 -arg1 "arg1value" -arg2 "arg2value"

Is there a way to store this exact command in a variable within the script so that I can log it?

Specifically, I'd like to know what to assign to the variable $currentCommand:

$currentCommand = <something>
Write-Host "currently running script " $currentCommand

Such that the Write-Host output would be the exact command line used to invoke the script. If the script command was the same as above, for example, then the output would be:

currently running script psScript.ps1 -arg1 "arg1value" -arg2 "arg2value"

like image 336
brad Avatar asked Jan 11 '13 17:01

brad


2 Answers

This may suit your needs:

Write-Host "currently running script " $myinvocation.Line

Reference

like image 69
Stéphane Gourichon Avatar answered Sep 21 '22 01:09

Stéphane Gourichon


The $MyInvocation variable will have the information. Here is a good blog post about it.

like image 23
Adam Driscoll Avatar answered Sep 24 '22 01:09

Adam Driscoll