2014-02-28 105 views
2

我寫了一個腳本,將文件複製到「所有用戶」桌面或「公共桌面」PowerShell的確定遠程計算機OS

但是我們有一個混合環境。有些人使用的是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 
    }  
} 
+0

[I如何確定一個遠程計算機的OS?](http://superuser.com/q/323238/241386) –

回答

3

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

2

在你的foreach循環到$ COMPUTERLIST您可以抓取OS標題爲每臺計算機使用WMI

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

螞蟻則檢查$ OS

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

感謝。你如何看待例外?複製項目:登錄失敗:未知的用戶名或錯誤的密碼。 在I:\ Path \ To \ Code \ powershell \ copy.ps1:194 char:22 + Copy-Item <<<< $ SOURCE「\\ $ computer \ $ DESTINATIONXP \」-Recurse -fo rce + CategoryInfo:NotSpecified:(:) [Copy-Item],IOException + FullyQualifiedErrorId:System.IO.IOException,Microsoft.PowerShell.Comman ds.CopyItemCommand –

+2

在Powershell 2.0中,您可以使用[Try/Catch](https: //blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx) –

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