kihl71
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.Automat ion.PSObje ct] doesn't contain a method named 'op_Addition'.
At C:\Powerscipts\export_upti me2.ps1:29 char:19
+ $Results+= <<<< $Srv
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
///
//Script starts
#add-PSSnapin quest.activeroles.admanage ment
$Servers = Get-QADComputer -SearchRoot 'domain.local/workstations /std/Lapto p' | 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.ConvertToDate Time($wmi. Lastbootup time)
[TimeSpan]$uptime=New-Time Span $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
Thanks for helping
//// Error starts
Method invocation failed because [System.Management.Automat
At C:\Powerscipts\export_upti
+ $Results+= <<<< $Srv
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
///
//Script starts
#add-PSSnapin quest.activeroles.admanage
$Servers = Get-QADComputer -SearchRoot 'domain.local/workstations
$datetime = Get-Date -Format "yyyyMMddHHmmss";
#Add headers to log file
Add-Content "c:\temp\uptime-laptop.csv
foreach($Server in $Servers)
{
#$desc = Get-Qadcomputer $server | select Description
$wmi=Get-WmiObject -class Win32_OperatingSystem -ComputerName $Server -ErrorAction SilentlyContinue
if ($wmi)
{
$LBTime=$wmi.ConvertToDate
[TimeSpan]$uptime=New-Time
$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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.ConvertToDate Time($wmi. Lastbootup time)
[TimeSpan]$uptime=New-Time Span $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
}
}
.
.
.
.
.
.
.
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.ConvertToDate
[TimeSpan]$uptime=New-Time
$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
}
}
.
.
.
ASKER
Sorry! I meant the Computer Descrition Field from active directory.
/thanks
/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
$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
ASKER
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?