Tag: powershell

使用PowerShell中的WinRM连接到远程服务器失败

我试图从我的计算机上运行PowerShell代码到我的计算机上的vm,但我不断收到此错误: 连接到远程服务器失败,并显示以下错误消息:WinRM客户端无法处理该请求。 如果身份validation方案与Kerberos不同,或者客户端计算机未加入域,则必须使用HTTPS传输,或者必须将目标计算机添加到TrustedHosts配置设置。 使用winrm.cmd配置TrustedHosts。 请注意,TrustedHosts列表中的计算机可能未经过身份validation。 您可以通过运行以下命令获取有关该信息的更多信息:winrm help config。 有关详细信息,请参阅about_Remote_Troubleshooting帮助主题。 我的代码: string runasUsername = @”\aaa”; string runasPassword = “aaa”; SecureString ssRunasPassword = new SecureString(); foreach (char x in runasPassword) ssRunasPassword.AppendChar(x); PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword); var connInfo = new WSManConnectionInfo(new Uri(“http://10.0.5.35/PowerShell”), “http://schemas.microsoft.com/powershell/Microsoft.Exchange”,credentials); connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; var runspace = RunspaceFactory.CreateRunspace(connInfo); var domainName = “domainName.COM”; var password […]

从C#调用powershell cmdlet

我正在尝试学习如何从C#调用PS cmdlet,并且遇到了PowerShell类。 它适用于基本用途,但现在我想执行此PS命令: Get-ChildItem | where {$_.Length -gt 1000000} 我试过通过powershell类来构建它,但我似乎无法做到这一点。 到目前为止这是我的代码: PowerShell ps = PowerShell.Create(); ps.AddCommand(“Get-ChildItem”); ps.AddCommand(“where-object”); ps.AddParameter(“Length”); ps.AddParameter(“-gt”); ps.AddParameter(“10000”); // Call the PowerShell.Invoke() method to run the // commands of the pipeline. foreach (PSObject result in ps.Invoke()) { Console.WriteLine( “{0,-24}{1}”, result.Members[“Length”].Value, result.Members[“Name”].Value); } // End foreach. 我跑这个时总是遇到exception。 是否可以像这样运行Where-Object cmdlet?