· 5 years ago · Mar 28, 2020, 01:18 AM
1
2Powershell script
3
4Enter Decimal Number
5
6/>
7
8 Write function called Get-DecimalNumber which will do the following:
9
10 Test if the file conversions.csv exists, and if not display a warning message, create it and suppress all output to the console
11 Validate user input as an integer (1-255) has been entered. If incorrect input is given display a message and exit.
12 Convert the decimal input to hexadecimal and binary using string methods and display the result
13 Send the output to a hash table and append to conversions.csv
14 Save all the inputs and outputs to a CSV file which can be displayed at the console in table format. The CSV file will maintain a running total of all conversions, the date of the conversion in short date format, decimal hexadecimal and binary output
15
16Expert Answer
17
18
19
20Please Fnd the Solution Below:
21
22function Get-DecimalNumber
23
24{
25
26if !(Test-Path -Path "D:\temp\test\conversions.csv")
27{
28Write-Warning "conversions.csv File Not Present"
29New-Item -Path D:\temp\test\conversions.csv
30[int]$Num = Read-Host "Enter number from 1-255"
31if ($Num -ge 255 -and $Num -le 1)
32{
33Throw 'You did not Entered number in Specified Range'
34exit
35}
36New-Variable -Name "hex" -Value PS> [Convert]::ToString(Num, 16)
37New-Variable -Name "bin" -Value PS> [Convert]::ToString(Num, 2)
38
39Write-Host "
40Decimal to hex and Binary Data:
41
42"
43
44$newhastable1 = @{}
45$newhastable1.Add("Hex Value",hex)
46$newhastable1.Add("Binary Value",bin)
47$newhastable1
48
49Add-Content D:\temp\test\conversions.csv $hex
50Add-Content D:\temp\test\conversions.csv $bin
51$timespan = [timespan]::Parse($row.TotalDuration)
52Get-Date -Format d
53Get-Process | conversions.csv
54}
55
56}
57
58
59Comment