Aim*_*mar 7 remote-access powershell
使用powershell,我计划在远程主机上运行许多函数来收集信息。
下面是一个示例,只需运行一个名为 getcontentfile 的函数并将参数作为远程主机的名称即可远程检索文件的内容:
function getcontentfile
{
[CmdletBinding()]
param($hostname)
$info = Get-Content "C:\fileinfo.xml"
write-host $info
}
Run Code Online (Sandbox Code Playgroud)
此函数应将有关远程主机的信息返回到 PowerShell 的本地实例。我怎样才能修改这个脚本来做到这一点?
您的第一个选择是启用Powershell 2.0 Remoting。
就我个人而言,我对远程处理并不感兴趣,尽管它很强大,所以我编写了一个脚本来使用 WMI,使用 cmd.exe 创建一个进程,然后将 stdout 和 stderr 通过管道传输到一个日志文件,然后您可以读取该日志文件。
该脚本将其日志文件保留在远程计算机上,因此您可以简单地: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" "/i c:\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)
}
Run Code Online (Sandbox Code Playgroud)
编辑:在本例中,脚本称为remoteExec.ps1,我用它来创建远程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
| 归档时间: |
|
| 查看次数: |
10890 次 |
| 最近记录: |