Hey everyone,
Just starting out in Powershell and I’ve put together this to get some basic information on a range of machines for basic monthly maintenance reports.
The code works but only to the local machine I think it has something to do with not assigning the variable $server in the foreach loop to anything any ideas on how I could fix this ? Thanks in advance ![]()
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
#list of servers file path
$ServersFilePath = [Microsoft.VisualBasic.Interaction]::InputBox('Example: C:\Folder\File.txt or \\SERVERNAME\SHARE\File.txt','Server List File Path','localhost')
#Report File Path
$ReportFilePath = [Microsoft.VisualBasic.Interaction]::InputBox('Example: C:\Folder or \\SERVERNAME\SHARE','End Report File Path','localhost')
#Number of Log enteries to retrieve
$NumLogEntries = "20"
#Maintenance start date in format D/M/YYYY
$MaintStartDate = [Microsoft.VisualBasic.Interaction]::InputBox('Format: D/M/YYYY','Maintenance Start Date','1/1/2017')
#Maintenance end date in format D/M/YYYY
$MaintEndDate = [Microsoft.VisualBasic.Interaction]::InputBox('Format: D/M/YYYY','Maintenance End Date','1/1/2017')
#CPU Test Length
$CPUTestLength = "10"
$Servers = Get-Content $ServersFilePath
foreach ($Server in $Servers)
{
#Servername
$SVRName = $env:COMPUTERNAME
#IP Address
$IPV4 = (Test-Connection -ComputerName $env:computername -count 1).IPV4Address.ipaddressTOstring
#System Event Log Errors
$SysEventLog = Get-EventLog -LogName System -Newest $NumLogEntries -EntryType Error -After $MaintStartDate -before $MaintEndDate
#Application Event Log Errors
$AppEventLog = Get-EventLog -LogName Application -Newest $NumLogEntries -EntryType Error -After $MaintStartDate -before $MaintEndDate
#Local Drive Information
$DriveInfo = Get-CimInstance win32_logicaldisk | ForEach-Object {write " $($_.Caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}
#CPU Min and Max Over x seconds
$CPURange = get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples $CPUTestLength | select -ExpandProperty countersamples | Measure-Object -property cookedvalue -Maximum -minimum
#Toal and Free RAM
$os = Get-WmiObject -Class win32_operatingsystem
$RAMTtl = [math]::Round(($os.TotalVisibleMemorySize/1000000))
$RAMFree = [math]::Round(($os.FreePhysicalMemory/1000000))
#Time Since Last Reboot
$rv = Get-WmiObject -Class win32_operatingsystem -Property Lastbootuptime
$LastBoot = $rv.ConvertToDateTime($rv.LastBootUpTime)
}
#Creating report file
$SVRName, $IPV4, $SysEventLog, $AppEventLog, $DriveInfo, $CPURange, $RAMTtl, $RAMFree | Out-File $ReportFilePath\$SVRName.txt -force
$LastBoot | Out-File $ReportFilePath\$SVRName.txt -Append