header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
File being over written
Last Post 22 Jul 2010 02:16 AM by James. 11 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JamesUser is Offline
Basic Member
Basic Member
Posts:374
Avatar

--
16 Jul 2010 03:35 AM

    Hello,

    I have the following code and its overwritting the file each time opposed to ammending it... can anyone see why this is or how to fix it?



    $Global:Date = Get-Date -Format d
    $FileDate = $Date -replace "/","-"
    $File = "C:\Scripts\" + $FileDate + " 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){    
    $Create = $User.WhenCreated.ToShortDateString()
    if ($Create -eq $Date){

    RandomPassword

    $DN = $User.DN
    $dn1 = $DN -replace "CN=.*,",""
    $School = $DN1 -replace ",OU=",""
    $User1 = $User -replace "teststuffhere\\" , ""

    Set-QADUser $User -UserPassword $Password `
                      -PasswordNeverExpires $False `
                      -UserMustChangePassword $true |
    Select-Object Title,@{n='School';e={ $School }}, @{n='User';e={ $User1 }}, @{n='Password';e={ $Password }} |
    Out-File $File}}

    $Users = $null



    Many Thanks

    James

    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    16 Jul 2010 04:39 AM
    Hello,

    I have used the append switch for Out-File and that gets the information however the format is wrong each time it finds one it does the following:

    Title School User Password
    ----- ------ ---- --------


    Title School User Password
    ----- ------ ---- --------


    The information below however I get the headings each time however I just want the information underneith opposed to each piece of information having a header...

    Does anyone know how I can do this?

    Many Thanks

    James
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    16 Jul 2010 07:26 AM
    Use the -Append switch on Out-File

    Out-File -FilePath "myfile.txt" -Append
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    16 Jul 2010 07:38 AM
    Hello,

    I have done wht you have suggested and I am getting the output of:

    Title School User Password
    ----- ------ ---- --------
    Data1

    Title School User Password
    ----- ------ ---- --------
    Data2


    Opposed to:

    Title School User Password
    ----- ------ ---- --------
    Data1
    Data2
    Data3

    Is there anything wrong with how I am doing things?

    Many THanks

    James
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    16 Jul 2010 10:21 AM
    The cmdlets that format data do so on a per invocation basis. That means the second time you call Out-File it has no way of knowing what it did the last time it was invoked. You'll want to pipe all the objects you want written at one time to Out-File rather than one at a time. The formatting of your original post had a lot of html junk so I had a hard time reading the sample code. I'll post back in a few minutes if I can provide a more concrete suggestion.
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    16 Jul 2010 10:52 AM

    Try this:

    $Date = Get-Date -Format d $FileDate = $Date -replace '/', '-' $File = "C:\Scripts\${FileDate} Passwords.txt" Get-QADUser -Title student, parent -SizeLimit 0 -IncludedProperties whenCreated, DN | Where-Object -Filter { $_.WhenCreated.ToShortDateString() -eq $Date } | Foreach-Object -Process { $Password = New-Password -Length 8 -UpperCase -LowerCase -Numbers $DN = $_.DN $DN1 = $DN -replace 'CN=.*,', '' $School = $DN1 -replace ',OU=', '' $User1 = $_ -replace 'teststuffhere\\', '' $null = Set-QAUser -Identity $_ -UserPassword $Password -PasswordNeverExpires $False -UserMustChangePassword $True New-Object -TypeName PSObject | Add-Member -PassThru -MemberType NoteProperty -Name School -Value $School | Add-Member -PassThru -MemberType NoteProperty -Name User -Value $User1 | Add-Member -PassThru -MemberType NoteProperty -Name Password -Value $Password } | Out-File -FilePath $File
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    16 Jul 2010 12:12 PM
    Hey,

    Thanks for the reply I will give it a go first thing monday morning! :)

    During my fiddling I starting looking into the add-member and such and I had something similar to :


    $objResults = @() $obj = New-Object -TypeName PSObject $obj | Add-Member -MemberType NoteProperty -Name School -Value $School $obj | Add-Member -MemberType NoteProperty -Name User -Value $User1 $obj | Add-Member -MemberType NoteProperty -Name Password -Value $Password $objresults += $obj $objresults | out-file $File

    But still it over written lol So I was kinda along the same lines by the end of the day!

    So see what Monday Brings and I will post back!

    Many Thanks

    James



    Many Thanks

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

    --
    19 Jul 2010 07:34 AM
    Hello,

    I have got the following woring thanks to your example:

    $Global:Date = Get-Date -Format d $FileDate = $Date -replace "/","-" $File = "C:\Scripts\${FileDate} Passwords.txt" Get-QADUser -Title "student","parent" ` -SizeLimit 0 ` -includedproperties whenCreated, DN | Where-Object -Filter {$_.WhenCreated.ToShortDateString() -eq $Date } | ForEach-Object { $Password = New-Password -length 8 -upperCase -lowerCase -numbers $User = $_.SamAccountName $DN = $_.DN $dn1 = $DN -replace "OU=","" $School = $dn1 -replace ",OU=foo1,DC=foo,DC=co,DC=uk","" $User1 = $User -replace "stufftoreplace" , "" Set-QADUser -Identity $User1 ` -UserPassword $Password ` -PasswordNeverExpires $False ` -UserMustChangePassword $true | Select-Object Title,@{n='School';e={ $School }}, @{n='User';e={ $User1 }}, @{n='Password';e={ $Password }} | Sort-Object School ; Ascending=$true } | Out-File $File

    However I am unable to sort by school....

    Does anyone know what I am doing wrong?

    Many Thanks

    James
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    19 Jul 2010 08:40 PM

    You are running Sort-Object inside the Foreach-Object. That means you are sorting a collection with s a single object. You need to Sort-Object after the Foreach-Object and before the Out-File:

    ... | Foreach-Object { ... } | Sort-Object -Property School | Out-File -File $File

    Sort-Object sorts Ascending by default. You can use the -Descending parameter to reverse the order.

    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    20 Jul 2010 12:48 AM
    Hello,

    Many Thanks for your reply I have the following code:

    $Global:Date = Get-Date -Format d $FileDate = $Date -replace "/","-" $File = "C:\Scripts\${FileDate} Passwords.txt" Get-QADUser -Title "student","parent" ` -SizeLimit 0 ` -includedproperties whenCreated, DN | Where-Object -Filter {$_.WhenCreated.ToShortDateString() -eq $Date } | ForEach-Object { $Password = New-Password -length 8 -upperCase -lowerCase -numbers $User = $_.SamAccountName $DN = $_.DN $dn1 = $DN -replace "OU=","" $School = $dn1 -replace ",OU=foo1,DC=foo,DC=co,DC=uk","" $User1 = $User -replace "stuffhere\\" , "" Set-QADUser -Identity $User1 ` -UserPassword $Password ` -PasswordNeverExpires $False ` -UserMustChangePassword $true | Select-Object Title,@{n='School';e={ $School }}, @{n='User';e={ $User1 }}, @{n='Password';e={ $Password }} } Sort-Object -Property School , User | Out-File $File

    Its not working though :( I am getting the error of:

    ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "Sort-Obje
    ct" value of type "System.String" to type "System.Management.Automation.ScriptB
    lock".
    At line:5 char:15
    + ForEach-Object <<<< {
    + CategoryInfo : InvalidArgument: (:) [ForEach-Object], Parameter
    BindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
    ell.Commands.ForEachObjectCommand

    Do you know what could be wrong?

    Many Thanks

    James
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    20 Jul 2010 01:49 AM

    You forgot the pipeline separator between the Foreach-Object process block and the Sort-Object cmdlet:

    } Sort-Object -Property School , User | Out-File $File

    Should look like this:

    } | Sort-Object -Property School , User | Out-File $File
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:374
    Avatar

    --
    22 Jul 2010 02:16 AM
    Ahhh well spotted!

    Many Thanks for that its working like a dream.

    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