Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

" ForEach" loop problem

$
0
0

I wrote a script that read all users in specific group in AD and get there SamAccountName and put them in txt file, and then get there mail box to anther txt file here is the script:

 

$UserList="c:\ADuser.txt"

Clear-Content $UserList

# mail.txt >> list of users emails

$MailList="c:\mail.txt"

Clear-Content $MailList

# Group Name to search in AD

$GroupName="Delete_technician_MailBox"

 

# get user list "Members" group ONLY USERS and put the list in $UserList

Get-ADGroupMember $GroupName | where {$_.objectClass -eq "user"}| foreach {$_.SamAccountName}| Out-File $UserList

# Every line in $UserList I get the user MailBox and put it in $MailList

ForEach ($system in Get-Content $UserList)

{

Get-ADUser $system -Properties mail | foreach { $_.mail }| Out-File -FilePath $MailList -append

}

I get an error in "ForEach ($system in Get-Content $UserList)..."
Get-Content : Cannot find path 'C:\PowerShellScripts\Done\LiorTest1' because it does not exist.
At line:1 char:21
+ foreach ($system in Get-Content $UserList)
+                     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\PowerShellScripts\Done\LiorTest1:String) [Get-Content], ItemNotFound
   Exception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
LiorTest1 is the username in the group.
Need Help
Thanks

Viewing all articles
Browse latest Browse all 10624

Trending Articles