Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using Invoke-Expression


In this recipe, we will use the Invoke-Expression cmdlet to compress some files using a free compression utility.

Getting ready

For this recipe, we will use the 7-Zip application to compress some files. Download 7-Zip from http://www.7-zip.org/.

How to do it...

Let's check out the Invoke-Expression cmdlet:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    $VerbosePreference = "Continue"
    
    $program = "`"C:\Program Files\7-Zip\7z.exe`""
    
    #arguments
    $7zargs = " a -tzip "
    $zipFile = " `"C:\Temp\new archive.zip`" "
    $directoryToZip = " `"C:\Temp\old`" "
    
    #compose the command
    $cmd = "& $program $7zargs $zipFile $directoryToZip "
    
    #display final command
    Write-Verbose $cmd
    
    #execute the command
    Invoke-Expression $cmd
    
    $VerbosePreference = "SilentlyContinue"

How it works...

The Invoke-Expression cmdlet allows PowerShell expressions to run from PowerShell. These expressions can consist of other PowerShell statements and functions, or they can contain executables...