【问题标题】:Powershell - Copying File to Remote Host and Executing Install exe using WMIPowershell - 将文件复制到远程主机并使用 WMI 执行安装 exe
【发布时间】:2014-08-06 17:31:58
【问题描述】:

已编辑:这是我的代码。安装文件确实复制到远程主机。但是,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")
    }

【问题讨论】:

  • 已更改为赞成票,以帮助您获得一些分数来回答您自己的问题。但是,这里仍然有很多不必要的代码。如果您的问题确实只存在于那一行,那么您只需要:([WMICLASS]"\\$targetJavaComputer\ROOT\CIMV2:win32_process").Create("cmd.exe /c c:\temp\java\jre-7u60-windows-i586.exe /s /v" /qn")... 您期望它做什么,以及它实际在做什么。要“静默”运行 psexec,请使用 -accepteula

标签: powershell copy wmi psexec


【解决方案1】:

我试图让 $Job = Invoke-Command -Session $Session -Scriptblock $Script 允许我在不同的服务器上复制文件,因为我需要从运行它的服务器上卸载它. 我正在使用 PowerShell Copy-Item 来执行此操作。 但是正在运行的 PowerShell 脚本会等到文件完成复制才能返回。

我希望它在运行 powershell 的服务器上占用尽可能少的资源,以便在另一台服务器上生成进程以复制文件。我尝试使用各种其他方案,但他们没有工作或我需要他们工作的方式。 (对我来说似乎有点笨拙或太复杂。)也许其中一些可能有效?但是我找到了一个我喜欢的最适合我的解决方案,这很容易。 (如果尚未设置,则可能需要的一些后端配置除外。)

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

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

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

$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 来执行您希望它执行的操作(甚至向其传递参数):

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 脚本。但这超出了本文的范围。您可以在互联网上的其他地方谷歌以向您展示如何做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多