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



我正在尝试编写一个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

你得到什么错误?那些文件根本不存在?.exe文件没有运行?

根据你上面发布的内容,像@filimonic指出的那样,向你的gwmi输出一个select是多余的。您还缺少脚本块}的结束语句。

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

最新更新