Search This Blog

Wednesday 1 July 2015

powershell help

today i m going to show a powershell script created for a book store manger to keep record record of a data and books by price

first the owner wants the font  colour to be yellow

$HOST.UI.RawUI.BackgroundColor = 0
$HOST.UI.RawUI.ForegroundColor = 14




in next step we write the menu by a variable we can use by write-host method but that is old and take so much length 

$menu= "___________________________________
___________________________________
 BOOK SALES
___________________________________
___________________________________
1. Book ID
2. Book Name
3. Book Description
4. Book price
5. Write to a file
6. Exit program
7. open file
___________________________________
___________________________________"  

after that we store a file path in a variable in which we gonna store our data

$menu
#Menu file
$menufile="C:\Users\parmindersingh\Desktop\bookconfrence.txt"
 


then we will write a script for a menu 
we will use do and untill loop for whole menu 


do{
$id = read-host "1.Enter the book id "
if ($id -notmatch '^[A-Z]{3}[0-9]{3}' ){WRITE-WARNING " DATA ENTRY MUST BE ALPHANUMERIC"}
[System.Media.SystemSounds]::hand.Play() 
if ($id -notmatch '^[A-Z]{3}[0-9]{3}' ){Read-Host "ENTER THE BOOK ID"}

#book title
[string]$book = Read-Host "2.Enter the book title."
if($book -notmatch "[string]"){write-warning "invalid name "}
[System.Media.SystemSounds]::hand.Play()
if($book -notmatch "[string]"){read-host "please enter valid name"}

# description of book
[string]$bookdescription = Read-Host "3.book description"
if($bookdescription -notmatch "[string]"){write-warning "invalid book description"}
[System.Media.SystemSounds]::hand.Play()
IF($BOOKDESCRIPTION -notmatch "[string]"){READ-HOST "ENTER THE BOOK DESCRIPTION"}

#book price
[decimal]$bp = Read-Host "4.Enter the book price"


#Tax
[decimal]$tax = $BP*0.13

#total  amount
[decimal]$ta = $bp+$tax
write-host  "total payable amount is $ta"
[System.Media.SystemSounds]::beep.Play()

in last step we will ask the user to save the data or want to exit 


# DO YOU WANT TO WRITE TO FILE
$RF = READ-HOST "DO YOU WANT TOSAVE THE DATA TO THE FILE Y/N"
IF ($R -EQ "Y"){Write-Output " $date "   >> $path

Write-Output "_______________________________________" >> $file
write-output  "_______________________________________" >> $file
write-output "book id : $id" >> $file
Write-Output "Title of Book: $book " >> $file 
Write-Output "Description: $bookdescription " >> $file
Write-Output "Book list Price:  $bp " >> $file
Write-Output "Tax applied: $tax " >> $file
write-output "==============================">> $file
Write-Output "total amount: $ta " >> $file
Write-Output "_______________________________________" >> $file
}
ELSE {EXIT}
$userresponce = read-host "do you wish to exit [y/n] "
}
until($userresponce -eq "y")


No comments: