Link to home
Start Free TrialLog in
Avatar of kihl71
kihl71Flag for Sweden

asked on

Uptime Script

I found a script that export the uptimes on computer to a csv. I modified it to suit my needs. I am new to powershell scripting and i wonder why do i get this error when running and what to do to get rid of it.

Thanks for helping

//// Error starts

Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
At C:\Powerscipts\export_uptime2.ps1:29 char:19
+         $Results+= <<<< $Srv    
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

///

//Script starts

#add-PSSnapin quest.activeroles.admanagement
$Servers = Get-QADComputer -SearchRoot 'domain.local/workstations/std/Laptop' | foreach {$_.name}
$datetime = Get-Date -Format "yyyyMMddHHmmss";

#Add headers to log file
Add-Content "c:\temp\uptime-laptop.csv" "SystemName, Uptime";
foreach($Server in $Servers)

{
#$desc = Get-Qadcomputer $server | select Description
$wmi=Get-WmiObject -class Win32_OperatingSystem -ComputerName $Server -ErrorAction SilentlyContinue
if ($wmi)
        {
       
        $LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
     
        [TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)

        $Srv = New-Object PSObject
        $Srv | Add-Member -MemberType NoteProperty -Name Computer -Value $Server
        $Srv | Add-Member -MemberType NoteProperty -Name Days -Value $Uptime.Days
        $Srv | Add-Member -MemberType NoteProperty -Name Hours -Value $Uptime.Hours
        $Srv | Add-Member -MemberType NoteProperty -Name Minutes -Value $Uptime.Minutes
        $Srv | Add-Member -MemberType NoteProperty -Name Seconds -Value $Uptime.Seconds
       
        $Results+=$Srv    
       
        }
}
$Results | Export-CSV "c:\temp\uptime-laptop.csv" -NoType
ASKER CERTIFIED SOLUTION
Avatar of comnuts
comnuts
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kihl71

ASKER

Thank´s that worked great.

Could you please help me with another thing i would like to include the computer.description field in the export. How can i do that the simplest way?
Please try if this works (in a hurry so I quickly got this). Replace just the FOR loop:

.
.
.
.
foreach($Server in $Servers)
{
$wmi=Get-WmiObject -class Win32_OperatingSystem -ComputerName $Server -ErrorAction SilentlyContinue
$wmi2=Get-WmiObject -class Win32_Environment -ComputerName $Server -ErrorAction SilentlyContinue
if ($wmi)
        {
       
        $LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
     
        [TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)

        $Srv = New-Object PSObject
        $Srv | Add-Member -MemberType NoteProperty -Name Computer -Value $Server
        $Srv | Add-Member -MemberType NoteProperty -Name ComputerDescription -Value $wmi2.Description
        $Srv | Add-Member -MemberType NoteProperty -Name Days -Value $Uptime.Days
        $Srv | Add-Member -MemberType NoteProperty -Name Hours -Value $Uptime.Hours
        $Srv | Add-Member -MemberType NoteProperty -Name Minutes -Value $Uptime.Minutes
        $Srv | Add-Member -MemberType NoteProperty -Name Seconds -Value $Uptime.Seconds
       
        $Results+=$Srv    
       
        }
}
.
.
.
Avatar of kihl71

ASKER

Sorry! I meant the Computer Descrition Field from active directory.

/thanks
You've already given away points so this is kind of a dead question, but I would recommend using the Quest Powershell Active Directory Snapin.  That way, you can easily use the computer name as your query and get back the description like this:

$Computer = get-qadcomputer $Server
$Description = $Computer.description

Remember, you must have the Quest Addin both installed (run the executable) AND called from the script if you don't add it into a running Powershell session:
Add-PSSnapin "*Quest*"

Good luck!

-DH