Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在新的Powershell.exe控制台中加载函数_Powershell - Fatal编程技术网

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

如何在新的Powershell.exe控制台中加载函数,powershell,Powershell,我有一个需要sta模式的函数(函数1)。我想从非sta模式poshconsole调用此函数。当我的个人资料中有function1时,此选项起作用 $command = "function1 'MyArguments'" powershell -sta -command $command 但是,如果配置文件中没有函数1,并且我调用 powershell -sta -noprofile -command $command 我可以通过powershell.exe调用执行多个命令吗?或者我可以移交c

我有一个需要sta模式的函数(函数1)。我想从非sta模式poshconsole调用此函数。当我的个人资料中有function1时,此选项起作用

$command = "function1 'MyArguments'"
powershell -sta -command $command
但是,如果配置文件中没有函数1,并且我调用

powershell -sta -noprofile -command $command

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

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

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

有一些处理STA的方法:

如果要调用powershell.exe(因为这显然更简单),可以先用函数加载脚本,然后执行
function1“我的参数”

powershell -sta -noprofile -command '<pathToYourScript>.ps1; function1 args'
powershell-sta-noprofile-command'.ps1;函数1 args'
我试图同时使用
-file
参数和
-command
参数,但这不起作用。

如果正在运行,则可以使用调用单元,例如:

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