none
Enter value of a Input box only the first time a script is run RRS feed

  • Question

  • Hello. I have a script that sends an email of some databases from different vessels. I have created a VB dialogbox which you enter the vesselname and this is used in the subject field of the mail. But I only want this to be valid the first time the script is run. So from the 2nd time its run the values from the first time you entered the value into the dialogbox will be true.

    My code is:

    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $vesselname = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Vessel Name", "Vessel Name", "$env:username") 
    
    $log = "C:\AM\logs\AutoMail_log.RTF" -f (Get-Date)
    Start-Transcript -Path $log -Append
    
    #Connection Details
    $username=”rutek@gmail.com”
    $password=”xxxx”
    $smtpServer = “smtp.gmail.com”
    $msg = new-object Net.Mail.MailMessage
    
    #Port number for SSL
    $smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
    
    #SSL  
    $smtp.EnableSsl = $true
    
    $smtp.Credentials = New-Object System.Net.NetworkCredential( $username, $password )
    
    #From Address
    $msg.From = "from@22.com"
    $msg.To.Add(“to@22.com”)
    
    #Message Body
    $msg.Body=”Please See Attached Database Files from $vesselname”
    
    #Message Subject
    $msg.Subject = “Database from $vesselname”
    write-host ""
    write-host "SENDING FILES"
    write-host ""
    
    #your file location
    $files=Get-ChildItem “C:\AM\Send\”
    Foreach($file in $files)
    {
    Write-Host "Attaching File :- " $file
    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList $file.FullName
    $msg.Attachments.Add($attachment)
    $smtp.Send($msg)
    $msg.Attachments.Clear()
    }
    $smtp.Send($msg)
    $attachment.Dispose();
    $msg.Dispose();
    write-host ""
    write-host "Mail Sent Successfully"
    write-host ""
    Stop-Transcript

    Hope someone can help :-)

    Thank you.

    Tuesday, January 14, 2014 10:00 AM

Answers

  • Hello Christopher,

    how would you propose solving this problem? Scripting is all about process, about a sequence of actions you take. So which sequence needs to be executed to reflect the process you want run (not in PowerShell Code, but in actions taken)?

    When you have that process, try mirroring it in script code. If you have trouble with a step, ask specifically for that part, showing your own thought on the matter.

    By the way, if you search for individual actions using a search engine (or this forum), you'll be a lot more likely to find what you are looking for.

    Since I'm feeling kind, here's one piece of info that may help you (as one part out of many parts) with solving your problem yourself: Test-Path

    Cheers,
    Fred 


    There's no place like 127.0.0.1

    Tuesday, January 14, 2014 1:19 PM

All replies

  • Save the values in a file.


    ¯\_(ツ)_/¯

    Tuesday, January 14, 2014 10:25 AM
  • Hello and thank you for answering. This works fine, but how do I make it only read to the file once and not each time the script is run?
    Tuesday, January 14, 2014 11:59 AM
  • Hello and thank you for answering. This works fine, but how do I make it only read to the file once and not each time the script is run?

    No - we always read the file.  The file tells us which vessels have been processed. Think work flow with persistent data.


    ¯\_(ツ)_/¯

    Tuesday, January 14, 2014 12:25 PM
  • Hehe, yes. My bad. But I dont want the script to show the dialogbox each time. Thats what I means. I understand it needs to read the file each time. :-)
    Tuesday, January 14, 2014 12:33 PM
  • Hehe, yes. My bad. But I dont want the script to show the dialogbox each time. Thats what I means. I understand it needs to read the file each time. :-)

    So you don't know anything about writing a script and you are asking for someone to do it for you?  Why not try to learn how to write a script.  It is not hard.  Even children can learn to script. 

    There are numerous resources on this site that can help you learn to script.


    ¯\_(ツ)_/¯

    Tuesday, January 14, 2014 12:38 PM
  • I am a novice in writing scripts, yes. But trying to learn something from these forums. I have searched for quite a while and no results. Thats why I asked this question.

    Thank you for beeing so helpful.....

    Tuesday, January 14, 2014 12:50 PM
  • Hello Christopher,

    how would you propose solving this problem? Scripting is all about process, about a sequence of actions you take. So which sequence needs to be executed to reflect the process you want run (not in PowerShell Code, but in actions taken)?

    When you have that process, try mirroring it in script code. If you have trouble with a step, ask specifically for that part, showing your own thought on the matter.

    By the way, if you search for individual actions using a search engine (or this forum), you'll be a lot more likely to find what you are looking for.

    Since I'm feeling kind, here's one piece of info that may help you (as one part out of many parts) with solving your problem yourself: Test-Path

    Cheers,
    Fred 


    There's no place like 127.0.0.1

    Tuesday, January 14, 2014 1:19 PM
  • Here is a second bit of help.

    Look into using "if" and into how to read a file.

    You may need to learn the basics of computing first.  I don't mean how to use the GUI or turn on a computer but the basics of how computers are able to perform complex tasks based on a stored program.  That is the fundamental that is the same for all computers and computer languages.

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Tuesday, January 14, 2014 4:57 PM