powershell: script to start a program with parameters?
Asked Answered
R

2

3

When i run the Powershell script below i receive the error below. How do i run programs through powershell with parameters? The script will be a group policy logon.

Invoke-Expression : A positional parameter cannot be found that accepts argument '\TBHSERVER\NETLOGON\BGInfo\BGIFILE.bgi /timer:0 /s ilent /nolicprompt '. At X:\Systems\scripts\PowerShell\UpdateDesktopWithBGInfo.ps1:6 char:18 + Invoke-Expression <<<< $logonpath $ArguList + CategoryInfo : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand

$LogonPath = $env:LOGONSERVER + "\NETLOGON\BGInfo\Bginfo.exe" 
$ArguList = $env:LOGONSERVER + '\NETLOGON\BGInfo\BGIFILE.bgi /timer:0 /silent /nolicprompt '
invoke-command $LogonPath
Invoke-Expression $logonpath $ArguList
Ripple answered 9/1, 2012 at 15:47 Comment(0)
P
5

Try this:

& "\\$env:LOGONSERVER\NETLOGON\BGInfo\Bginfo.exe" "\\$env:LOGONSERVER\NETLOGON\BGInfo\BGIFILE.bgi" /timer:0 /silent /nolicprompt

If the BGIFILE.bgi reside in the same location as Bginfo.exe then you can specify only the file name:

& "\\$env:LOGONSERVER\NETLOGON\BGInfo\Bginfo.exe" BGIFILE.bgi /timer:0 /silent /nolicprompt
Pasquale answered 9/1, 2012 at 16:0 Comment(3)
Thanks, works a treat :-) I just needed to change the 2nd path to include a $envLogonserver, thought i would include the final command in-case anyone needs to run the same command. "& "$env:LOGONSERVER\NETLOGON\BGInfo\Bginfo.exe" "$env:LOGONSERVER\NETLOGON\BGInfo\default.bgi" /timer:0 /silent /nolicprompt"Ripple
@Ripple - If it worked, you should Accept the answer. Click on the tick next to the answer. ( and work on accepting answers for your other questions)Dare
@Ripple You can shorten the path if the .bgi file resides in the same location as bginfo.exe. I will update my answerPasquale
E
7

Invoke-Command is best suited for running commands remotely. As Shay points out you can use the ampersand & to tell PowerShell to execute something locally just like the cmd.exe shell.

In order to make Invoke-Command work you would need to do something like this:

$program = "C:\windows\system32\ping.exe"
$programArgs = "localhost", "-n", 1
Invoke-Command -ScriptBlock { & $program $programArgs }

Notice the use of the the ampersand in the script block. So if you are running a command locally just use the ampersand as Shay's example shows.

Expendable answered 9/1, 2012 at 16:41 Comment(1)
Thanks for the info. Im learning powershell so all the information im soking up all the info you guys can throw at me :-)Ripple
P
5

Try this:

& "\\$env:LOGONSERVER\NETLOGON\BGInfo\Bginfo.exe" "\\$env:LOGONSERVER\NETLOGON\BGInfo\BGIFILE.bgi" /timer:0 /silent /nolicprompt

If the BGIFILE.bgi reside in the same location as Bginfo.exe then you can specify only the file name:

& "\\$env:LOGONSERVER\NETLOGON\BGInfo\Bginfo.exe" BGIFILE.bgi /timer:0 /silent /nolicprompt
Pasquale answered 9/1, 2012 at 16:0 Comment(3)
Thanks, works a treat :-) I just needed to change the 2nd path to include a $envLogonserver, thought i would include the final command in-case anyone needs to run the same command. "& "$env:LOGONSERVER\NETLOGON\BGInfo\Bginfo.exe" "$env:LOGONSERVER\NETLOGON\BGInfo\default.bgi" /timer:0 /silent /nolicprompt"Ripple
@Ripple - If it worked, you should Accept the answer. Click on the tick next to the answer. ( and work on accepting answers for your other questions)Dare
@Ripple You can shorten the path if the .bgi file resides in the same location as bginfo.exe. I will update my answerPasquale

© 2022 - 2024 — McMap. All rights reserved.