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