Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Uptime Script

Posted on 2012-09-10
5
Medium Priority
?
939 Views
Last Modified: 2012-09-10
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
0
Comment
Question by:kihl71
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
comnuts earned 705 total points
ID: 38382433
Add this

$Results = @();

to line four of your script below $datetime = Get-Date -Format "yyyyMMddHHmmss";

This is to declare $Results variable as an Array. See link:
http://www.vistax64.com/powershell/32290-method-failed-op_addition.html
0
 

Author Closing Comment

by:kihl71
ID: 38382449
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?
0
 
LVL 6

Expert Comment

by:comnuts
ID: 38382489
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    
       
        }
}
.
.
.
0
 

Author Comment

by:kihl71
ID: 38382504
Sorry! I meant the Computer Descrition Field from active directory.

/thanks
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 38382515
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question