Search code examples
powershellwmi

Powershell If statement to get OS Architecture from remote computers


I'm trying to write a powershell script that reaches out to a group of computers and according to the OS Architecture 32 or 64 bit runs the install. But I cannot get it to work.

$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
    }
}

I get two errors from Powershell one is that it cannot find the file and the other is that it doesn't recognize the Else


Solution

  • What error are you getting? That the files don't exist? Is the .exe not running?

    Based off what you posted above, piping a select to your gwmi is redundant like @filimonic pointed out. You are also missing the closing statement for your script block }.

    $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
            }
        }
    }
    

    EDIT: Give this a shot. . .

    $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: Theoretically, this should work as well. . .

    $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
            }
        }
    }