【问题标题】:Powershell If statement to get OS Architecture from remote computersPowershell If 语句从远程计算机获取操作系统架构
【发布时间】:2021-02-16 17:57:22
【问题描述】:

我正在尝试编写一个 powershell 脚本,该脚本可以访问一组计算机,并根据操作系统架构 32 位或 64 位运行安装。但我无法让它工作。

$Computers =Get-Content -Path.\vms.txt

foreach ($Computer in $Computers)
{ Invoke-Command -ComputerName $Computer -ScriptBlock {
    If ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*")
    {
    Start-Process D:\setup64.exe 
    }
    Else
    {
    Start-Process D:\setup.exe
    }
}

我从 Powershell 收到两个错误,一个是找不到文件,另一个是它无法识别 Else

【问题讨论】:

  • #1。左括号应与if 条件位于同一行。 #2。 | select osarchitecture 是多余的。 #3。远程机器上是否存在文件?
  • 哪个 PowerShell 版本?确切的错误信息是什么?请注意,Get-WmiObject 已在 PowerShell Core 中耗尽。
  • 该文件存在于同一目录中,其中包含计算机的名称。我正在使用 PowerShell 2.0
  • 您确定列表中的所有计算机在它们的本地磁盘上都有安装文件D:\setup64.exeD:\setup.exe

标签: powershell wmi


【解决方案1】:

您遇到了什么错误?文件不存在? .exe 没有运行吗?

根据您在上面发布的内容,像@filimonic 指出的那样,将选择管道传输到您的 gwmi 是多余的。您还缺少脚本块 } 的结束语句。

$Computers = Get-Content -Path .\vms.txt

foreach ($Computer in $Computers){ 
Invoke-Command -ComputerName $Computer -ScriptBlock {
    If ((Get-WmiObject win32_operatingsystem).osarchitecture -like "64*"){
        Start-Process D:\setup64.exe}Else{
        Start-Process D:\setup.exe
        }
    }
}

编辑:试一试。 . .

$Computers = Get-Content -Path .\vms.txt

foreach ($Computer in $Computers){ 
$OS = Get-WmiObject win32_operatingsystem -ComputerName $Computer | Select-Object -ExpandProperty osarchitecture 
    if($OS -like "64*"){

Invoke-WmiMethod -path win32_process -ComputerName $Computer -name create -argumentlist "CMD /C `"D:\setup.exe`""}else{
Invoke-WmiMethod -path win32_process -ComputerName $Computer -name create -argumentlist "CMD /C `"D:\setup.exe`""
    }
}

EDIT2:理论上,这也应该有效。 . .

$sessions =  New-PSSession -ComputerName (Get-Content -Path .\vms.txt)
    foreach ($Computer in $sessions){ 

Invoke-Command -Session $Computer -ScriptBlock {
    If ((Get-WmiObject win32_operatingsystem).osarchitecture -like "64*"){
        Start-Process D:\setup64.exe}Else{
        Start-Process D:\setup.exe
        }
    }
}

【讨论】:

  • 感谢修复关闭语句有效,但我仍然无法在远程机器上运行任何程序我将 Start-Process 更改为 Start-Process 为:Start-Process C:\Windows\System32\notepad .exe 但还是不行
  • Powershell 远程处理是使用带有运行代码的会话凭据的 netlogon 完成的。如果其他人登录,您将看不到您正在运行的任何内容,因为它是在您的帐户上运行的。
  • 在TaskManager中可以看到所有用户的进程,也可以先对机器运行Get-Process,运行代码后看看区别。
  • 谢谢亚伯拉罕,我知道这一点,但记事本没有显示在进程列表中。我不知道我做错了什么。我没有收到任何错误。
  • Abraham 谢谢,我尝试了您的编辑,其中一个使用“Invoke-WmiMethod”工作,另一个没有我从您的帖子中复制并粘贴代码以确保我没有输入错误的内容我得到的另一个 rpc 服务器不可用,这对我来说没有意义,因为使用“Invoke-WmiMethod”的编辑再次起作用了谢谢。
猜你喜欢
  • 2014-04-01
  • 2021-10-31
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
相关资源
最近更新 更多