In the following Powershell example, how can I get $server to populate within line #5? I'm trying to save the results of a Get-EventLog query to an array for each $server loop iteration.
Example:
$serversArray = @("one", "two", "three")
ForEach ($server in $serversArray) {
echo $server "Variable array iteration populates here correctly"
$eventArray = @()
$eventArray += Get-EventLog -computerName $server #But not here, where I need it
$eventArray #Yet it populates from calling the second array correctly
}
- I've tried assigning the scope of the $server variable to global or script.
- I've researched other similar issues, but none that I could find had this circumstance
- I've tried different combinations of piping, quotes, back ticks, etc.
As always, thanks in advance.
Edit Ok, ISE was caching some of the variables (or something strange), as the above example began working after I restarted ISE. Anyways, the issue is with the $servers array input, which in the full script is from a MySQL query. If I statically assign the array with server names (like in the example above) the script works. If I use the input from the MySQL query, Get-EventLog fails with The network path was not found. So even though the server values look correct (and something could be expanding), it could be a text encoding issue, etc. Sorry to waste time, but discussing it has helped narrow it down. Here's the pertinent part of the actual script:
#Open SQL Connection
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$connectionString = "server=dbserver;uid=odbc;pwd=password;database=uptime;"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
#Get server names from db
$sqlGetServers = "select server_nm from servers limit 3;"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sqlGetServers, $connection)
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
$dataSet = New-Object System.Data.DataSet
$recordCount = $dataAdapter.Fill($dataSet, "sample_data")
$server_names = @()
$server_names += $dataSet.Tables["sample_data"]
#$server_names = @("server-1","server-2") <-- this works
#loop through array of server names
foreach($server in $server_names) {
$eventData = @()
$eventData += Get-EventLog -computerName $server -LogName System -Newest 10
foreach($event in $eventdata) {Do-Stuff}
}
$servers, which in the actual script are from a MySQL query. I edited the question to include the original script reflecting this.$server_names = $dataSet.Tables["sample_data"](don't make it an empty array, don't use+=. Then run$server_names | gmand look at what it is. There's probably a property that you can use (likely the column name) to access the actual string you want.$server_names | gmand usedselect, and it worked:$server_names += $dataSet.Tables["sample_data"]| Select -Expand "server_nm"Thank you again. Choosing your answer below because I still needed to pipe the output of the array toForEach-Objectfor it to work, which you originally suggested.