· 5 years ago · Mar 28, 2020, 01:20 AM
1
2
3Powershell Script
4
5Write function called Get-HexadecimalNumber which will to the following:
6
7 Test if the file conversions.csv exists, and if not display a warning message, create it and suppress all output to the console
8 Validate user input that as a hexadecimal number beginning with "0x" (0x2A). If incorrect input is given display a message and exit.
9 Remove the "0x" from input and convert to decimal and binary using string methods/operators. Display the result
10 Send the output to a hash table and append to conversions.csv
11 Save all input and output to 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.
12
13
14EXPERT ANSWER:
15
16
17
18function Get-DecimalNumber
19
20{
21
22if !(Test-Path -Path "D:\temp\test\conversions.csv")
23{
24Write-Warning "conversions.csv File Not Present"
25New-Item -Path D:\temp\test\conversions.csv
26[int]$Num = Read-Host "Enter number from 1-255"
27if ($Num -ge 255 -and $Num -le 1)
28{
29Throw 'You did not Entered number in Specified Range'
30exit
31}
32New-Variable -Name "hex" -Value PS> [Convert]::ToString(Num, 16)
33New-Variable -Name "bin" -Value PS> [Convert]::ToString(Num, 2)
34
35Write-Host "
36Decimal to hex and Binary Data:
37
38"
39
40$newhastable1 = @{}
41$newhastable1.Add("Hex Value",hex)
42$newhastable1.Add("Binary Value",bin)
43$newhastable1
44
45Add-Content D:\temp\test\conversions.csv $hex
46Add-Content D:\temp\test\conversions.csv $bin
47$timespan = [timespan]::Parse($row.TotalDuration)
48Get-Date -Format d
49Get-Process | conversions.csv
50}
51
52}
53Hide comments (1)
54Comments
55
56 Anonymous's Avatar Anonymous posted 4 hours ago
57
58 See the question again. The input is hexadecimal number not decimal number. The code should convert the hexadecimal number not decimal number.