IT
 New Member Posts:55

 |
| 06 Jan 2009 04:57 AM |
|
I am trying to view the log files from a server and would like them to be in the same exported file as the code I am currently using.
Start-Transcript "c:\scripts\transcript.doc" function Output { $strComputer = "." $colItems = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -computername $strComputer foreach ($objItem in $colItems) { if($objItem.Name -eq "C:" -OR $objItem.Name -eq "E:" -OR $objItem.Name -eq "F:") { $name = $objItem.Name $volName = $objItem.VolumeName $size = $objItem.Size/1gb $free = $objItem.FreeSpace/1gb $percent = $(($free/$size*100)).ToString("0.##") $size = $size.ToString("0.##") $free = $free.ToString("0.##") Write-Host "$name Name: $volName Size: $size Free Space: $free Percent Free: $percent" } } } Output Stop-Transcript |
|
|
|
|
EBGreen
 Veteran Member Posts:1092

 |
| 06 Jan 2009 07:24 AM |
|
Do you mean the event logs? |
|
| "Look Ma...no strings!" |
|
|
IT
 New Member Posts:55

 |
|
get-james
 New Member Posts:59

 |
| 07 Jan 2009 10:52 PM |
|
Try the following, which will put your results to a variable called "$report": $Strcomputer = "." $Disks = get-wmiobject -class Win32_LogicalDisk -Namespace ROOT\CIMV2 -ComputerName $Strcomputer | select SystemName,DeviceID,Description,VolumeName,Size,FreeSpace $Report = @(Foreach ($disk in $Disks) { #DeviceID,VolumeName,Size,FreeSpace If ($disk.DeviceID -eq "C:" -or $disk.DeviceID -eq "D:") { $myobj = "" | select SystemName,Drive,Name,"Total Size (GB)","Free Space (GB)","Free Space (%)","Used Space (%)" $myobj.SystemName = $Disk.SystemName $myobj.Drive = $Disk.DeviceID $myobj.Name = $Disk.VolumeName $myobj."Total Size (GB)" = "{0:##.#}" -f ($Disk.size/1gb) $myobj."Free Space (GB)" = "{0:##.#}" -f ($Disk.FreeSpace/1gb) $myobj."Free Space (%)" = "{0:P1}" -f(($Disk.FreeSpace/1gb)/($Disk.size/1gb)) $myobj."Used Space (%)" = "{0:P1}" -f((($Disk.size/1gb)-($Disk.FreeSpace/1gb))/($Disk.size/1gb)) $myobj } }) #Display Results PS C:\> $Report | ft -auto SystemName Drive Name Total Size (GB) Free Space (GB) Free Space (%) Used Space (%) ---------- ----- ---- --------------- --------------- -------------- -------------- SONY-LAPTOP C: System 126.5 30.2 23.9 % 76.1 % SONY-LAPTOP D: Data 95.2 8.6 9.0 % 91.0 % To output the information to a file you can use the following commands: #Output results to a text file: PS C:\> $report | out-file C:\_Powershell\Diskusage.txt PS C:\> Get-Content C:\_Powershell\Diskusage.txt SystemName : SONY-LAPTOP Drive : C: Name : System Total Size (GB) : 126.5 Free Space (GB) : 30.2 Free Space (%) : 23.9 % Used Space (%) : 76.1 % SystemName : SONY-LAPTOP Drive : D: Name : Data Total Size (GB) : 95.2 Free Space (GB) : 8.6 Free Space (%) : 9.0 % Used Space (%) : 91.0 % #Output results to a text file with format table : PS C:\> $report | format-table -auto | out-file C:\_Powershell\Diskusage_FT.txt PS C:\> Get-Content C:\_Powershell\Diskusage_FT.txt SystemName Drive Name Total Size (GB) Free Space (GB) Free Space (%) Used Space (%) ---------- ----- ---- --------------- --------------- -------------- -------------- SONY-LAPTOP C: System 126.5 30.2 23.9 % 76.1 % SONY-LAPTOP D: Data 95.2 8.6 9.0 % 91.0 % #Export results to a csv file : PS C:\> $report | export-csv C:\_Powershell\Diskusage.csv -NoTypeInformation PS C:\> import-csv C:\_Powershell\Diskusage.csv | format-table -auto SystemName Drive Name Total Size (GB) Free Space (GB) Free Space (%) Used Space (%) ---------- ----- ---- --------------- --------------- -------------- -------------- SONY-LAPTOP C: System 126.5 30.2 23.9 % 76.1 % SONY-LAPTOP D: Data 95.2 8.6 9.0 % 91.0 % Hope this helps James
|
|
|
|
|
IT
 New Member Posts:55

 |
| 08 Jan 2009 12:58 PM |
|
I already have the code to view the disk space. I'm not sure how to view the event log files from a server. |
|
|
|
|
EBGreen
 Veteran Member Posts:1092

 |
|
get-james
 New Member Posts:59

 |
| 08 Jan 2009 02:03 PM |
|
Check out the following script, which I did to query my event logs on my remote servers: http://powershellcommunity.org/Foru...fault.aspx $logfile="Application" $eventcode="1221" $recent=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((Get-Date).AddDays(-1)) # Gets yesterdays date. $Events = get-wmiobject -computer $server -class Win32_NTLogEvent ` -filter "logfile = '$logfile' AND (TimeGenerated >='$recent') AND EventCode = '$eventcode'" #This section formats the results from $Events Foreach ($item in $Events) { $item } |
|
|
|
|
IT
 New Member Posts:55

 |
| 12 Jan 2009 05:48 AM |
|
The code from get-james didnt seem to work on Windows based Server 2003. I recieved a repeated error. You cannot call method on a null-valued expression. Maybe I'm doing something wrong since I just started using PowerShell. |
|
|
|
|
get-james
 New Member Posts:59

 |
| 12 Jan 2009 08:47 AM |
|
Post your script and I will take a look? James. |
|
|
|
|
IT
 New Member Posts:55

 |
| 12 Jan 2009 08:48 AM |
|
Function GetWhiteSpace($Server) { $logfile="Application" $eventcode="1221" $recent=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((Get-Date).AddDays(-1)) $Events = get-wmiobject -computer $server -class Win32_NTLogEvent ` -filter "logfile = '$logfile' AND (TimeGenerated >='$recent') AND EventCode = '$eventcode'" Foreach ($item in $Events) { $iDay= ($item.TimeGenerated.SubString( 6, 2)) $iMonth= ($item.TimeGenerated.SubString( 4, 2)) $iYear= ($item.TimeGenerated.SubString( 0, 4)) $iHour= ($item.TimeGenerated.SubString( 8, 2)) $iMinute= ($item.TimeGenerated.SubString(10, 2)) $iSecond= ($item.TimeGenerated.SubString(12, 2)) If ($iDay.length -eq 1) {"0"+$iday} If ($iMonth.length -eq 1) {"0"+$iMonth} $strDate=$iDay+"/"+$iMonth+"/"+$iYear $StrTime=$iHour+":"+$iMinute+":"+$iSecond $iServer = $item.message | % {($_.Split("""")[1])} $iSize = $item.message | % {($_.Split("") | ?{$_ -match "\d"})[1]} $myobj = "" | Select-Object Date,Time,"Server","Size MB" $myobj."Date" = $strDate $myobj."Time" = $StrTime $myobj."Server" = $iServer $myobj."Size MB" = $iSize } } $i=0 $Server = "use-app01","use-main" Foreach($Serv in $Server) { $i++ Write-Progress -id 10 -Activity "Exchange White Space Check" -Status "Progress" -PercentComplete (($i/$Server.length)*100) Write-Progress -id $i -parentId 10 -Activity "Extracting Information" -Status $Serv GetWhiteSpace($Serv) }
|
|
|
|
|
get-james
 New Member Posts:59

 |
| 12 Jan 2009 08:53 AM |
|
I forgot to set the $server variable, see the example below: $Server="." $logfile="Application" $recent=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((Get-Date).AddDays(-1)) # Gets yesterdays date. $Events = get-wmiobject -computer $server -class Win32_NTLogEvent ` -filter "logfile = '$logfile' AND (TimeGenerated >='$recent')" #This section formats the results from $Events Foreach ($item in $Events) { $item }
|
|
|
|
|
get-james
 New Member Posts:59

 |
| 12 Jan 2009 09:06 AM |
|
What kind of server are you running this on, as this script will show the application events logs that match event code 1221?
|
|
|
|
|
IT
 New Member Posts:55

 |
| 14 Jan 2009 05:08 AM |
|
I got the code to work, but the output that the code shows is nothing like what i would like to see. Basically I would like to have the output of the script be the same view as event viewer on a server, but with filters based on "Application" events, and only events from the previous day. Other then that the information should be identical to event viewer when the scripts is ran. |
|
|
|
|
get-james
 New Member Posts:59

 |
| 14 Jan 2009 05:52 AM |
|
Have you tried selecting only the properties you are interested in like:
$Server="." $logfile="Application" $recent=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((Get-Date).AddDays(-1)) # Gets yesterdays date.
$Events = get-wmiobject -computer $server -class Win32_NTLogEvent ` -filter "logfile = '$logfile' AND (TimeGenerated >='$recent')"
#This section formats the results from $Events Foreach ($item in $Events) { $item | select TimeGenerated,SourceName,CategoryString,Type,EventCode,User,ComputerName,Message } |
|
|
|
|
IT
 New Member Posts:55

 |
| 14 Jan 2009 05:57 AM |
|
Thanks that works great. I just started using PowerShell a couple weeks ago so I'm still learning how to do the simple stuff. |
|
|
|
|