English 中文(简体)
动态形成指挥线,然后使用权力hell
原标题:Dynamically generate command-line command, then invoke using powershell
  • 时间:2011-07-06 23:06:22
  •  标签:
  • powershell

凭借权力,你可以使用“灯”;品格操作另一种应用和参数。

一个简单的例子。

$notepad =  notepad 
$fileName =  HelloWorld.txt 

# This will open HelloWorld.txt
& $notepad $fileName   

这是好的。 但是,如果我想要利用商业逻辑,以动态的方式产生指挥力,那么什么呢? 采用同样简单的例子:

$commandString = @( notepad ,  HelloWorld.txt ) -join    ;
& $commandString

我发现错误:

The term notepad HelloWorld.txt is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

在我的真实例子中,我试图积极补充或排除最后指挥线的备选方案。 我能否做到这一点?

最佳回答

Two ways to do that:

  1. www.un.org/Depts/DGACM/index_spanish.htm 脱离的论点

    各位的生动充斥着这些论点,但认为这些论点是正常的。

    $exe =  notepad 
    $argument =  "D:spaced pathHelloWorld.txt" 
    &$exe $argument
    
    #or
    notepad $argument
    

    如果你有不止一个论点,如果它与呼吁的前部分分开的话,你就应当把它变成一个阵列:

    $exe =  notepad 
    $arguments =  "D:spaced pathHelloWorld.txt" , --switch1 , --switch2 
    &$exe $arguments
    
  2. <><>Use Invoke-Expression>>

    如果所有事情都必须加以扼杀,你可以援引这种扼杀,就像它是一种正常表达。 <代码>Invoke-Expression也有iex的其他事项。

    $exp =  notepad "D:spaced pathHelloWorld.txt" 
    Invoke-Expression $exp
    

在这两种情况下,应适当引述和编排论点的内容,仿佛是在指挥线上写明。

问题回答

如果你想保持这一逻辑,以建立你的指示:

$commandString = @( notepad ,  HelloWorld.txt ) -join    

&([scriptblock]::create($commandstring))




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签