Assigning object property inside a foreach loop; data does not persist outside foreach loop. Why not? INT and STRING ass
Date : March 29 2020, 07:55 AM
I wish this help you It seems that weekly_report_dm->getProjectHours($options) returns reference to same private (?) property , which changes each call. You can try to clone the result with clone : $data[$i]->data->proj_hours = clone $weekly_report_dm->getProjectHours($options);
|
Write-Output Failing Inside Foreach Loop - Powershell
Date : March 29 2020, 07:55 AM
Hope this helps I am new to Powershell and I am wondering why this function doesn't work the way I want it too. The following function uncomments line in the hosts file of the computer specified. It works great! But I want the ability to output each line to the screen using a Write-Output and get the variable and store it in another array. This Write-Output doesn't do anything and I cannot add it to the array. Why? The above Write-Output works just fine. , To answer your question, when you do this: $hosts = $hosts | Foreach {...}
|
How can I write an if statement using a variable in a foreach loop using Powershell?
Date : March 29 2020, 07:55 AM
I hope this helps . Declare an array before the foreach and add messages. Then call Send-MailMessage after your foreach. Example: $errors = @()
foreach ($server in $servers) {
$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $server
$DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
$SUTC = $DateTime.ToUniversalTime()
$now = Get-Date
$NUTC = $now.ToUniversalTime()
$Difference = (New-TimeSpan -Start ($NUTC) -End ($SUTC))
Write-Host "Time at $server is $SUTC. Time difference of $Difference."
if ($Difference -ge '00:03:00') {
$errors += "Time difference of more then 3 minutes detected."
}
}
if($errors.Count -gt 0) {
Send-MailMessage -Subject "Time Report -$(get-date -Format "MM-dd-yyy")" -Body ($errors -join "`n") -SmtpServer "SMTPSERVER" -From "admin@firm.com" -To "group@firm.com" -UseSsl
}
|
Powershell advance through an array in a ForEach loop (or nested Foreach)
Date : March 29 2020, 07:55 AM
wish of those help Folks, , Just nest another foreach: $x = 2
foreach ($Server in $Servers) {
$infos = Invoke-Command -computername $Server { Get-ChildItem "V:\Service\*" -File -Include *.dll, *.ocx | Select-Object name,@{label="FileVersion";expression={$_.versioninfo.fileversion}},@{label="Date";expression={$_.LastWriteTime}}}
foreach ($info in $infos) {
$sheet.Cells.item($x, 1)=($Server)
$sheet.Cells.item($x, 2)=($info.name)
$sheet.Cells.item($x, 3)=($info.FileVersion)
$sheet.Cells.item($x, 4)=($info.Date)
$x++
}
}
|
How to compare to string in PowerShell foreach loop to files
Date : March 29 2020, 07:55 AM
wish of those help Your comparison is wrong. And will return $true causing all files to be read It should be if ($month -eq $curr_month)
$curr_month = (get-date).month
|