Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell determine the remote computer OS

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.

$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
    }    
}
like image 929
software is fun Avatar asked Feb 28 '14 16:02

software is fun


People also ask

How do I find the OS of a remote computer?

EASIEST METHOD: Click the Windows Start button and type msinfo32 and press Enter. Click View > Remote Computer > Remote Computer on the Network. Type machine name and click OK.

How do I get the OS information in PowerShell?

There are two PowerShell cmdlets for extracting OS information, Get-WMIObject and Get-CimInstance.


2 Answers

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.

like image 107
Kirt Carson Avatar answered Oct 24 '22 00:10

Kirt Carson


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

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

Ant then check the $OS

if($OS.caption -like '*Windows 7*'){
    #Code here for Windows 7
}
#....
like image 27
Michael Burns Avatar answered Oct 24 '22 00:10

Michael Burns