Move all users to a different OU and output the results to csv file

lee roberts 186 Reputation points
2023-09-27T16:29:11.5633333+00:00

Good evening,

I'm trying to write a script that moves all user in one OU to another and then writes out the results to a CSV. This file needs to be stored on a file server in another domain, as its going to be used by another process that is being automated.

This is the script i have, which does move the user but i get the error below:

Start-Transcript -path C:\Lee\EmailMigLog.txt -append $password = Get-Content "\\Domain1\NETLOGON\cred.txt" | ConvertTo-SecureString -Key (Get-Content "\\Doamin1\NETLOGON\aes.key") $credential = New-Object System.Management.Automation.PsCredential("Domain1\Adminaccount",$password) $credential2 = New-Object System.Management.Automation.PsCredential("Domain2\Adminaccount",$password)  $ou1 = 'OU=test OU 1,OU=X-test Area,DC=Doamin1,DC=Local' $ou2 = 'OU=test OU 2,OU=X-test Area,DC=Domain2,DC=Local'  $AdUsers = Get-ADUser -Filter 'Enabled -eq "True"' -SearchBase $ou1  foreach($user in $AdUsers) {         Move-ADObject -Identity $user -TargetPath $ou2 } $nameOfUser = Invoke-Command -ComputerName Server1 -ScriptBlock {(Get-ADUser -Identity $using:username).Name} -Credential $credential $useremailaddress = Invoke-Command -ComputerName Server1 -ScriptBlock {(Get-ADUser -Identity $using:username -properties EmailAddress).EmailAddress}  -Credential $credential $SamaccountName = Invoke-Command -ComputerName Server1 -ScriptBlock {(Get-ADUser -Identity $using:username -properties Samaccountname).Samaccountname}  -Credential $credential $datemigrated = Get-Date -Format "dd/MM/yyyy" $timemigrated = Get-Date -Format "HH:mm:ss" $logline = $nameOfUser + "," + $Samaccountname + "," + $useremailaddress + "," + $datemigrated + "," +$timemigrated New-PSDrive -Name "MigLog" -Root "\\Domain2\share\MigrationLog" -PSProvider "FileSystem" -Credential $credential2 Add-Content -Path MigLog:\Emailmigrationlog.csv -Value $logline Remove-PSDrive -Name "MigLog" Stop-Transcript
Invoke-Command : The value of the using variable '$using:username' cannot be retrieved because it has not been set in the local session. At line:14 char:15 + ... ameOfUser = Invoke-Command -ComputerName server1 -ScriptBlock {(G ... +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], RuntimeException     + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand   Invoke-Command : The value of the using variable '$using:username' cannot be retrieved because it has not been set in the local session. At line:15 char:21 + ... iladdress = Invoke-Command -ComputerName server1 -ScriptBlock {(G ... +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], RuntimeException     + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand   Invoke-Command : The value of the using variable '$using:username' cannot be retrieved because it has not been set in the local session. At line:16 char:19 + ... countName = Invoke-Command -ComputerName server1 -ScriptBlock {(G ... +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], RuntimeException     + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand    Name           Used (GB)     Free (GB) Provider      Root                                                                                                                                                                                  CurrentLocation ----           ---------     --------- --------      ----                                                                                                                                                                                  --------------- MigLog                                 FileSystem    \\domain2\share...                                                                                                                                                                   Add-Content : Could not find a part of the path '\\domain2\share\MigrationLog\domain2\share\MigrationLog\Domain2\share\MigrationLog\Emailmigrationlog.csv'. At line:21 char:1 + Add-Content -Path MigLog:\Emailmigrationlog.csv -Value $logline + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : ObjectNotFound: (\\Domain2...igrationlog.csv:String) [Add-Content], DirectoryNotFoundException     + FullyQualifiedErrorId : GetContentWriterDirectoryNotFoundError,Microsoft.PowerShell.Commands.AddContentCommand   Transcript stopped, output file is C:\Lee\EmailMigLog.txt
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,476 Reputation points
    2023-09-27T19:09:24.0066667+00:00

    I don't see a variable named $username anywhere in your script. There's a $user variable in the foreach expression, though.

    Why are you using the users' "name", though? That's not a property that's acceptable as an identity value in the Get-ADUser cmdlet.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.