We help IT Professionals succeed at work.

PowerShell script to pull from text file

07vette
07vette asked
on
Hi All,
I have a PS script that uses server names from AD and then copys a log file from those servers, renames the file, and then copies them to a share. I am trying to edit the PS script to pull from a text file locate on my local drive, instead of AD. Here is the original Script below. I have tried Get-Content instead of Get-QADComputer and it does not work.
Thanks in advance!!!
$OnlineServers = @()

Get-QADComputer -SearchRoot 'mydomain.com/Web servers' -OSName "Windows*Server*" |
 Foreach {If (Test-Connection $_.Name) {$OnlineServers += $_.Name}}

$ServerCount = 0

foreach($Server in $OnlineServers)
{
 $OS = Get-WMIObject Win32_OperatingSystem -ComputerName $Server 
 $winupdatepath = "\\" + $Server + "\" + (($os.windowsdirectory).replace(":", "$"))

 If (Test-Path ($winupdatepath + "\windowsupdate.log"))
 {
  Copy-Item ($winupdatepath + "\windowsupdate.log") -Destination ("\\SomeServer\Share\" + $server + "_windowsupdate.log")
  If ($LastExitCode -eq 0) {$ServerCount ++}
 } # End If

} # End Foreach

Write-Host "Total server WindowsUpdate.log copied into \\FileServer\SharedFolder is $ServerCount out of $($OnlineServers.Count) total online servers."

Open in new window

Comment
Watch Question

Support Escalation Engineer
Commented:
When you were using get-content, did it look as follows? If not, try it like this:
$OnlineServers = @(get-content "C:\serverlist.txt")

$ServerCount = 0

foreach($Server in $OnlineServers)
{
 $OS = Get-WMIObject Win32_OperatingSystem -ComputerName $Server 
 $winupdatepath = "\\" + $Server + "\" + (($os.windowsdirectory).replace(":", "$"))

 If (Test-Path ($winupdatepath + "\windowsupdate.log"))
 {
  Copy-Item ($winupdatepath + "\windowsupdate.log") -Destination ("\\SomeServer\Share\" + $server + "_windowsupdate.log")
  If ($LastExitCode -eq 0) {$ServerCount ++}
 } # End If

} # End Foreach

Write-Host "Total server WindowsUpdate.log copied into \\FileServer\SharedFolder is $ServerCount out of $($OnlineServers.Count) total online servers."

Open in new window

Author

Commented:
That worked! Thank you very much. I guess where I messed up was I did not place the get-content in the $OnlineServers = @(get-content "C:\serverlist.txt").
What I did (Which didnt work) was

$OnlineServers = @($Server)
$Server = Get-content C:\serverlist.txt

Thanks again for your help!