如何在新的Powershell.exe控制台中加载函数

时间:2010-02-23 14:00:08

标签: powershell

我有一个需要sta模式的函数(function1)。我想从非sta模式poshconsole中调用此函数。这在我的个人资料

中有function1时有效
$command = "function1 'MyArguments'"
powershell -sta -command $command

但是当我没有在配置文件中使用function1并且我打电话

时,我怎么能这样做呢?
powershell -sta -noprofile -command $command

我可以使用powershell.exe调用执行多个命令吗?或者我可以移交一个customprofilepath?

3 个答案:

答案 0 :(得分:2)

您可以用分号(;)分隔多个命令,例如,点源包含您的函数的脚本,然后调用函数:

powershell -sta -noprofile -command ". c:\functions.ps1 ; function1 'MyArguments'"

答案 1 :(得分:1)

如何处理STA有一些方法:

如果您将调用powershell.exe(因为它显然更容易),您可以先使用该函数加载脚本,然后执行function1 'My arguments'

powershell -sta -noprofile -command '<pathToYourScript>.ps1; function1 args'

我尝试将-file参数和-command参数一起使用,但这不起作用。

答案 2 :(得分:1)

如果您正在运行PowerShell Community Extensions,则可以使用Invoke-Apartment,例如:

PS> Invoke-Apartment -Apartment STA `
                     -Expr {[Threading.Thread]::CurrentThread.ApartmentState}
STA
相关问题