Brite Roy
Brite Roy

Reputation: 425

Getting Error in Powershell script while invoking exe file

I want to invoke an exe file from a drive having a variable drive name. The code first searches the path in every drive of the system and once found it enters the If condition where I want to invoke a command -bpimagelist.

Below is the code which gives an unusual error.

for($pat=67 ;$pat -le 87 ;$pat++)
{
 $y = [char]$pat;
$path = $y+":\Veritas\Netbackup\bin\admincmd"
If((Test-Path $path))
{
Write-Host $path "is found in" $y "drive"
Invoke-Item $y":\Veritas\Netbackup\bin\admincmd\bpimagelist -client abc.ge.com -d 11/01/2014 -U"
break
}
Else
{
continue
}
}

ERROR :

PS C:\Documents and Settings\abala\Desktop> .\veritasscript.ps1
D:\Veritas\Netbackup\bin\admincmd is found in D drive
Invoke-Item : Cannot find path 'C:\Veritas\Netbackup\bin\admincmd\bpimagelist -
client hclinnobpm01.jnj.com -d 11\01\2014 -U' because it does not exist.
At C:\Documents and Settings\admin_broy5\Desktop\veritasscript.ps1:8 char:17
+      Invoke-Item  <<<< $y":\Veritas\Netbackup\bin\admincmd\bpimagelist -clien
t hclinnobpm01.jnj.com -d 11/01/2014 -U"

=================================================

I'm not sure why the Error shows C:\Veritas when the current value of $y is D and also in the Write-Host line it prints as path found in D drive. Can someone please suggest a method to invoke an exe file above?

Upvotes: 0

Views: 332

Answers (2)

arco444
arco444

Reputation: 22861

You're using the wrong cmdlet when you use Invoke-Item

Change it to Invoke-Expression or iex:

Invoke-Expression "$($y):\Veritas\Netbackup\bin\admincmd\bpimagelist -client abc.ge.com -d 11/01/2014 -U"

Upvotes: 0

Paul
Paul

Reputation: 5871

A better way to do this would be to use Start-Process instead of one of the Invoke commands.

Here is an example:

Start-Process "$y:\Veritas\Netbackup\bin\admincmd\bpimagelist" -argumentList @("-client abc.ge.com","-d 11/01/2014","-U")

If this throws the same error you should check the machine if bpimagelist really exists.

For more info on running executables with powershell check out the Technet Wiki

Upvotes: 1

Related Questions