AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 936477
Accepted
Glenn Sullivan
Glenn Sullivan
Asked: 2018-10-21 18:13:24 +0800 CST2018-10-21 18:13:24 +0800 CST 2018-10-21 18:13:24 +0800 CST

Powershell Invoke-Command 在手动运行时有效,但在脚本中无效

  • 772

我正在构建一个脚本来远程安装 PSWindowsUpdate PowerShell 模块,打开正确的防火墙端口,然后运行命令来安装等待更新。为了安装 PSWindowsUpdate 模块,我需要在我的一些机器上安装 MSI 以启用“Install-Module”命令行开关。

如果我在本地计算机上的管理员 PowerShell 会话中手动运行以下代码,它可以正常工作:

$RemoteMachine = "DCSvrRDS16"
write-host "Server Name is: $RemoteMachine"

#Copy the MSI Local to the computer
Write-Host "Copying MSI locally"
Copy "\\dcsvrstorage2\software\microsoft\powershell\PackageManagement_x64.msi" \\$RemoteMachine\c$\

#Run the MSI remotely
Write-Host "Running MSI"
Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { c:\windows\system32\msiexec /i c:\PackageManagement_x64.msi /quiet /lvx* c:\PackageManagement.log } 4>&1

但是,如果我在完全相同的已打开 PowerShell 会话中运行此完整脚本,则不会安装 MSI 并且不会创建日志文件:

Import-Module PSWindowsUpdate
$domaincredentials = Get-Credential


Function ProcessMachine($RemoteMachine) {

    write-host "Server Name is: $RemoteMachine"

    #Copy the MSI Local to the computer
    Write-Host "Copying MSI locally"
    Copy "\\dcsvrstorage2\software\microsoft\powershell\PackageManagement_x64.msi" \\$RemoteMachine\c$\

    #Run the MSI remotely
    Write-Host "Running MSI"
    Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { c:\windows\system32\msiexec /i c:\PackageManagement_x64.msi /quiet /lvx* c:\PackageManagement.log } 4>&1

    #Install the module
    Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { Install-Module PSWindowsUpdate -Force }

    #Turn on the firewall rules
    Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { Set-NetFirewallRule -DisplayName "COM+ Network Access (DCOM-In)" -Enabled True }
    Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { Set-NetFirewallRule -DisplayName "COM+ Remote Administration (DCOM-In)" -Enabled True }
    Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { Remove-NetFirewallRule -DisplayName "Remote WSUS Install" }
    Invoke-Command -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { New-NetFirewallRule -DisplayName "Remote WSUS Install" -Profile Domain -Enabled True -Direction Inbound -Action Allow -Protocol TCP -LocalPort RPC }

    #Try the update
    Install-WindowsUpdate -ComputerName $RemoteMachine -AcceptAll
}

foreach ($machine in (Get-Content NeedsWSUS.txt)) {

    ProcessMachine $machine

}

这让我快疯了。我已经在远程机器上运行了 ProcessMonitor,我可以看到 msiexec 启动然后停止,而无需访问 C: 驱动器根目录中的 MSI 或尝试打开日志文件进行写入。

有没有人见过类似的东西?客户端机器是 Windows 7 pro、Powershell 4,目标机器是 Windows 2012 R2 和 PowerShell 4。

提前致谢。

powershell
  • 1 1 个回答
  • 5092 Views

1 个回答

  • Voted
  1. Best Answer
    Enigman
    2018-10-21T22:05:05+08:002018-10-21T22:05:05+08:00

    尝试运行需要一些时间才能执行的脚本块或命令(尤其是安装活动)时,可能会出现此问题。该脚本在脚本块中的命令完成其活动之前返回。使用带有 -wait 参数的 Start-Process 让脚本等待,直到正在运行的命令完成并返回给脚本。我之前在远程机器上安装时注意到这一点,而在没有 Start-Process 的情况下在本地运行时似乎工作正常。

    如您所见,正确的命令更改是:

    调用命令 -ComputerName $RemoteMachine -Credential $domaincredentials -ScriptBlock { Start-Process msiexec "/ic:\PackageManagement_x64.msi /quiet /lvx* c:\PackageManagement.log" -wait }

    • 2

相关问题

  • 资源锁和电源外壳

  • 脚本 - 如何断开远程桌面会话?

  • 如何限制向通讯组发送到外部地址?

  • Powershell对值与数组的作用不同?

  • Windows Powershell Vim 键绑定

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve