samiam41
asked on
Echo results in powershell script
Hey Experts. I have a simple powershell script that a fellow Expert on here helped me write. Now I need to find out how successful the script was but I'm not sure what the command/synatx is to output or echo the results.
Example, the script moves all of the computers in a text file from one OU to another OU. What I would like to know is which computers were moved and which ones did not. Any suggestions?
Thanks everyone!
Example, the script moves all of the computers in a text file from one OU to another OU. What I would like to know is which computers were moved and which ones did not. Any suggestions?
Thanks everyone!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to post the script so we can see if it already generates output of some type to the screen.
ASKER
Got stuck working on a problem with a driver controller battery failure. I should be back on this script tonight or tomorrow morning. Updates to follow. Thanks Experts!
ASKER
Here is the code for the PS script.
GC C:\log-or-text.file | Get-ADUser | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=x,DC=x'
ASKER
Thanks Experts for your suggestions. I am the level right below novice when it comes to Powershell scripting/coding so please provide an example using the script I provided in my previous post.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can try this..
Foreach ($User in GC C:\log-or-text.file) {
Try
{
Get-ADUser $User | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=x,DC=x' -ErrorAction Stop
Write-Host "Moved user $User"
}
Catch
{
Write-Host "Moved user $User - Error - $($_.Exception.Message)"
}
}
ASKER
Ok. This is getting a little frustrating. I just changed the executionpolicy setting so now the PSmove.ps1 script should run but I get this error:
The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\tools\PSmove.ps1:1 char:40
+ GC C:\tools\staleADpc1.log | Get-ADUser <<<< | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC= x,DC=x,
DC=x'-PassThru | Set-Content c:\tools\output.log
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\tools\PSmove.ps1:1 char:40
+ GC C:\tools\staleADpc1.log | Get-ADUser <<<< | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=
DC=x'-PassThru | Set-Content c:\tools\output.log
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
GC C:\tools\staleADpc1.log | Get-ADUser | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=ky,DC=x'-PassThru | Set-Content c:\tools\output.log
So what am I doing wrong here?
Add following line as a first line of script..
Import-Module ActiveDirectory
ASKER
Like this?
GC C:\tools\staleADpc1.log |Import-Module ActiveDirectory | Get-ADUser | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=ky,x=gov'-PassThru | Set-Content c:\tools\output.log
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ahhh... I see. Testing now.
ASKER
Subsun, where is the output log file for that script?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dang! That headache earlier really messed me up. I'm trying to move computer objects, not user. My apologies everyone for that oversight but as I listed in the OP, it needs to be related to computer accounts. I know it's a matter of changing a couple of values, but I'm not sure what they are.
Thanks everyone.
Thanks everyone.
ASKER
I think I got it.
Import-Module ActiveDirectory
Foreach ($Computer in GC C:\tools\staleADPC1.log) {
Try
{
Get-ADcomputer $Computer | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=x,DC=x' -ErrorAction Stop
Echo "Moved computer $Computer"
}
Catch
{
Echo "Moved computer $Computer - Error - $($_.Exception.Message)"
}
}
ASKER
Thanks for the help. We got a little side-tracked with user and computer variables being mixed up but I am very excited that I have a script to begin moving these stale accounts. I appreciate your help and time.
For example...
Open in new window
If you can post the script which you are using then we can give more details on error checking..