none
Need Help in Script

    Question

  • Hi,

    I have a scipt which upload the pictures in Active directory(Tumbnail attribute),i need help to make some changes in this script.

    -I don't want any option to come while running this script.Like  in this script whenever i run it ,options come

    "Warning  ## This action will upload all pictures of C:\Photos to the AD users.
     Warning  ## All pictures must have the same name of the usernames.
     Warning "Are you sure that you want upload all pictures to the users (Y/N)"

    So unless i don't hit 'y' script doesn't run.So don't want this ,script should run as soon as i run it through command line.

    Please help as i have no idea where changes should made.Sharing the script.

    Thanks as always.

    #
    # Script created by Anderson Patricio (<a href="http://msmvps.org/blogs/AndersonPatricio">http://msmvps.org/blogs/AndersonPatricio</a>)
    #
    # Syntax:
    # uploadphoto.ps1 <mailbox-name>
    # uploadphoto.ps1 -all (It will load all jpg files from the default folder and upload to the users in AD
    #
    param([Switch]$all, [String]$UserName)

    #Default Values. Change them based on your environment.
    $DefaultPhotoPath = 'C:\Photos'

    Function CheckPhoto(){
     Write-Warning "Validating file(s).."
     Write-Host "File exists... " -nonewline
     If (Test-Path $PhotoPath)
      {
      Write-Host "[OK]" -ForeGroundColor Green
      Write-host "Photo size... "  -nonewline
      $PhotoSize = Get-ChildItem $PhotoPath | select Length
      If ($PhotoSize.Length -le 10000) { Write-Host "[OK]" -ForeGroundColor Green } Else { Write-Host "[Fail]" -ForeGroundColor Red; exit }
      }
     Else
      {
      Write-Host "[Fail]" -ForeGroundColor Red
      Exit
      }
    }

    Function UploadAll(){
     ForEach ($TempFile in Get-ChildItem $DefaultPhotoPath | Where-Object { $_.Extension -eq ".jpg" } )
      {
       $TempUserName = $TempFile.Name.substring(0, $TempFile.Name.Length - 4)
       Write-Host $TempUserName -ForeGroundColor Yellow -NoNewLine
       Import-RecipientDataProperty -Identity $TempUserName -Picture -FileData ([Byte[]]$(Get-Content -path $TempFile.Fullname -Encoding Byte -ReadCount 0))
       Write-Host "[Done]" -ForeGroundColor Green
      }
    }

    If ( $all -eq $true)
     {
     Write-Warning " ## This action will upload all pictures of C:\Photos to the AD users."
     Write-Warning " ## All pictures must have the same name of the usernames"
     Write-Warning "Are you sure that you want upload all pictures to the users (Y/N)?"
     $Opt = Read-Host
     If ( $Opt -eq 'y' ) { UploadAll; } Else { Write-Host "No changes were made."; Exit }
     }
    Else
     {
     $PhotoPath = $DefaultPhotoPath + $UserName + '.jpg'
     CheckPhoto;
     If ( $AbortMission -eq '$true' ) { Write-Error "Please, review the errors and try again." } Else { Import-RecipientDataProperty -Identity $UserName -Picture -FileData ([Byte[]]$(Get-Content -path $PhotoPath -Encoding Byte -ReadCount 0)) }
     }

     

    Friday, June 17, 2011 3:42 PM

Answers

  • Hi,

    Change these lines:

    Write-Warning " ## This action will upload all pictures of C:\Photos to the AD users."
    Write-Warning " ## All pictures must have the same name of the usernames"
    Write-Warning "Are you sure that you want upload all pictures to the users (Y/N)?"
    $Opt = Read-Host
    If ( $Opt -eq 'y' ) { UploadAll; } Else { Write-Host "No changes were made."; Exit }

    to this:

    UploadAll

    Use caution as this bypasses the warning mechanism that the script's author created.

    HTH,

    Bill

    Friday, June 17, 2011 3:50 PM
  • Hi,

    You can redirect output using a command like this:

    UploadPhoto.ps1 -all | out-file logfile.txt

    However, note that lines in the script that use Write-Host and Write-Warning will not be saved to the log file.

    Bill

    Friday, June 17, 2011 4:02 PM
  • Building on Bills earlier suggestion, you will not only have to remove the write-message and write-errors, but you will have to also remove any of the parameters, such as -foregroundcolor or -nonewline.
    Paul Frankovich
    Friday, June 17, 2011 4:52 PM

All replies

  • Hi,

    Change these lines:

    Write-Warning " ## This action will upload all pictures of C:\Photos to the AD users."
    Write-Warning " ## All pictures must have the same name of the usernames"
    Write-Warning "Are you sure that you want upload all pictures to the users (Y/N)?"
    $Opt = Read-Host
    If ( $Opt -eq 'y' ) { UploadAll; } Else { Write-Host "No changes were made."; Exit }

    to this:

    UploadAll

    Use caution as this bypasses the warning mechanism that the script's author created.

    HTH,

    Bill

    Friday, June 17, 2011 3:50 PM
  • Grt.....man

    Thanks for the quick reponse,i apprecitate,,,,,,,,,,You guys are really genius.............

    One more question,how can i write the output of this script to any text file..For the logs purpose.

     

    Thanks as always.

    Friday, June 17, 2011 3:57 PM
  • Hi,

    You can redirect output using a command like this:

    UploadPhoto.ps1 -all | out-file logfile.txt

    However, note that lines in the script that use Write-Host and Write-Warning will not be saved to the log file.

    Bill

    Friday, June 17, 2011 4:02 PM
  • So how can i achieve that.

    Please help log file is blank

    Friday, June 17, 2011 4:21 PM
  • Hi,

    You will need to edit the script to replace the Write-Host and Write-Warning calls with strings. That is, instead of

    Write-Host "This is a message"

    and

    Write-Warning "This is a message"

    You can use

    "This is a message"

    and

    "WARNING: This is a message"

    Bill

    Friday, June 17, 2011 4:23 PM
  • I tried but not writing the output to the text file. Can u send me the complete script after making the changes in it.
    Friday, June 17, 2011 4:42 PM
  • Hi,

    Sorry but I don't have the resources to test and debug that script for you. I recommend contacting the script's author for further assistance.

    Bill

    Friday, June 17, 2011 4:46 PM
  • Building on Bills earlier suggestion, you will not only have to remove the write-message and write-errors, but you will have to also remove any of the parameters, such as -foregroundcolor or -nonewline.
    Paul Frankovich
    Friday, June 17, 2011 4:52 PM
  • Thanks for the help guys..

    Well i got to writing the output to a log file but whenver i runs it ,it overwrites the output of the previous one.

    How can i keep all the logs,is there any way.

    Friday, June 17, 2011 6:24 PM
  • Thanks I got it.
    Friday, June 17, 2011 6:41 PM
  • Hi  bill

       good script!  

              this modify can run and don't need input "Y".  as far as know, this script should run in exchange manager shell,such as,your ps1 name is uploadphoto.ps1,   you should run command “.\uploadphoto.ps1 -all”,

             I want to know,  I want to put this ps1 into a task,  and run it automatic at 2:00 ,  what should i do?   I have USE BAT to run some script in EMS,but without " - ALL",  how can i to slove "- ALL"

    Thanks !

    I am very sorry my english!

    China Boy


    Andes

    Friday, February 17, 2012 4:17 PM
  • Hi  bill

       good script!  

              this modify can run and don't need input "Y".  as far as know, this script should run in exchange manager shell,such as,your ps1 name is uploadphoto.ps1,   you should run command “.\uploadphoto.ps1 -all”,

     

             I want to know,  I want to put this ps1 into a task,  and run it automatic at 2:00 ,  what should i do?   I have USE BAT to run some script in EMS,but without " - ALL",  how can i to slove "- ALL"

     

     

    Thanks !

    I am very sorry my english!

    China Boy


    Andes

    Friday, February 17, 2012 4:18 PM
  • Hi,

    This thread is already marked answered. If you have a scripting question, please start a new thread; thanks.

    Bill

    Friday, February 17, 2012 4:31 PM