要从远程计算机在计算机上运行powershell命令,我们必须将远程计算机添加到主机的可信主机列表中.
我使用以下命令将机器A添加到机器B的可信主机:
winrm set winrm/config/client ‘@{TrustedHosts="machineA"}’
Run Code Online (Sandbox Code Playgroud)
如何添加更多机器说机器C,机器D到机器B的可信主机列表?
我能够使用此链接在亚马逊S3中创建一个桶.
我使用以下代码创建一个存储桶:
resource "aws_s3_bucket" "b" {
bucket = "my_tf_test_bucket"
acl = "private"
}
Run Code Online (Sandbox Code Playgroud)
现在我想在桶内创建文件夹,比方说Folder1.
我找到了创建S3对象的链接.但这有一个强制性参数source.我不确定这个值有什么,因为我的意图是在S3存储桶中创建一个文件夹.
我尝试使用以下代码从C#运行脚本localwindows.ps1:
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machineName", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
String file = "C:\\localwindows.ps1";
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(file);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
但是获得异常:'术语'C:\ localwindows.ps1'不被识别为cmdlet,函数,脚本文件或可操作程序的名称.
所以我尝试了以下方法:
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machineName", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
PSCommand new1 = new PSCommand();
String …Run Code Online (Sandbox Code Playgroud) 我使用powershell命令来执行脚本和cmdlet.因此,在执行cmdlet时,我使用了powershell.invoke,在执行脚本时,我使用了pipeline.invoke方法.我想知道System.Management.Automation.pipeline.invoke()方法和System.Management.Automation.Runspaces.powershell.invoke()方法之间是否有任何区别.
对于交换,uri使用的连接是:
http://machineName/powershell
Run Code Online (Sandbox Code Playgroud)
WSManConnectionInfo对象按以下方式创建:
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://machineName/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential)
Run Code Online (Sandbox Code Playgroud)
要连接到Windows,使用的是uri
http://machineName:5985/wsman
Run Code Online (Sandbox Code Playgroud)
WSManConnectionInfo对象按以下方式创建:
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://machineName:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credential);
Run Code Online (Sandbox Code Playgroud)
为什么交换和窗口的连接uri有区别?
machineName 尝试通过以下方式使用 invoke-command 执行脚本:
Invoke-command -filepath C:\scripts\GettMembers.ps1 -computername "machinename" -credential $Getcredential
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Connecting to remote server failed with the following error message : The WinRM client cannot process the request because the server name cannot be resolved. For more information, see the about_Remote_Troubleshooting Help topic.
Run Code Online (Sandbox Code Playgroud)
但我可以使用以下命令将计算机添加到本地计算机的受信任主机:
winrm set winrm/config/client `@{TrustedHosts="machineName"}'
Run Code Online (Sandbox Code Playgroud) 我尝试在同一台机器上使用C#在powershell中执行AD命令行开关.这很好用.
static void Main(string[] args)
{
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "activedirectory" });
Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
Command myCommand = new Command("Get-ADUser");
myCommand.Parameters.Add("Filter", "sAMAccountName -eq 'user1'");
//myCommand.Parameters.Add("IncludeDeletedObjects");
pipeLine.Commands.Add(myCommand);
//Command restoreCommand = new Command("Restore-ADObject");
//pipeLine.Commands.Add(restoreCommand);
Console.WriteLine("Before Invoke");
Collection<PSObject> commandResults = pipeLine.Invoke();
Console.WriteLine("After Invoke");
foreach (PSObject cmdlet in commandResults)
{
//Console.WriteLine("Inside foreach");
string cmdletName = cmdlet.BaseObject.ToString();
System.Diagnostics.Debug.Print(cmdletName);
Console.WriteLine(cmdletName);
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
但是在尝试使用invoke命令远程运行相同的命令时,它会给出错误 The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, …