none
Results of script run written to file RRS feed

  • Question

  • Hi,

    long story short, i am the only one in my department with more than a little knowledge of Powershell, but its not much.  most recently I have been sent lists of computers(some that dont exist in Active Directory any more) that need to be added/removed to a cpr-deploy group... i got this part down.  There is one feature i would like to add to my script but Im not sure where to start.

    What I would like to add, is for the script to run and also create a text file or csv file with the results.. EX:

    Added to Deploy group Successfully
    Comp1
    Comp2
    Comp3
    ....

    Already has Group Membership
    Comp4
    Comp5
    Comp6

    Computer not in AD
    Comp7

    <# This script must be used in a particular fashion because of the way it is written #>
    <# Paste this script in the Script Pane of PowerShell ISE #>
    <# This script presupposes that you have two files created on your computer: #>
    <# "C:\Scripts\pcs.txt" and "C:\Scripts\groups.txt" #>
    <# If you desire for these text files to be in a different location, I reccommend keeping them on your C: drive #>
    
    <# This first section opens your text file and prompts you to add the pc namess to the text file #>
    <# Make sure there are no spaces, or extra tabs or lines; this would cause the script to fail #>
    <# Press enter after you have sanitized the list, saved the list, and closed the file #>
    A:\PoSh.tools\pcs.txt
    Write-Host ""
    Write-Host ""
    Write-Host ""
    Write-Host ""
    Write-Host ""
    Write-Host "**********************************************************************************************************************"
    Write-Host "Enter Computer Names into the text file, remove unneeded spaces or lines, save the file, and close the file" -foregroundcolor "green"
    Read-Host -Prompt "Press Enter to continue"
    
    <# This first section opens your text file and prompts you to add the AD Groups to the text file #>
    <# Make sure there are no spaces, or extra tabs or lines; this would cause the script to fail #>
    <# Press enter after you have sanitizedthe list, saved the list, and closed the file #>
    A:\PoSh.Tools\groups.txt
    Write-Host "**********************************************************************************************************************"
    Write-Host "Enter groups into the text file, remove unneeded spaces or lines, save the file, and close the file" -foregroundcolor "green"
    Read-Host -Prompt "Press Enter to continue"
    
    <# This creates variables of the content of the text files #>
    $pcs = Get-Content -Path A:\PoSh.Tools\pcs.txt
    $groups = Get-Content -path A:\PoSh.Tools\groups.txt 
     
    <# This does all of the work #>
    <# -Confirm:$false so that you dont have to confirm it 2000 times #> $pcs | ForEach-Object {Get-ADComputer $_ | Add-ADPrincipalGroupMembership -MemberOf $groups -Confirm:$false} <# Clears the text files after use; this is not needed for the script to run properly #> Clear-Content -Path A:\PoSh.Tools\pcs.txt Clear-Content -Path A:\PoSh.Tools\groups.txt Write-Host "Task Complete" -ForegroundColor "green"

    I have a few different variations of this script also for removing groups and also adding/removing users. and the script looks identical with the exception of a few words, so if possible I would like to add this new feature without changing to much.

    I would like to add it to this so that I can report back to my superiors or whoever may need it how many computers have been added/removed and which ones are non existant

    any help/guidance/solutions you may have to offer would be greatly appreciated

    Thank you

    • Moved by Bill_Stewart Tuesday, December 11, 2018 11:23 PM This is not "debug/fix/rewrite my script for me" forum
    Tuesday, August 7, 2018 3:21 PM

All replies

  • You will have t add outputs to a file that save what you are trying to save.

    help Out-File -online
    help Append-File -online


    \_(ツ)_/

    Tuesday, August 7, 2018 3:33 PM