Solved

Echo results in powershell script

Posted on 2013-06-04
18
641 Views
Last Modified: 2013-06-11
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!
0
Comment
Question by:samiam41
  • 10
  • 5
  • 2
  • +1
18 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39220112
You can use Try Catch..
For example...
Try{
<your script>
Write-host "Moved the computer $comp"
}
Catch{
Write-host "error moving computer $comp"
}

Open in new window

If you can post the script which you are using then we can give more details on error checking..
0
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 50 total points
ID: 39220315
you need to add applicable write-host or out-file items within the script itself but you need to have some form of output then you can use standard redirection i.e.
powershell -noprofile myscript.ps1 > output.txt or even pipe the output into the input of another script.

i.e.
powershell -noprofile myscript.ps1 | .\process-list.ps1 | .\send-output.ps1
0
 
LVL 10

Expert Comment

by:CSI-Windows_com
ID: 39220866
You need to post the script so we can see if it already generates output of some type to the screen.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 9

Author Comment

by:samiam41
ID: 39227046
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!
0
 
LVL 9

Author Comment

by:samiam41
ID: 39229303
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'

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39229309
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.
0
 
LVL 10

Assisted Solution

by:CSI-Windows_com
CSI-Windows_com earned 100 total points
ID: 39229371
add "-passthru" like this:

GC C:\log-or-text.file | Get-ADUser | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=x,DC=x' -PassThru

To send to a log file add "set-content" like this:

GC C:\log-or-text.file | Get-ADUser | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=x,DC=x' -PassThru | Set-Content c:\full\path\to\log\file.log
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39230713
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)"
 }
}

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39238113
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

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

Open in new window

So what am I doing wrong here?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39238119
Add following line as a first line of script..
Import-Module ActiveDirectory

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39238159
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

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 350 total points
ID: 39238194
If you are trying to echo results then...
Import-Module ActiveDirectory
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)"
 }
}

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39238198
Ahhh...  I see.  Testing now.
0
 
LVL 9

Author Comment

by:samiam41
ID: 39238402
Subsun, where is the output log file for that script?
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 350 total points
ID: 39238438
For now it just displays the details on console.. if you want to save the result to log file.. You can change the command Write-Host to Echo in script and then..
Run the script using
.\Script.ps1 >C:\log.txt
0
 
LVL 9

Author Comment

by:samiam41
ID: 39238634
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.
0
 
LVL 9

Author Comment

by:samiam41
ID: 39238668
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)"
 }
}

Open in new window

0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39238737
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.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Move windows 10 users 8 51
Powershell 5 Save-Module Access to Path is denied help please 2 29
Server timing 4 19
LastLogonDate for 10000 users in a csv file 2 11
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question