Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Having issues with Invoke-Item (Powershell)

Hi guys, hope you are well and can assist.

Im very new to Powershell but want to learn.

I want to run the following command which has switches in it.

Ill place a working copy of it below, which is a bat file:

======================================= install.bat
"C:\Program Files\test\AeXNSC.exe" /install ns=server.test.net nsweb=http://webtest/altiris/ NOSTARTMENU NOTRAYICON /NOADDREMOVE /s

======================================= install.ps1
$cmdagent = "C:\Program Files\test\AeXNSC.exe /install ns=server.test.net nsweb=http://webtest/altiris/ NOSTARTMENU NOTRAYICON /NOADDREMOVE /s"
Invoke-Item $cmdagent

Im not sure what is wrong with my install.ps1, but I suspect it has to do with the quotes and switches.

Any help greatly appreciated.

SOLUTION
Avatar of Gitcho
Gitcho

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

Hi Gitcho,

Thanks for your help.

It doesnt fail, but does not execute.

Likewise, if I do the following:

$cmdagent = "c:\windows\notepad.exe"
icm {$cmdagent}

It simply outputs the line.
Avatar of Chris Dent

It's because "c:\windows\notepad.exe" is a string not a script block, if you put that in a script block ( { ... } ) and invoke it then all you'll get is the string.

You can either:

1. Make it a real script block

# This command will not execute immediately, it prepares the command
$cmdagent = { c:\windows\notepad.exe }
# Execute it
Invoke-Command $cmdagent

2. Call it with the call operator:

# Create a string containing the command to run
$cmdagent = "c:\windows\notepad.exe"
# Call it
& $cmdagent

3. Use Invoke-Expression

# Create a string containing the command to run
$cmdagent = "c:\windows\notepad.exe"
# Invoke the string as an expression
Invoke-Expression $cmdagent

Only the last two will convert a string into an executable command.

HTH

Chris
Hi Chris, that works with regards to:

# Create a string containing the command to run
$cmdagent = "c:\windows\notepad.exe"
# Invoke the string as an expression
Invoke-Expression $cmdagent


So, should the following work, with the double quotes embracing the entire line?, or do you think the spaces and switches are mucking things up?

$cmdagent = "C:\Program Files\test\AeXNSC.exe /install ns=server.test.net nsweb=http://webtest/altiris/ NOSTARTMENU NOTRAYICON /NOADDREMOVE /s"
Invoke-Command $cmdagent
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much Chris.