Powershell-使用WMI将文件复制到远程主机并执行Install exe [英] Powershell - Copying File to Remote Host and Executing Install exe using WMI

查看:152
本文介绍了Powershell-使用WMI将文件复制到远程主机并执行Install exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这是我的代码。安装文件确实会复制到远程主机。但是,WMI部分不会安装.exe文件,并且不会返回任何错误。也许这是WMI的语法错误?有没有办法让PsExec静默运行安装程序?再次感谢您提供的所有帮助。对于造成混乱的情况,我们深表歉意。

EDITED: Here is my code now. The install file does copy to the remote host. However, the WMI portion does not install the .exe file, and no errors are returned. Perhaps this is a syntax error with WMI? Is there a way to just run the installer silently with PsExec? Thanks again for all the help sorry for the confusion:

#declare params
param (
[string]$finalCountdownPath = "",
[string]$slashes = "\\",  
[string]$pathOnRemoteHost = "c:\temp\",
[string]$targetJavaComputer = "",
[string]$compname = "",
[string]$tempPathTarget = "\C$\temp\"
)

# user enters target host/computer
$targetJavaComputer = Read-Host "Enter the name of the computer on which you wish to install Java:"
[string]$compname = $slashes + $targetJavaComputer
[string]$finalCountdownPath = $compname + $tempPathTarget
#[string]$tempPathTarget2 = 
#[string]$finalCountdownPath2 = $compname + $

# say copy install media to remote host
echo "Copying install file and running installer silently please wait..."

# create temp dir if does not exist, if exist copy install media
# if does not exist create dir, copy dummy file, copy install media
# either case will execute install of .exe via WMII
#[string]$finalCountdownPath = $compname + $tempPathTarget;
if ((Test-Path -Path $finalCountdownPath) )
    {
    copy c:\hdatools\java\jre-7u60-windows-i586.exe $finalCountdownPath 
    ([WMICLASS]"\\$targetJavaComputer\ROOT\CIMV2:win32_process").Create("cmd.exe /c c:\temp\java\jre-7u60-windows-i586.exe /s /v`" /qn")
    }
else {
    New-Item -Path $finalCountdownPath -type directory -Force
    copy c:\hdatools\dummy.txt $finalCountdownPath
    copy "c:\hdatools\java\jre-7u60-windows-i586.exe" $finalCountdownPath
    ([WMICLASS]"\\$targetJavaComputer\ROOT\CIMV2:win32_process").Create("cmd.exe /c c:\temp\java\jre-7u60-windows-i586.exe /s /v`" /qn")
    }


推荐答案

我试图获取$ Job = Invoke-命令-Session $ Session -Scriptblock $ Script,允许我在其他服务器上复制文件,因为我需要从运行它的服务器上卸载文件。我正在使用PowerShell Copy-Item来执行操作t。 但是正在运行的PowerShell脚本会等到文件复制完毕后返回。

I was trying to get $Job = Invoke-Command -Session $Session -Scriptblock $Script to allow me to copy files on a different server, because I needed to off load it from the server it was running from. I was using the PowerShell Copy-Item to do it. But the running PowerShell script waits until the file is done copying to return.

我希望它占用尽可能少的资源在运行Powershell的服务器上启动另一台服务器上的进程以复制文件。我尝试使用各种其他方案,但是它们没有用,或者我需要它们起作用。 (似乎对我来说太过随意或太复杂了。)也许其中一些可以奏效?但是我找到了自己喜欢的最适合我的解决方案,这很容易。 (除非尚未设置某些后端配置,否则可能需要。)

I want it to take as little resources as possible on the server that the powershell is running to spawn off the process on another server to copy the file. I tried to user various other schemes out there, but they didn't work or the way I needed them to work. (Seemed kind of kludgey or too complex to me.) Maybe some of them could have worked? But I found a solution that I like that works best for me, which is pretty easy. (Except for some of the back end configuration that may be needed if it is is not already setup.)

背景:
我正在运行一个SQLServer Job,它调用Powershell来运行一个脚本,该脚本备份数据库,复制备份文件并删除旧的备份文件,并将参数传递给该脚本。我们的服务器配置为允许PowerShell运行,并在具有SQL Server Admin和Active Directory帐户中dbo特权的预设置用户帐户下运行,以使其也可以看到我们网络上的各个位置。

Background: I am running a SQLServer Job which invokes Powershell to run a script which backups databases, copies backup files, and deletes older backup files, with parameters passed into it. Our server is configured to allow PowerShell to run and under the pre-setup User account with SQL Server Admin and dbo privileges in an Active Directory account to allow it to see various places on our Network as well.

但是我们不希望它从主服务器上夺走资源。将要运行的PowerShell脚本将备份数据库日志文件,然后使用另一个服务器异步复制文件本身,而不使SQL Server Job / PowerShell等待它。我们希望它在备份后立即发生。

But we don't want it to take the resources away from the main server. The PowerShell script that was to be run would backup the database Log file and then use the another server to asynchronously copy the file itself and not make the SQL Server Job/PowerShell wait for it. We wanted it to happen right after the backup.

这是我使用Windows Integrate Security使用WMI的新方法:

$ComputerName = "kithhelpdesk"
([Wmiclass]'Win32_Process').GetMethodParameters('Create')
Invoke-WmiMethod -ComputerName RemoteServerToRunOn -Path win32_process -Name create -ArgumentList 'powershell.exe -Command "Copy-Item -Path \\YourShareSource\SQLBackup\YourDatabase_2018-08-07_11-45.log.bak -Destination \\YourShareDestination\YourDatabase_2018-08-07_11-45.log.bak"'

这是我使用凭证传递的新方法,并构建arg列表变量:

$Username = "YouDomain\YourDomainUser"
$Password = "P@ssw0rd27"   
$ComputerName = "RemoteServerToRunOn"
$FromFile = "\\YourShareSource\SQLBackup\YourDatabase_2018-08-07_11-45.log.bak"
$ToFile = "\\YourShareDestination\SQLBackup\YourDatabase_2018-08-07_11-45.log.bak"
$ArgumentList = 'powershell.exe -Command "Copy-Item -Path ' + $FromFile + ' -Destination ' + $ToFile + '"'
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord
([Wmiclass]'Win32_Process').GetMethodParameters('Create')
Invoke-WmiMethod -ComputerName $ComputerName -Path win32_process -Name create -ArgumentList $ArgumentList -Credential $Cred 

我们认为以上一项是首选的。

We think that this above one is the preferred one to use.

您还可以运行一个特定的Powershell,该Shell将执行您想要的操作(甚至将参数传递给它):

Invoke-WmiMethod -ComputerName RemoteServerToRunOn -Path win32_process -Name create -ArgumentList 'powershell.exe -file "C:\PS\Test1.ps1"'

此示例可以更改为将参数传递给Test1.ps1 PowerShell脚本来使它更加灵活和可重复使用。而且您可能还想传递一个凭据,就像我们在上面的上一个示例中使用的那样。

This example could be changed to pass in parameters to the Test1.ps1 PowerShell script to make it more flexible and reusable. And you may also want to pass in a Credential like we used in a previous example above.

帮助配置WMI:


此工作的主要要旨来自: https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/invoke-wmimethod?view=powershell-5.1

但它可能还需要使用以下命令进行WMI配置:

But it may have also needed WMI configuration using:

https://com.com / hc / zh-CN / articles / 202447926-如何配置Windows-远程PowerShell访问非特权用户帐户?flash_digest = bec1f6a29327161f08e1f2db77e64856b433cb5a

https://docs.microsoft.com/zh-CN/powershell/module/microsoft .powershell.core / enable-psremoting?view = powershell-5.1

Powershell New-PSSession访问被拒绝-管理员帐户

https://docs.microsoft.com/en -us / powershell / module / microsoft.powershell.management / invoke-wmimethod?view = powershell-5.1 (我曾经获得过如何调用Invoke-WmiMethod的方法)。
https://docs.microsoft.com/zh-cn/powershell/scripting/core-powershell/console/powershell.exe-command-line-help?view=powershell-6 (我曾经使用命令行语法)

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/invoke-wmimethod?view=powershell-5.1 (I used to get how to call Invoke-WmiMethod). https://docs.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help?view=powershell-6 (I used to get syntax of command line)

我没有使用过,但是可能有:如何在远程计算机上执行命令?

I didn't use this one, but could have: How to execute a command in a remote computer?

我不确定是否需要上面网络文章中的所有步骤,我怀疑不是。但是我以为我会使用Invoke-Command PowerShell语句将文件复制到远程服务器上,但是我对上面的文章所做的更改却保留了大部分我认为是完整的。

I don't know for sure if all of the steps in the web articles above are needed, I suspect not. But I thought I was going to be using the Invoke-Command PowerShell statement to copy the files on a remote server, but left my changes from the articles above that I did intact mostly I believe.

您将需要在Active Directory中进行专门的用户设置,并配置运行SQL Server和SQL Server Agent的用户帐户,以便为主调用PowerShell提供访问网络和其他内容所需的特权,并且还可以用于在远程服务器上运行PowerShell。您可能需要配置SQLServer,以允许SQL Server作业或存储过程能够像我一样调用PowerShell脚本。但这超出了本文的范围。您在互联网上的Google其他地方向您展示了如何操作。

You will need a dedicated User setup in Active Directory, and to configure the user accounts that SQL Server and SQL Server Agent are running under to give the main calling PowerShell the privileges needed to access the network and other things to, and can be used to run the PowerShell on the remote server as well. And you may need to configure SQLServer to allow SQL Server Jobs or Stored Procedures to be able to call PowerShell scripts like I did. But this is outside the scope of this post. You Google other places on the internet to show you how to do that.

这篇关于Powershell-使用WMI将文件复制到远程主机并执行Install exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆