我試圖以編程方式運行HPC cmdlet,以便在遠程計算機上更改HPC安裝憑證。如果在本地運行cmdlet,這是非常簡單的:以編程方式在C#中傳遞ScriptBlock中的參數對象(PSCredential)
Runspace rs = GetPowerShellRunspace();
rs.Open();
Pipeline pipeline = rs.CreatePipeline();
PSCredential credential = new PSCredential(domainAccount, newPassword);
Command cmd = new Command("Set-HpcClusterProperty");
cmd.Parameters.Add("InstallCredential", credential);
pipeline.Commands.Add(cmd);
Collection<PSObject> ret = pipeline.Invoke();
但是,如果我想要做同樣的事情與遠程PowerShell,我需要運行調用命令,並在憑證傳遞到命令裏面的腳本塊。我怎樣才能做到這一點?它可能是這個樣子,但我需要在憑證傳遞爲綁定到腳本塊而不是字符串內InstallCredential參數的對象:
Pipeline pipeline = rs.CreatePipeline();
PSCredential credential = new PSCredential(domainAccount, newPassword);
pipeline.Commands.AddScript(string.Format(
CultureInfo.InvariantCulture,
"Invoke-Command -ComputerName {0} -ScriptBlock {{ Set-HpcClusterProperty -InstallCredential {1} }}",
nodeName,
credential));
Collection<PSObject> ret = pipeline.Invoke();