我正在使用C#代码运行一个PowerShell脚本。
txtLogs.AppendText($"Initializing bootstrapping process for {padName}");
/*
* -x : administrator username
* -P : administrator password
* --msi-url : URL location of the MSI file for the chef-client within L3
* -r : Role that the user run-list should execute on bootstrap execution
* -N : Node name that should dynamically be assigned to each server within the L3 network
*/
string script = @"knife bootstrap windows winrm xx.xx.xx"
+ " -x " + userName
+ " -P " + passWord
+ " --msi-url " + msiLocation
+ " -r 'role[deploy]'"
+ " -N " + padName;
ps.AddScript(script);
try
{
Collection<PSObject> PSOutput = ps.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{ .......
出于某种原因,似乎脚本在文本实际上被附加到文本框之前运行。为什么会这样?它都在同一个线程中运行。
编辑:我找到了一种解决方法,不确定它是否是最佳方式:
Dispatcher.Invoke(() => { }, DispatcherPriority.Background);