Tôi đang sử dụng API System.Management.Automation để gọi các tập lệnh PowerShell là ứng dụng C# WPF. Trong ví dụ sau, làm thế nào bạn sẽ thay đổi thư mục bắt đầu ($ PWD) để nó thực hiện foo.ps1 từ C: \ scripts \ thay vì vị trí của .exe nó được gọi là từ?Đặt thư mục bắt đầu khi gọi Powershell từ .NET?
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
using (Pipeline pipeline = runspace.CreatePipeline())
{
pipeline.Commands.Add(@"C:\scripts\foo.ps1");
pipeline.Invoke();
}
runspace.Close();
}
Có một giải pháp cho các cuộc gọi từ xa? RemoteSessionStateProxy.Path đưa ra một PSNotSupportedException. – Rookian
@Rookian: Chạy lệnh ghép ngắn 'Set-Location' có thể là lựa chọn duy nhất của bạn trong trường hợp đó. –