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

时间:2014-02-28 16:50:27

标签: windows powershell windows-7 windows-xp powershell-v2.0

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

但是我们有一个混合的环境。有些人正在使用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
    }    
}

3 个答案:

答案 0 :(得分:4)

DESTINATION是空的。扩展基思的建议:

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

这可以避免你得到的错误。 empty $DESTINATION

答案 1 :(得分:2)

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

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

Ant然后检查$ OS

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

答案 2 :(得分:0)

我的目标略有不同......但感谢基础。

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