从远程计算机获取 OS 架构的 Powershell If 语句

Powershell If statement to get OS Architecture from remote computers

我正在尝试编写一个 powershell 脚本来连接一组计算机,并根据 OS 架构 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

你遇到了什么错误?文件不存在? .exe不是运行吗?

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

的结束语句
$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
        }
    }
}