bm_1212
November 19, 2018, 10:55am
1
Hi
I am trying to get some details from a list of servers, and put the server name beside it.
But the only server name being returned is the last one on the list.
If anyone has any tips or hints about how to get the results for all servers that would be great
Get-Content "P:\Powershell\Friday Checks\Pi_servers.txt" |
ForEach-Object {
Get-PIBackupSummary -Connection ( Connect-PIDataArchive -PIDataArchiveMachineName $_ )
} |
Where-Object { $_ . StatusMessage -eq '[0] Success' } |
Select-Object @{Name = "Server" ;Expression = { $Server }} , status , statusmessage , type , backupend |
Export-Csv "P:\Powershell\Friday Checks\Log_Errors.csv" -NoTypeInformation -Encoding UTF8
Take a look at the closing foreach “}” placement. You need to move this to the end of the Select, then pipe to Export-csv.
bm_1212
November 19, 2018, 11:22am
3
[quote quote=125586]Take a look at the closing foreach “}” placement. You need to move this to the end of the Select, then pipe to Export-csv.
[/quote]
Hi Thanks for your reply , have made the change but still the same results, the last servername on the list is the only one returned for all results
Get-Content "P:\Powershell\Friday Checks\Pi_servers.txt" |
ForEach-Object {
Get-PIBackupSummary -Connection ( Connect-PIDataArchive -PIDataArchiveMachineName $_ ) |
Where-Object { $_ . StatusMessage -eq '[0] Success' } |
Select-Object @{Name = "Server" ;Expression = { $Server }} , status , statusmessage , type , backupend } |
Export-Csv "P:\Powershell\Friday Checks\Log_Errors.csv" -NoTypeInformation -Encoding UTF8
Does removing the export-csv give you the desired results in the console?
bm_1212
November 19, 2018, 11:30am
5
[quote quote=125597]Does removing the export-csv give you the desired results in the console?
[/quote]
Hi
Unfortunately not, results in console matches the output file
sample
Server
Status
StatusMessage
Type
BackupEnd
ho045s
0
[0] Success
Full
21/09/2018 17:45
ho045s
0
[0] Success
Incremental
22/09/2018 02:15
ho045s
0
[0] Success
Incremental
23/09/2018 02:15
js2010
November 19, 2018, 12:05pm
6
What is $server? I think you mean $_.server?
Here is one way, tweek it for your use.
This example, while trivial, shows how to create an object with data from an original CSV and adding data.
Not sure what information you have in your original csv file.
[pre]
Import-Csv C:\TEMP\test.csv | foreach {
$computer = Get-ADComputer $.server -Properties OperatingSystem
[psCustomObject] @{
NameFromFile = $ .server
NoteFromFile = $_.Note
OperatingSystem = $computer.operatingsystem
}
} | Export-Csv C:\TEMP\testResult.csv -NoTypeInformation
[/pre]
I think you need to do this differently using a ForEach loop (The code below is untested)
foreach ($computer in $(Get-Content "P:\Powershell\Friday Checks\Pi_servers.txt")) {
Get-PIBackupSummary -Connection (Connect-PIDataArchive -PIDataArchiveMachineName $computer ) |
Where-Object{ $_.StatusMessage -eq '[0] Success'} |
select @{name="ServerName";expression={$computer }},status,statusmessage,type,backupend }
I have used this technique for other commands that do not have the server name as a property. You could also create a PSObject in the code as well., I am pretty sure that objects are necessary. So use some of the code above for the PSObjs. Emphasis is mine in the code. Hope this helps or gets you closer to your objective. Good Luck
Quickly put together a solution that uses the PSObjects. This is a dumb example but it was tested.
$(
ForEach ($computer in $(gc C:\temp\srv.csv)) {
$result = Test-Connection $computer -Count 1
New-Object psobject -Property @{
ServerName = $computer
IPAddress = ($result.IPV4Address).tostring()
}
}
) | Export-Csv .\test.csv -NoTypeInformation
Good luck
bm_1212
November 20, 2018, 6:41am
10
have used this technique for other commands that do not have the server name as a property. You could also create a PSObject in the code as well., I am pretty sure that objects are necessary. So use some of the code above for the PSObjs. Emphasis is mine in the code. Hope this helps or gets you closer to your objective. Good
Thank you so much for your help (and everyone else !) This works exactly- now to try export it to csv!
Pretty sure you will need to use a PSObject since the Export-CSV command expects objects as input. The foreach loop is going to produce text strings.