Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Export results to log file of commands in script

Posted on 2014-10-07
10
Medium Priority
?
241 Views
Last Modified: 2014-10-30
Hey Experts.  Here is a script that I found and am trying to tweak.  I want to echo the results of the script to a log file so I can review it and see how well the script ran.

#Specify the OU you want to search for inactive accounts 
 
    $SearchOU=“ou=depts,dc=12" 
 
#Specify the OU you want to move your inactive computer accounts to 
 
    $DestinationOU=“ou=inactive,dc=12" 
 
#Specify the number of days that computers have been inactive for 
 
    $NumOfDaysInactiveFor = 60
     
#Specify the description to set on the computer account 
 
    $Today = Get-Date 
     
    $Description = "Account disabled due to inactivity on $Today" 

Get

 
#DO NOT MODIFY BELOW THIS LINE 
 
Get-QADComputer -InactiveFor $NumOfDaysInactiveFor -SizeLimit 0 -SearchRoot $searchOU -IncludedProperties ParentContainerDN | foreach {  
 
    $computer = $_.ComputerName 
    $SourceOU = $_.DN 
     
    #Remove the commented # from the next line if you want to set the description to be the source OU 
    #$Description = "SourceOU was $SourceOu" 
     
    Set-QADComputer $computer -Description $Description 
 
    Disable-QADComputer $computer 
 
    Move-QADObject $computer -NewParentContainer $destinationOU  

Open in new window


The log would show which computer(s) were disabled and moved to the Inactive OU.  I'm figuring this would be pretty easy for some PS guru but that isn't me so your help is greatly appreciated!
0
Comment
Question by:samiam41
  • 6
  • 3
9 Comments
 
LVL 22

Expert Comment

by:mcsween
ID: 40366823
Open Powershell prompt:
c:\path\to\script.ps1 | tee-object c:\path\to\outputfile.txt

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 40366828
Thanks mcsween.  Can the command not be placed in the script?
0
 
LVL 22

Expert Comment

by:mcsween
ID: 40366831
You define the output when you call the script so you cannot place it within the script.  You could however create a second script with that command in it which  you can call.

Also, if you don't want the output on the screen and only in the file you can use this instead to overwrite
c:\path\to\script.ps1 > c:\path\to\outputfile.txt

Open in new window

or use this to append to the output file
c:\path\to\script.ps1 >> c:\path\to\outputfile.txt

Open in new window

0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
LVL 9

Author Comment

by:samiam41
ID: 40366862
Gotcha.  Yeah I just want the results to show up in the log file, not on screen.  I'll test with the information you provided.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40369298
I couldn't get the PS script to work as their was some sort of error relating to not having the AD piece installed or enabled or whatever.  I wrote a batch script and never looked back at PS.

I have no idea if that works but believe that it does.  I will ask a mod to verify so that you can get the proper credit rather than delete the question out on you since I can't determine whether or not it does.  I'd rather you have the points AND a verified answer.

Hope that you understand.  Thanks!
0
 
LVL 22

Accepted Solution

by:
mcsween earned 2000 total points
ID: 40369764
If you want to use that powershell script you need to install Quest AD tools
http://www.quest.com/powershell/activeroles-server.aspx

Add this line to the top of your script (not the batch file)
Add-PSSnapin Quest.ActiveRoles.ADManagement

Open in new window


Then you would run the script from a batch file like this
powershell.exe -command "& 'c:\path\to\script.ps1' " > "C:\path\to\outputfile.txt"

Open in new window

0
 
LVL 22

Expert Comment

by:mcsween
ID: 40370921
So you can test until you get your original script running do the following.  Save the following line of code that calls  a single cmdlet to a new file C:\test.ps1.
Get-ADDomain

Open in new window

Then you can use the following command to launch the script and output to a file called c:\output.txt
powershell.exe -command "& 'c:\test.ps1' " > "C:\output.txt"

Open in new window

My original post assumed you were running this script from within a powershell prompt.  This one assumes you are running from a cmd window, run dialogue, or task scheduler.

Also make sure you execution policy is set to unrestricted or these scripts will not run.
Open Powershell Prompt and issue Get-ExecutionPolicy cmdlet.  If it returns anything but Unrestricted issue this command.
Set-ExecutionPolicy Unrestricted

Open in new window

0
 
LVL 22

Expert Comment

by:mcsween
ID: 40406236
Comment http:#a40366831 should be accepted as the answer as it directly answers the author's original question of how to save script output to a text file.
0
 
LVL 22

Expert Comment

by:mcsween
ID: 40406239
My last commend just disappeared so re-posting.

Commend http:#a40369764  should be the accepted answer with a grade of A as it directly answered the author's original question of how to save script output to a text file.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

877 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