首页 文章

powershell:用参数启动程序的脚本?

提问于
浏览
2

当我在下面运行Powershell脚本时,我收到以下错误 . 如何通过参数PowerShell运行程序?该脚本将是一个组策略登录 .

Invoke-Expression:找不到接受参数'\ TBHSERVER \ NETLOGON \ BGInfo \ BGIFILE.bgi / timer:0 / s ilent / nolicprompt'的位置参数 . 在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

2 回答

  • 7

    试试这个:

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

    如果BGIFILE.bgi与Bginfo.exe位于同一位置,则只能指定文件名:

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

    Invoke-Command 最适合远程运行命令 . 正如Shay指出的那样,你可以使用&号 & 告诉PowerShell在本地执行某些内容,就像cmd.exe shell一样 .

    为了使 Invoke-Command 工作,你需要做这样的事情:

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

    注意在脚本块中使用&符号 . 因此,如果您在本地运行命令,请使用&符号,如Shay的示例所示 .

相关问题