Hi All,
Here's my objective: We have the following tasks that we have to do it manually every week. We have to enter the projects we worked on, hours we worked, the date we worked on. I want to write a script so we can run it and we would be able to select the projectID from the list, enter how many hours we worked on it, the date and write it to an excel spreadsheet. The projected is static, we get a csv file and it has all the projectid's available. I have created a dynamic parameter that the engineer can tab through and find the projected he/she has been working on. Repeat for every projects that we worked for that week.
My script collects the projectid, hours, date and writes it to the excel spreadsheet, but I can't do this for multiple project for that week. How would make it so that, once you enter information for 1 projectid and you have the option to enter the 2nd, 3rd, etc. and at the end it will write all of it to the excel spreadsheet.
Here's my code:
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$hours,
[parameter(mandatory=$true)]
[datetime]$date
)
DynamicParam {
#create a new ParameterAttribute Object
$project_attributes = new-object System.Management.Automation.ParameterAttribute
$project_attributes.ParameterSetName = "__AllParameterSets"
$project_attributes.Mandatory = $true
#create an attibutecollection object for the attribute we just created
$project_attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
#add our custom attribute
$project_attributeCollection.Add($project_attributes)
#$_Values = (Get-wmiobject -computername $servername win32_printer).drivername
$csv = (import-csv -path "c:\user\peoplesoft automation\projectlists.csv")
$_Values_project = $csv.project
$project_ValidateSet = new-object System.Management.Automation.ValidateSetAttribute($_Values_project)
$project_attributeCollection.Add($project_ValidateSet)
#add our parameter specifying the attribute collection
$project_dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("project", [string], $project_attributeCollection)
#expose the name of our new parameter
#$project_paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
#$project_paramDictionary.Add("project", $project_dynParam1)
#$project_paramDictionary
#expose the name of our new parameter
$paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
#$paramDictionary.Add("description", $description_dynParam1)
$paramDictionary.Add("project", $project_dynParam1)
$paramDictionary }
begin
{
}
process {
$projects = $PSBoundparameters.project
write-host ""
write-host ""
write-host "Project ID " $projects
write-host "Date you worked" $date
write-host "Hours Worked: " $hours
#write-host "CSV file to save to:" $outfile
$title = "Review carefully the information above!"
$message = "Do you want to save the information above to a CSV file"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Create a timesheet CSV for peoplesoft"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancel."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result)
{
0 {"Operation Started..."}
1 {"Operation Canceled.."; exit;}
}
# Create new Excel workbook
foreach ($p in $projects) {
$Excel = new-Object -comobject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Cells.Item(1,1) = "Project ID"
$sheet.Cells.Item(1,2) = "Hours Worked"
$sheet.Cells.Item(1,3) = "Date Worked"
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True
$Sheet.Cells.Item($intRow, 1) = $projects
$sheet.cells.item($introw, 2) = $hours
$sheet.cells.item($introw, 3) = $date
$WorkBook.EntireColumn.AutoFit()
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
}
}
my csv file contains header projectID and some random projectid's