在家庭灯光控制应用上花费数小时。当我在powershell中使用以下代码时,cmd.exe命令会从远程计算机上获取并执行它以关闭指示灯。这部分正在运作。
alllightsoff.ps1
$options=New-PSSessionOption -SkipCACheck -SkipCNCheck
Enter-PSSession -ComputerName HTPC -UseSSL -SessionOption $options
& "C:\Program Files (x86)\Home Control\HarmonyHubControl\Release\x86\HarmonyHubControl.exe" 10.90.1.8 issue_device_command 42458302 Light7
& "C:\Program Files (x86)\Home Control\HarmonyHubControl\Release\x86\HarmonyHubControl.exe" 10.90.1.8 issue_device_command 42458303 LightOff
Exit-PSSession
现在我有一个C#脚本,我之前在使用ssh的linux机器上工作了。当我按下按钮时,这段代码被执行了。
using (var ssh = new SshClient(CreateConnectionInfo()))
{
ssh.Connect();
ssh.RunCommand("/home/iiidefconiii/HarmonyHubControl/HarmonyHubControl 10.90.1.8 issue_device_command 42458302 Light7");
ssh.RunCommand("/home/iiidefconiii/HarmonyHubControl/HarmonyHubControl 10.90.1.8 issue_device_command 42458303 LightOff");
ssh.Disconnect();
}
我搬到了Windows计算机而不是ssh,linux。所以我需要从该按钮中抛出powershell代码。
using System.Management.Automation;
应用了引用,所以现在应该没问题。
我发现下面的代码完成了这项工作,但我不想为每个命令创建* .ps1文件,而是我想直接将应用程序本身的powershell命令
private void Sleep_Click(object sender, EventArgs e)
{
PowerShell ps = PowerShell.Create();
ps.AddScript(@"D:\Desktop\alllightsoff.ps1");
ps.Invoke();
}
如果我插入
private void Sleep_Click(object sender, EventArgs e)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
param([string]$command)
$options = New - PSSessionOption - SkipCACheck - SkipCNCheck
Enter - PSSession - ComputerName HTPC - UseSSL - SessionOption $options
cmd.exe / c "C:\Program Files (x86)\Home Control\HarmonyHubControl\Release\x86\HarmonyHubControl.exe" 10.90.1.8 issue_device_command 42458302 $command
Exit - PSSession
}
}
我也得到错误
答案 0 :(得分:0)
参数化您的脚本:
param([string]$command)
$options=New-PSSessionOption -SkipCACheck -SkipCNCheck
Enter-PSSession -ComputerName HTPC -UseSSL -SessionOption $options
cmd.exe /c "C:\Program Files (x86)\Home Control\HarmonyHubControl\Release\x86\HarmonyHubControl.exe" 10.90.1.8 issue_device_command 42458302 $command
Exit-PSSession
将命令的参数传递给脚本。