2

件名が平凡すぎるように見え、そのような質問に対処する投稿が多数あることは理解しています。私が探していたものが見つからなかったので、この投稿をしました。

リモート マシン (マシン B) で実行されるように、マシン A から powershell ファイルを呼び出しています。

// コード スニペットは次のとおりです。

 Runspace runspace = RunspaceFactory.CreateRunspace();
 runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();
string scripttext = "$secpasswd = ConvertTo-SecureString '222_bbbb' -AsPlainText –Force";
string scripttext1 = "$mycreds = New-object -typename System.Management.Automation.PSCredential('TS-TEST-09\\Administrator',$secpasswd)";
string scripttext2  = "$s = New-PSSession -ComputerName TS-TEST-09 -Credential $mycreds";
**string scripttext5 = "Invoke-Command -Session $s -FilePath 'helper.ps1' | out-null";**

pipeline.Commands.AddScript(scripttext);
pipeline.Commands.AddScript(scripttext1);
pipeline.Commands.AddScript(scripttext2);
pipeline.Commands.AddScript(scripttext5);

Collection<PSObject> results = pipeline.Invoke();

現在行内 string scripttext5 = "Invoke-Command -Session $s -FilePath 'helper.ps1' | out-null"; 処理のために、いくつかのパラメーター (たとえば、c# 環境のユーザー名: useralias) をこの helper.ps1 ファイルに渡す必要があります。誰かがそうする正しい方法に私を導くことができますか?

TIA、マニッシュ

4

2 に答える 2

0

わかりましたので、ここに私のために働いた解決策があります。

キースのソリューションから一部を抜粋しています。トリックは、を使用してC#から変数を公開することです

runspace.SessionStateProxy.SetVariable("PSuseralias", this.useralias);

$PSuseralias を

string scripttext5 = "Invoke-Command -Session $s -FilePath 'helper.ps1' -Args  $PSuseralias;

これでうまくいきます。

于 2012-01-25T14:07:12.207 に答える
0

ユーザー名とユーザー エイリアスがローカル マシンからのものである場合は、次のようにできます。

string scripttext5 = "Invoke-Command -Session $s -FilePath 'helper.ps1' -Args " +
                      username + "," + useralias + " | Out-Null";

これは、helper.ps1 ファイルが少なくとも 2 つのパラメーターを取ることを前提としています。その場合、C# 変数の値がusername最初useraliasの 2 つのパラメーターに割り当てられます。

于 2012-01-24T17:59:27.050 に答える