Powershell - 使用WMI将文件复制到远程主机并执行Install exe

时间:2014-06-16 20:06:48

标签: powershell copy wmi psexec

编辑:这是我的代码。安装文件会复制到远程主机。但是,WMI部分不会安装.exe文件,并且不会返回任何错误。也许这是WMI的语法错误?有没有办法用PsExec静默运行安装程序?再次感谢所有帮助,感到抱歉:

#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")
    }

1 个答案:

答案 0 :(得分:1)

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

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

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

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

这是我使用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 

我们认为上面的这一点是首选。

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

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

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

帮助配置WMI:

  

我从https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/invoke-wmimethod?view=powershell-5.1

了解了这项工作的主要要点。      

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

     

https://helpcenter.gsx.com/hc/en-us/articles/202447926-How-to-Configure-Windows-Remote-PowerShell-Access-for-Non-Privileged-User-Accounts?flash_digest=bec1f6a29327161f08e1f2db77e64856b433cb5a

     

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

     

Powershell New-PSSession Access Denied - Administrator Account

     

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

     

我没有用过,但是可能有:How to execute a command in a remote computer?

我不确定是否需要上面网络文章中的所有步骤,我怀疑不是。但是我以为我将使用Invoke-Command PowerShell语句将文件复制到远程服务器上,但是我对上面的文章所做的更改仍然保留了下来,但我相信大部分都没有改变。

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

相关问题