0

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?

2
  • Yes it does exists in D drive, that is why the first line of the output is - "D:\Veritas\Netbackup\bin\admincmd is found in D drive" Commented Nov 5, 2014 at 17:34
  • No sir, even including bpimagelist in TestPath doesnt works. For test purpose currently I'm trying to invoke fsutil.exe located at C:\Windows\syste32 with below code but even this doesnt works. Invoke-Expression "volume diskfree "$y":" Any suggestions please? Commented Nov 5, 2014 at 17:36

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'll try Start-Process tomorrow when I reach office in Prod environment. Meanwhile at home, I'm trying to invoke simple fsutil.exe with a variable drive name inside Invoke-Expression which now works with the helpful reply of arco444. But this is not my requirement. I want to invoke bpimagelist which has few switches in the expression which I'm not sure will work or not.I'll try tomorrow and post here. THanks
0

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"

7 Comments

Even this doesn't works. For a simple test I'm trying to invoke windows fsutil file: Invoke-Expression "fsutil volume diskfree "$y":" But even this doesnt works. Error below: Invoke-Expression : A positional parameter cannot be found that accepts argument 'C:'.
Be VERY careful with that command because of the colon. It should be: Invoke-Expression "fsutil volume diskfree $($y):"
In fact, I get the same error when I try the command you pasted, so you have it wrong. I would test with the full command in your question before you can be sure it won't work
THanks Arco444, this indeed works with NO error. So what the catch I figured out is , In case a variable comes inside Invoke-Expression, we need to use $ twice to make shell understand that its a variable. Am I right?
Yes, spot on. And I'd even missed that in my own answer..! Have updated to reflect that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.