Newbie to loops

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 :slight_smile:

[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 

You’ve used $env:computername, which would be your local computer name, for the -ComputerName parameter.

It would seem like you’d want to use $server, which is your loop’s enumeration variable, no? I also note that you’re not consistently querying a remote computer - not every command uses -ComputerName, for example. In fact, it seems only your first Get-EventLog does.

As a note, you’re starting down a dark path in terms of PowerShell practices and patterns, and it’s going to cost you a lot more work the further you go. You might consider “Learn PowerShell Scripting in a Month of Lunches,” which will get you on The Path of Righteousness when it comes to scripting “the PowerShell way.”

Hey Don,
While waiting for this post to be approved I did notice the same thing and have added -ComputerName parameters to all my commands.
I have actually bought your book and am getting through it slowly as I’m not the biggest fan of reading. Is very interesting though
Thank you again :slight_smile:
Matt