这与http://www.computerperformance.co.uk/powershell/powershell_remote.htm 相关。目前,我的测试环境中的大多数机器都在 Powershell 1.0 上。在这种情况下,有没有办法让我仍然可以通过 Powershell从一台机器到另一台机器进行远程调用。我的最终动机是使用 Powershell远程启动/停止/重新启动 Windows 服务。
我想远程启用其他计算机上的powershell远程处理.远程计算机需要身份验证.我试图创建一个批处理文件来安排执行Enable-Psremoting的任务,显然我无法将它放在远程计算机中.
我遇到了WinRM服务的一些问题.它一直坚持要求"延迟启动(自动)"服务,而不仅仅是"自动"服务.
为什么?这导致我的VM出现问题(在Hyper-V上).我通过PowerShell以编程方式将它们还原,然后需要通过PowerShell远程处理来访问它们,但有时当我第一次将虚拟机联机时,WinRM服务还没有启动(它们是"完全启动",就像我可以登录它们一样).
如果我将服务设置为自动,则运行PowerShell命令winrm quickconfig表示该服务未设置为远程处理,并且坚持将服务设置回延迟启动.
在尝试打开远程PowerShell会话之前,如何确保Windows RM服务正在运行?
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) 我正在寻找使用 Invoke-Command 将 PsObject 传递到远程函数的最佳或正确方法。ConvertTo-Xml 非常适合序列化,但没有内置的反向 cmdlet。我在网上找到的解决方案都是针对具体内容的。
我实现了以下函数,这些函数适用于我测试的几个对象,但看起来相当老套。这是要咬我吗?有没有更好但仍然简单的解决方案?
function Get-TmpFileName () {...}
function Serialize-PsObject($obj) {
$tmpFile = Get-TmpFileName
Export-Clixml -InputObject $obj -Path $tmpFile
$serializedObj = Get-Content $tmpFile
Remove-Item $tmpFile
$serializedObj
}
function Deserialize-PsObject($obj) {
$tmpFile = Get-TmpFileName
$obj > $tmpFile
$deserializedObj = Import-Clixml $tmpFile
Remove-Item $tmpFile
$deserializedObj
}
Run Code Online (Sandbox Code Playgroud) 我创建了 PowerShell 脚本来在远程 Windows Server 上创建用户并添加到管理员组:
\n\n$Computer = Read-Host "Computer name:"\n$UserName = Read-Host "User name:"\n$Password = Read-Host "Password" -AsSecureString\n$AdminGroup = [ADSI]"WinNT://$Computer/Administrator,group"\n$User = [ADSI]"WinNT://$Computer/$UserName,user"\n$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -AsPlainText \xe2\x80\x93Force)\n$User.SetPassword($Cred.GetNetworkCredential().Password)\n$AdminGroup.Add($User.Path)\nRun Code Online (Sandbox Code Playgroud)\n\n它给了我以下错误:
\n\n\n检索成员“SetPassword”时发生以下异常:“\n找不到用户名。\n在 C:\\test1.ps1:7 char:18\n+ $User.SetPassword <<<< ($Cred. GetNetworkCredential().Password)\n + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException\n + ExcellentQualifiedErrorId : CatchFromBaseGetMember\n\n检索成员“Add”时发生以下异常:“指定的\n本地组不存在。 \n在 C:\\test1.ps1:8 字符:16\n+ $AdminGroup.Add <<<< ($User.Path)\n + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException\n + ExcellentQualifiedErrorId : CatchFromBaseGetMember \n\n
我成功地让winrm工作了,我能够Enter-PSSession my-machine在shell中运行并随后输入命令.但是,当我尝试运行启动远程会话的脚本时,所有后续调用都在本地计算机上运行.例如:
PS> test.ps1
Run Code Online (Sandbox Code Playgroud)
test.ps1的内容
Enter-PSSession remote-pcname
gc env:computername
Run Code Online (Sandbox Code Playgroud)
打印出local-pcname而不是remote-pcname任何想法为什么脚本文件不尊重远程会话?它肯定是成功连接的,因为当脚本完成后我返回到远程机器的shell提示符.
我试图在几个服务器上使用Powershell远程获取DLL的文件版本,并将服务器名称,DLL版本和DLL文件位置写入csv或html报告.问题是一些服务器是Win 2003和Win 2008.因此该文件可能位于例如C:\ Program Files\WinZip\WZCAB.DLL或C:\ Program Files(x86)\ WinZip\WZCAB.DLL中.该脚本将检查一个位置,如果它不存在,它将检查另一个,然后将其写出.谁能帮我吗?
只需几件事 - 这个脚本将用于访问2003年和2008年的200多台服务器或VM.2003年它需要Powershell 2.0和2008,它将及时启用远程处理.我想也许我需要利用WMI.我有两个其他脚本利用WMI获取补丁和重启时间.虽然我尝试了Ravikanth脚本(再次感谢你)并且在传递服务器的txt文件时出现以下错误 - 一个或多个计算机名称无效.如果您尝试传递Uri,请使用-ConnectionUri参数或传递Uri对象而不是字符串.因为我不会在每台服务器上启用远程处理,还有另一种方法吗?我修改了Ravikanth脚本(下面)并在本地尝试过,效果很好.当我远程尝试时,它没有.有什么想法吗?
$servers = "D:\scripts\winzip\servers.txt"
$x86Path = 'C:\Program Files (x86)\WinZip\WZCAB.DLL'
$x64Path = 'C:\Program Files\WinZip\WZCAB.DLL'
foreach ($computername in $servers){
if (Test-Path $x86Path) {
(Get-Item $x86Path | Select -ExpandProperty VersionInfo).FileVersion
} elseif (Test-Path $x64Path) {
(Get-Item $x64Path | Select -ExpandProperty VersionInfo).FileVersion
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用远程机器上执行代码invoke-command.该方法的一部分包括一个ScriptBlock参数,我感觉我没有正确地做某事.
首先,我尝试在脚本中创建一个方法,如下所示:
param([string] $filename)
function ValidatePath( $file, $fileType = "container" )
{
$fileExist = $null
if( -not (test-path $file -PathType $fileType) )
{
throw "The path $file does not exist!"
$fileExist = false
}
else
{
echo $filename found!
$fileExist = true
}
return $fileExist
}
$responseObject = Invoke-Command -ComputerName MININT-OU9K10R
-ScriptBlock{validatePath($filename)} -AsJob
$result = Receive-Job -id $responseObject.Id
echo $result
Run Code Online (Sandbox Code Playgroud)
打电话给我,我会的.\myScriptName.ps1 -filename C:\file\to\test.该脚本将执行,但不会调用该函数.
然后我想也许我应该把这个函数放到一个新的脚本中.这看起来像:
档案1:
$responseObject = Invoke-Command -ComputerName MININT-OU9K10R -ScriptBlock {
.\file2.ps1 -filename …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, …