header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Exporting Info Is not working
Last Post 07 Jul 2010 08:07 AM by James. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
JamesUser is Offline
Basic Member
Basic Member
Posts:374
Avatar

--
06 Jul 2010 03:17 AM

    Hello,

    I have the following code:



    # Get all users created today and give them random passwords

    $Global:Date = Get-Date -Format d

    function RandomPassword {
    $Global:Password = new-password -length 8 -upperCase -lowerCase -numbers
    }

    Import-Csv "C:\Powershell\CSV\SchoolsList.csv"  | ForEach-Object {

    $School = $_.School
    $ou = 'OU=foo2,OU=' + $School + ',OU=foo1,DC=foo,DC=CO,DC=UK'

    $Users = Get-QADUser -Title "student","parent" `
                -SearchRoot $ou `
                -SizeLimit 0 `
                -includedproperties whenCreated           
    foreach ($User in $Users){$Create = $User.WhenCreated.ToShortDateString
    if ($Create -eq $Date){
    RandomPassword
    Set-QADUser $User -UserPassword $Password -PasswordNeverExpires $False -UserMustChangePassword $true |
    Select-Object title,@{n='OU';e={ $ou }}, @{n='User';e={ $User }}, @{n='Password';e={ $Password }}|
    Out-File "C:\Scripts\Passwords.txt"}}}



    Basically it all works apart from for some reason it will not export the information out... It created the file however nothing is ever in it I know there are two users created today and I am using this on the test network.

    Does anyone know why the info is not exporting out?

    Many Thanks

    James

    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    07 Jul 2010 03:24 AM
    Does anyone have any suggestions its driving me nuts!

    I have tried moving the curley brackets around but I get an Empty Pipeline error and tried putting the information into a variable and exporting thatout with no luck.

    Any suggestions are very welcome!

    Many Thanks

    James
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1643
    Avatar

    --
    07 Jul 2010 04:31 AM
    You have some special characters in your code that are making it hard to read what you have. I'm not sure if your user account has permissions, but can you try attaching your script instead?
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:451
    Avatar

    --
    07 Jul 2010 07:01 AM
    James;

    Your script did not post completley. As Marco says, either attach it, or repaste it.

    This is what shows up, after removing the HTML formatting codes:
    # Get all users created today and give them random passwords $Global:Date = Get-Date -Format d function RandomPassword {$Global:Password = new-password -length 8 -upperCase -lowerCase -numbers} Import-Csv "C:\Powershell\CSV\SchoolsList.csv" | ForEach-Object { $School = $_.School$ou = 'OU=foo2,OU=' + $School + ',OU=foo1,DC=foo,DC=CO,DC=UK' $Users = Get-QADUser -Title "student","parent" `

    Karl
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    07 Jul 2010 07:11 AM
    Hello,

    I have got it sorted now however I have been asked to change it slightly so its being tested currently and its to get the current date in the file name:

    # Get all users created today and give them random passwords $Global:Date = Get-Date -Format d $File = "C:\Scripts\" + $Date + " Passwords.txt" function RandomPassword { $Global:Password = new-password -length 8 -upperCase -lowerCase -numbers } $Users = Get-QADUser -Title "student","parent" ` -SizeLimit 0 ` -includedproperties whenCreated foreach ($User in $Users){ $User $Create = $User.WhenCreated.ToShortDateString() if ($Create -eq $Date){ RandomPassword Set-QADUser $User -UserPassword $Password -PasswordNeverExpires $False -UserMustChangePassword $true | Select-Object @{n='OU';e={ $ou }}, @{n='User';e={ $User }}, @{n='Password';e={ $Password }}| Out-File $File}}

    Many Thanks

    James

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    07 Jul 2010 07:16 AM
    Sorry I also need to pull the OU its in too as I have changed my original code! I cant seem to find how to do it :(

    Do I have to add in the DN or something?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    07 Jul 2010 08:07 AM
    I have sorted it now after some fiddling might not be a proper way but it works!

    My code:

    # Get all users created today and give them random passwords $Global:Date = Get-Date -Format d $NewDate = $Date -replace "/","-" $File = "C:\Scripts\" + $NewDate + "Passwords.txt" function RandomPassword { $Global:Password = new-password -length 8 -upperCase -lowerCase -numbers } $Users = Get-QADUser -Title "student","parent" ` -SizeLimit 0 ` -includedproperties whenCreated, DN foreach ($User in $Users){ $User $Create = $User.WhenCreated.ToShortDateString() if ($Create -eq $Date){ RandomPassword Set-QADUser $User -UserPassword $Password -PasswordNeverExpires $False -UserMustChangePassword $true | Select-Object @{n='OU';e={ $_.DN }}, @{n='User';e={ $User }}, @{n='Password';e={ $Password }}| Out-File $File}}

    Many Thanks

    James
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer