我如何远程运行PowerShellfunction?

使用powershell,我打算在远程主机上运行许多function来收集信息。

下面是一个例子,通过运行一个名为getcontentfile的函数来远程检索文件的内容,该参数作为远程主机的名字:

function getcontentfile { [CmdletBinding()] param($hostname) $info = Get-Content "C:\fileinfo.xml" write-host $info } 

此函数应该将有关远程主机的信息返回给PowerShell的本地实例。 我怎样才能修改这个脚本来做到这一点?

首先要返回信息不要使用Write-Host (总是这样,除非你真的只想拥有颜色只能在本地交互操作)。

使输出成为函数的返回值:

 function getcontentfile { [CmdletBinding()] param($hostname) $info = Get-Content "C:\fileinfo.xml" $info } 

其次:在目标系统上启用PSH远程处理:请参阅Enable-PSRemoting的帮助 。

第三:远程运行命令:

 Invoke-Command -computer comp1,comp2 -ScriptBlock { Get-Content "C:\fileinfo.xml" } 

这将返回两台计算机上的文件内容,以分离结果-AsJob将返回作业对象,然后可以使用Job cmdlet单独查询作业对象(请参阅gcm -noun job列表,请注意Receive-Job以获取一份工作的结果)。

您可以在远程计算机上运行本地加载的function:

 Invoke-Command -ComputerName Comp1 -cred $cred -ScriptBlock ${function:get-contentfile } -argumentlist "ParameterA", "ParameterB" 

您的第一个选项是启用Powershell 2.0远程处理 。

就我个人而言,虽然它的function非常强大,但对于远程处理并不是很有趣,所以我编写了一个脚本来使用WMI,使用cmd.exe创build一个进程,然后将stdout和stderr传输到日志文件,然后您可以读取。

该脚本在远程计算机上get-content \\remotecomputer\c$\remoteExec.log其日志文件,因此您可以简单地: get-content \\remotecomputer\c$\remoteExec.log来读取它。

 <# .SYNOPSIS Remotely executes a command and logs the stdout and stderr to a file on the remote computer. .DESCRIPTION This script accepts three parameters (one optional) and executes a program on a remote computer. It will verify connectivity and optionally (verifyPath) the existence of the program to be executed. If either verifications fail, it will not attempt to create the process on the remote computer. .EXAMPLE .\remoteExec.ps1 -program "dir" -args "c:\" -computerName "SEANC" .EXAMPLE .\remoteExec "C:\Windows\SysWOW64\msiexec.exe" "/ic:\a.msi /passive /log c:\a-install.log" SEANC C:\Windows\Temp\remote.log -verifyPath .PARAMETER computerName The name of the computer on which to create the process. .PARAMETER program The command to run on the remote computer. .PARAMETER args The command arguments. .PARAMETER log The file to which the stderr and stdout generated by the command will be redirected. This is a local path on the remote computer. .PARAMETER verifyPath Switch to enforce path verification. #> param( [parameter(Mandatory=$true)] [string]$program, [parameter(Mandatory=$false)][string]$args = "", [parameter(Mandatory=$true)] [string]$computerName, [parameter(Mandatory=$false)][string]$log = "C:\remoteExec.log", [parameter(Mandatory=$false)][switch]$verifyPath = $false ) if (-not (Test-Connection $computerName -Quiet -Count 1)) { return Write-Error "Unable to connect to $computerName." } if ($verifyPath -and (-not (Test-Path \\$computerName\$($program.replace(":","$")) -PathType Leaf))) { return Write-Error "Path $program does not exist on $computerName." } try { $remoteWmiProcess = [wmiclass]"\\$computerName\root\cimv2:win32_process" $remoteProcess = $remoteWmiProcess.create( "cmd.exe /c `"$program $args > $log 2>&1`"" ) } catch { return Write-Error ("Unable to create process through WMI."); } if ($remoteProcess.returnValue -ne 0) { return Write-Error ("FAILED on $computerName with return code: " + $remoteProcess.returnValue) } else { return ("Successful trigger on $computerName; returned: " + $remoteProcess.returnValue) } 

编辑:在这个例子中,脚本被称为remoteExec.ps1,我用它来创build一个远程PowerShell进程,并运行一个命令(提问者试图做什么):

.\remoteExec.ps1 -program "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -verifyPath -computerName "computer1" -args "-command Get-ChildItem C:\"

然后我可以阅读日志:

Get-Content \\computer1\C$\remoteExec.log

简短的答案是你不能。

通常,您可以使用Invoke-Command在远程计算机上运行命令。 该命令可以是脚本块或PowerShell脚本。 这些方法都不能使用本地计算机上存在的variables或函数,也就是说无法提前加载函数以使其可以在远程计算机上访问。

你必须做的是让他们在远程计算机上,然后从那里加载它们。 我们创build一个共享共享并将我们的PowerShell脚本部署到该共享中。