PowerShell如何确定远程计算机的操作系统

6

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

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

我如何确定远程计算机的操作系统? - phuclv
3个回答

8

目标文件夹为空。在基思的建议上进行扩展:

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

这可以避免你收到的错误,也就是“空的 $DESTINATION”错误。

3

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

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

然后检查 $OS

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

谢谢。如何捕获异常?Copy-Item:登录失败:未知用户名或错误的密码。位于 I:\Path\To\Code\powershell\copy.ps1:194 字符:22
  • Copy-Item <<<< $SOURCE "\\$computer\$DESTINATIONXP\" -Recurse -fo
rce + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Comman ds.CopyItemCommand
- software is fun
2
在Powershell 2.0中,你可以直接使用Try/Catch - Michael Burns

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接