如何使用C#调用PowerShell w32tm重新同步命令?
我想在远程设备上调用w32tm重新同步,并确定它是否成功。这就是我现在正在尝试的,没有运气。我的代码中有明显的错误吗如何使用C#调用PowerShell w32tm重新同步命令?,c#,powershell,C#,Powershell,我想在远程设备上调用w32tm重新同步,并确定它是否成功。这就是我现在正在尝试的,没有运气。我的代码中有明显的错误吗 static void Main(string[] args) { using (PowerShell PowerShellInstance = PowerShell.Create()) { //// use "AddScript" to add the contents of a script file to
static void Main(string[] args)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
//// use "AddScript" to add the contents of a script file to the end of the execution pipeline.
//// use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
//Create password variable
PowerShellInstance.AddScript("$password = ConvertTo-SecureString \"mypassword\" -AsPlainText -Force;");
//Create Credential Object
PowerShellInstance.AddScript("$cred = New-Object System.Management.Automation.PSCredential (\"mycomputer\\Administrator\",$password);");
PowerShellInstance.AddScript("Invoke-Command -ComputerName minwinpc -Credential $cred -ScriptBlock {w32tm /resync /force;}");
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
Console.WriteLine(outputItem.BaseObject.GetType().FullName);
Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
}
}
Console.WriteLine("DONE");
Console.ReadLine();
}
}
static void Main(字符串[]args)
{
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
////使用“AddScript”将脚本文件的内容添加到执行管道的末尾。
////使用“AddCommand”将单个命令/cmdlet添加到执行管道的末尾。
//创建密码变量
PowerShellInstance.AddScript(“$password=converttoSecureString\'mypassword\'-AsPlainText-Force;”);
//创建凭证对象
PowerShellInstance.AddScript($cred=New Object System.Management.Automation.PSCredential(\'mycomputer\\Administrator\',$password););
AddScript(“Invoke命令-ComputerName minwinpc-Credential$cred-ScriptBlock{w32tm/resync/force;}”);
集合PSOutput=PowerShellInstance.Invoke();
foreach(PSOutput中的PSObject输出项)
{
if(outputItem!=null)
{
Console.WriteLine(outputItem.BaseObject.GetType().FullName);
Console.WriteLine(outputItem.BaseObject.ToString()+“\n”);
}
}
控制台。写入线(“完成”);
Console.ReadLine();
}
}