Thanks
You aren’t outputting anything. Usually an object array is made like this. Use the PRE button for code.
$list = 1,2,3
$result = foreach ($i in $list) {
[pscustomobject]@{num = $i
nextnum = $i + 1
}
}
$result
num nextnum
--- -------
1 2
2 3
3 4
Don’t see why?
My output shall be from the import and when I am doing foreach I want to save the result in a variable as I am trying.
Please use PRE tags to format code. Here are two options that you can use, both are stored in the $results variable:
#Emulate a CSV import
$users = @()
$users += [pscustomobject]@{FirstName = 'Jim';LastName = 'Johnson'}
$users += [pscustomobject]@{FirstName = 'Sally';LastName = 'Smith'}
$users += [pscustomobject]@{FirstName = 'Tommy';LastName = 'Sampson'}
#Method 1 - Appending to the current object
#Use calculated expressions to get all of the current properties of the user
#with a Select and then add properties with the calculated expression
$results = foreach ( $user in $users ) {
$login = '{0}{1}' -f $user.FirstName.SubString(0,1), $user.LastName
$user | Select *,
@{Name='Login';Expression={$login}},
@{Name='Date';Expression={Get-Date}}
}
#Method 2 - Generate a new object
#Build a new PSObject from scratch using the [pscustomobject] accelerator
$results = foreach ( $user in $users ) {
$login = '{0}{1}' -f $user.FirstName.SubString(0,1), $user.LastName
[pscustomobject]@{
FirstName = $user.FirstName
LastName = $user.LastName
Login = $login
Date = Get-Date
}
}
Output:
PS C:\WINDOWS\system32> $results FirstName LastName Login Date --------- -------- ----- ---- Jim Johnson JJohnson 12/4/2019 9:36:29 AM Sally Smith SSmith 12/4/2019 9:36:29 AM Tommy Sampson TSampson 12/4/2019 9:36:29 AM
Assignment statements don’t output anything.
foreach ($i in 1) {
$a = 'hi'
$b = 'there'
$c = 'how are you?'
}
# no output
Thanks
I have test method 2 and it works like a charm ![]()
AS for the final script ![]()
Clear-Host
cd E:\automation\kuafUsers
Remove-Item .\final.csv -Force
#Start to calculate the day of month
$Year = (get-date).Year
$Month = (get-date).Month -1
$StartOfMonth = Get-Date -Year $Year -Month $Month -Day 1
#Importing and cleaning character content in rows depending on selection
$users = Import-Csv '.\KUAF Users dates (w defgroup).csv' -Delimiter ";" -Encoding UTF8 | Where {$_.Deleted -ge ($StartOfMonth).ToString('yyyy-MM-dd') -or $_.Deleted -eq "?"}
$results = foreach ( $user in $users ) {
$login = '{0}{1}' -f $user.FirstName.SubString(0,1), $user.LastName
[pscustomobject]@{
ID = $user.ID
NAME = $user.NAME -replace 'corp\\','' -replace 'dfns\\','' -replace '\(Delete\).*',''
LASTNAME = $user.LASTNAME -replace '\(AU\).*',''
FIRSTNAME = $user.FIRSTNAME
TITLE = $user.TITLE
GROUPNAME = $user.GROUPNAME
CREATED = $user.CREATED
DELETED = $user.DELETED
LATEST_LOGIN = $user.LATEST_LOGIN
}
}
$results | Export-Csv .\final.csv -Delimiter ";" -Encoding UTF8 -NoTypeInformation
clear-host
#Deleting selected rows depending on selection
$csv = import-csv ".\final.csv" -Delimiter ";" -Encoding UTF8
$nodelete = foreach($line in $csv){
if(-not($line.GROUPNAME -like '*XAD-CFIS*') -and `
(-not($line.GROUPNAME -like '*ORG-CFI*') -and `
(-not($line.GROUPNAME -like '*XAD-P*') -and `
(-not($line.NAME -like '*adm*') -and `
(-not($line.NAME -like '*admin*') -and `
(-not($line.NAME -like '*op5*'))))))){
$line
}
}
#Creating final Excel file
$nodelete | select NAME, LASTNAME, FIRSTNAME `
| sort NAME `
| Export-Excel "E:\automation\kuafExcel\Debitering $((Get-Date).ToString('yyyy-MM')).xlsx"
#Remove-Item ".\final.csv" -Force
#Now it's time to send the result to right instance.
Send-MailMessage -From 'mail@mail.com' `
-To 'epost@epost.com' `
-Subject 'Hej, Vänligen bekräfta när ni läst in data. Stort tack för hjälpen!' -Encoding "UTF8" `
-Attachments "E:\automation\kuafExcel\Debitering $((Get-Date).ToString('yyyy-MM')).xlsx" `
-SmtpServer 'smtp.test.com'