Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to confirm completion of previous command in powershell

Tags:

powershell

I have a simple powershell script that gets ran daily to compress and move some log files. How can i test that the command completes successfully before deleting the original log file.

set-location $logpath1
& $arcprg $pram $dest_file $source_file
Move-Item $dest_file $arcdir

If the Move-Item completes ok i want to remove-item $source_file

like image 777
Travis Avatar asked Sep 15 '10 20:09

Travis


People also ask

Can I see history of PowerShell commands?

Part 1: View Command History in PowerShell When you're at the PowerShell console, just run the history command and it will display all commands you typed during the current session. File Explorer will open to your specified location and you can see a text file named ConsoleHost_history. txt .

How do I know if a PowerShell command worked?

You can use “$error” automatic variable. This will check if the first command throws error or if it completes successfully. if you want to clear the errors afterwards you can use: $error.

How do I check PowerShell execution status?

Use one of the two variables PowerShell provides to determine the status of the last command you executed: the $lastExitCode variable and the $? variable. The $lastExitCode PowerShell variable is similar to the %errorlevel% variable in DOS. It holds the exit code of the last application to exit.

What is the PowerShell command to display the history of commands executed?

F7 - Displays the command history.


2 Answers

The completion status of the previous command can be accessed via the special variable $?.

Note that this works best with non-terminating errors (like you would get from Move-Item). Terminating errors are the result of a direct throw or an exception getting thrown in .NET and they alter the flow of your code. Best to use a trap or try/catch statement to observe those type of errors.

One other thing to watch out for WRT $? and console exes is that PowerShell assumes an exit code of 0 means success (i.e. $? is set to $true) and anything else means failure ($? set to $false). Unfortunately not all console exe's observe that exit code convention e.g. there may be multiple success codes and a single failure code (0). For those exes that don't follow the exit code rules, use $LastExitCode as pointed out in the comments to determine success or failure.

like image 80
Keith Hill Avatar answered Oct 29 '22 03:10

Keith Hill


Depending on how parnoid you are and what component you are using for archiving, you can check the archive to confirm the file exixts. We use DotNetZip component to zip our archive log files (http://dotnetzip.codeplex.com/).

$zipFileObj =  new-object Ionic.Zip.ZipFile($zipName);

[void] $zipFileObj.UpdateFile( "$fileName", "" )   # adds file if doesn't already exist

trap   #catch an zip errors and Stop processing
{
  write-error "Caught a system exception. Execution stopped"
  write-error $("TRAPPED: " + $_.Exception.Message); 
  exit
}

if ( $zipFileObj.ContainsEntry( $fileName) )
{
  remove-item $pathFile  # delete file from file-system
}
else
{
  # throw error
}
like image 25
Jim Ecker Avatar answered Oct 29 '22 03:10

Jim Ecker