PowerShell: Running smartctl.exe (smart scan) remotely with psexec

3 min readGinja

This is a script I wrote to run smartctl.exe remotely using psexec.

function Perform-SmartScan {
    param (
    $computerName,
    $device,
    $testToRun
    )
    $smrtTool  = "$boxDir\smrtscn\smartctl.exe"
    
    if ($testTorun -eq $null) { $testTorun = "-a" } 
    if ($device -eq $null)    { $device = "/dev/sda" }
 
    if ($computerName) { $computerName = Search-Computer($computerName) }
    
    if ($computerName) {
        if (Test-Connection $computerName -quiet -count 1) {
        
            $smrtrTool = "\\$computerName\c$\smartctl.exe"
            Copy-Item $smrtTool "\\$computerName\c$\" -ea Stop 
            $cmd = "cmd.exe /c '$psexec' \\$computerName $smrtrTool $testTorun $device"
            Invoke-Expression $cmd -ea Stop
            Remove-Item $smrtrTool
         
    
        } else {
            
            Write-Host "---------------------------"            -fore Gray
            Write-Host "Computer $computerName is unreachable." -fore Red
            Write-Host "---------------------------"            -fore Gray 
    
        }
    }
}

 

I added a few parameters, which can be changed or not used entirely.  This was written quickly but works well if you have administrator access to the remote PC. 
You do need my Search-Computer function to use it as is, which assumes you're using AD :)

function Search-Computer {

 

    param(

        $srcComputer = $(Throw "Please specify a computer name.")

    ) 

    $srcComputer = "*" + $srcComputer + "*"

    $cmpList = Get-QADComputer -Name $srcComputer -ip LastLogonTimeStamp

    if ($cmpList.count -gt 1) {

        $i = 0

        Write-Host "----------------------------------" -fore Gray

        Write-Host "Computer listing for $srcComputer"  -fore Red

        Write-Host "----------------------------------" -fore Gray

        ForEach ($cmp in $cmpList) {

            $cmpName    = $cmp.Name

            $cmpEnabled = $cmp.AccountIsDisabled

            $cmpDN      = $cmp.DN

            $cmpLastllt = $cmp.LastLogonTimeStamp

            if ($cmpEnabled -eq $true) {

                $cmpEnabled = "This computer is disabled!"

            } else {

                $cmpEnabled = ""

            }    

 

            Write-Host "------------------------" -fore gray

            Write-Host "[$i] Name   : " $cmpName 

            Write-Host "Last Logon : " $cmpLastllt 

            Write-Host "OU          :" $cmpDN

            Write-Host $cmpEnabled  -fore red

            Write-Host "------------------------" -fore gray

 

            $i++

        }

        Write-Host "----------------------------------" -fore Gray

        $cmpSelection  = Read-Host "Please [select] a number"

        Return ($cmpList[$cmpSelection]).Name

     } else {

        if ($cmpList) {

            Return $cmpList.Name

        } else {

            Return Write-Error "$srcComputer not found in AD."

        }

     }   

 

 

Comments

No comments yet. Be the first!