samiam41
asked on
Export results to log file of commands in script
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.
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!
#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
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!
ASKER
Thanks mcsween. Can the command not be placed in the script?
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
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
or use this to append to the output filec:\path\to\script.ps1 >> c:\path\to\outputfile.txt
ASKER
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.
ASKER
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
Get-ADDomain
Then you can use the following command to launch the script and output to a file called c:\output.txtpowershell.exe -command "& 'c:\test.ps1' " > "C:\output.txt"
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
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.
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.
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.
Open in new window