Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Problem getting powershell script to export defrag recomended to sql db...

$
0
0

Im trying to grab DefragRecomended from a Servers defrag analysis and plug it into a SQL DB, the output works, adds to the DB but it is NOT inserting the TRUE or FALSE-DefragRecomended output into the DB... I am using an 'if' statement as you see below to grab it but it is blank everytime? Someone please tell me where I am going wrong...

Code below

 Here is the code:

$datetime = Get-Date
$Time = $datetime

#Defrag Status

foreach ($server in get-content "C:\Documents and Settings\bobby\Desktop\Defrag\servers.txt")
{

"Server: $server"
       
    Write-Verbose $("Attempting to connect to $server")
       
    if ($(Test-Connection -ComputerName $server -Count 1 -Quiet) -eq $false) {
        Write-Verbose "Connection Failed"
        Write-Error $("Could not connect to computer: $computer")
    }
    Else {
        Write-Verbose "Connection established"   
   
        #Get all local disks on the specified computer via WMI class Win32_Volume
        Get-WmiObject -ComputerName $server -Class Win32_volume -Filter "DriveLetter = 'C:'" | Where-Object { $_.drivetype -eq 3 -and $_.driveletter -ne $null } |

        #Perform a defrag analysis on each disk returned
        ForEach-Object -begin {} -process {      
             
            #Initialise properties hashtable
            $properties = @{}
            
            #perform the defrag analysis
            Write-Verbose $("Analyzing volume " + $_.DriveLetter + " on computer " + $server)
            $results = $_.DefragAnalysis()
   
            #if the return code is 0 the operation was successful so output the results using the properties hashtable
            if ($results.ReturnValue -eq 0) {                  
                #$properties.Add('ComputerName',$_.__Server)
                if ($_.DefragAnalysis().DefragRecommended -eq $true) { $properties.Add( 'DefragRequired',$true ) } else {$properties.Add( 'DefragRequired',$false)}
                Write-Verbose "Analysis complete"
                New-Object PSObject -Property $properties
             
                #Database Update 
                "INSERT INTO Defrags" | out-file "C:\Documents and Settings\bobby\Desktop\Defrag\insert.sql"
                "Values ('$Server','$DefragRequired','$Time')" | out-file "C:\Documents and Settings\bobby\Desktop\Defrag\insert.sql" -append
                
                sqlcmd -S prma.mermind.Comp.com -d Comp_KnowledgeBase -i "C:\Documents and Settings\bobby\Desktop\Defrag\insert.sql"
               
                cls remove-item "C:\Documents and Settings\bobby\Desktop\Defrag\insert.sql"
                                                        
            } #Close for Hashtable PSobject
 
        } #Close ForEach loop for Defrag Analysis
   
    } #Close else clause on test-computer if conditional   

} #Close ForEach loop on Test-Computer


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>