Pages

Friday, October 22, 2010

Passing Arguments in PowerShell to Start-Process

Recently while automating a Tivoli Storage install with a Powershell script I noticed trouble passing arguments to the normal Start-Process cmdlet. In this case I was calling msiexec.exe and then passing it arguments to it to the installer, it was an issue due to the fact that most of the arguments had quotes and spaces.

Later in on the same project I was working with a command line and wanted the command output . I noticed that Start-Process didn’t allow me to have the output lines in a string array. The result was that both problems could be solved by writing a function that wrapped Start-Process and allowed a solution to both problems.

function StartProcess ($FileToExecute,$Arguments,$ClientDir , $printCommandtoConsole) {
 $Tempfile = "$Env:TEMP\tempfile.txt"  
 New-Item $Tempfile  -ItemType file  -Force
 
 Start-Process $FileToExecute -ArgumentList $Arguments -Wait -WorkingDirectory $ClientDir -RedirectStandardOutput $Tempfile 
 
 if($printCommandtoConsole){
  Write-Host "$ClientDir\$FileToExecute" $Arguments
 }
 
 Get-Content $Tempfile
 Remove-Item $Tempfile -Force 
}

Here's an example of showing the creation of the arguments and then calling the StartProcess function.

function InstallTSMScheduler {
 $Arguments = @()
 $Arguments += "install"
 $Arguments += "scheduler"
 $Arguments += "/name:`"TSM Scheduler`""
 $Arguments += "/node:" + [Environment]::MachineName
 $Arguments += "/password:`"$NodePassword`""
 $Arguments += "/clientdir:`"$ClientDir`""
 $Arguments += "/optfile:`"$ClientDir\$OptFile`""
 $Arguments += "/autostart:no"
 $Arguments += "/startnow:no"
 
 StartProcess "$ClientDir\dsmcutil.exe" $Arguments $ClientDir $printCommands
}

$CommandLog = InstallTSMScheduler 
$CommandLog
}

I'll Post the rest of the Tivoli Client Install Script at a later date if some one wants it.

Seems i wasn't the only one with the problem (link here)

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am trying to convert an old batch file that installs an msi to powershell. Can you let me know what I'm doing wrong?

    This is my batch file:
    echo off
    echo ###############################
    echo Starting deploying process
    echo ###############################
    echo Starting Installing msi file
    set folder=ReleaseInstallers_%date:~4,2%-%date:~7,2%-%date:~10,4%
    msiexec /quiet /i D:\DAQ\DAQ\%folder%\DetailedQuote.Install.msi INSTALL_WEBSITE_IP=10.1.54.102 INSTALL_WEBSITE_PORT=80 INSTALL_WEBSITE_DIR="d:\Program Files"
    echo Successfully Deployed Code
    echo ######################################

    This is me trying to convert it to PowerShell:
    $InstallerFolder = "D:\DAQ\DAQ\ReleaseInstallers_$((get-date).toString('MM-dd-yyyy'))"

    function InstallDetailedQuote {
    $Arguments = @()
    $Arguments += "/quiet"
    $Arguments += "/i"
    $Arguments += "`"$InstallerFolder\DetailedQuote.Install.msi`""
    $Arguments += "INSTALL_WEBSITE_IP='10.1.54.102'"
    $Arguments += "INSTALL_WEBSITE_PORT='80'"
    $Arguments += "INSTALL_WEBSITE_DIR='d:\Program Files'"
    $Arguments += "INSTALL_VIRTUAL_PATH='Applications/AgencyWebsites'"

    StartProcess "msiexec.exe" -ArgumentList $Arguments -Wait }

    Any help you can provide me would be greatly appreciated.

    Thanks in Advance.

    ReplyDelete
  3. Chris,

    I'd love the Tivoli Client Install script please! I have 35 servers with old clients that I need to update to the corrent client and just came out of Powershell class. Please and thanks!!

    ReplyDelete

Please leave a comment; someone, anyone!