windows Powershell 确定远程计算机操作系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22101076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:25:32  来源:igfitidea点击:

Powershell determine the remote computer OS

windowspowershellwindows-7windows-xppowershell-2.0

提问by software is fun

I wrote a script to copy files to the "All Users" desktop or "Public Desktop"

我写了一个脚本将文件复制到“所有用户”桌面或“公共桌面”

However we have a mixed environment. Some people are using Windows XP and other people are using Windows 7.

然而,我们有一个混合环境。有些人使用 Windows XP,其他人使用 Windows 7。

$SOURCE = "I:\Path\To\Folder\*"
$DESTINATION7 = "c$\Users\Public\Desktop"
$DESTINATIONXP = "c$\Documents and Settings\All Users\Desktop"

$computerlist = Get-Content I:\Path\To\File\computer-list.csv

$results = @()
$filenotthere = @()
$filesremoved = @()
foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {   
        Write-Host "\$computer$DESTINATION\"      
        Copy-Item $SOURCE "\$computer$DESTINATION\" -Recurse -force        
    } else {
        $details = @{            
            Date             = get-date              
            ComputerName     = $Computer                 
            Destination      = $Destination 
        }                           
        $results += New-Object PSObject -Property $details
        $results | export-csv -Path I:\Path\To\logs\offline.txt -NoTypeInformation -Append
    }    
}

回答by Kirt Carson

DESTINATION is empty. Expanding on Keith's suggestion:

目的地是空的。扩展基思的建议:

foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        $OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem
        if($OS.caption -like '*Windows 7*'){
            $DESTINATION = $DESTINATION7
        }
        if($OS.caption -like '*Windows XP*'){
            $DESTINATION = $DESTINATIONXP
        }
    }
}

This could avoid the error you're getting also. empty $DESTINATION.

这也可以避免您遇到的错误。empty $DESTINATION.

回答by Michael Burns

In your foreach loop through $computerlist you can grab the OS Caption for each computer by using WMI:

在通过 $computerlist 的 foreach 循环中,您可以使用WMI获取每台计算机的操作系统标题:

$OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem 

Ant then check the $OS

Ant 然后检查 $OS

if($OS.caption -like '*Windows 7*'){
    #Code here for Windows 7
}
#....

回答by SysAdminAD Guy

I had a slightly different goal...But thanks for the basics.

我的目标略有不同……但感谢您的基础知识。

 del C:\scripts\OS.csv
$computerlist = Get-Content c:\scripts\computerlist.csv
foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {   
        Get-WMIObject Win32_OperatingSystem -ComputerName $computer |
        select-object CSName, Caption, CSDVersion, OSType, LastBootUpTime, ProductType| export-csv -Path C:\Scripts\OS.csv -NoTypeInformation -Append
    }
}