We help IT Professionals succeed at work.

Powershell Function

jskfan
jskfan asked
on
Medium Priority
51 Views
Last Modified: 2020-02-19
Powershell Function

I am running the code shown below. I enter a value and it should return the Cubicle value of it.
However I do not understand where the  outcome came from.

for instance, when I enter number 3 I should see 27,  instead of the outcome shown below

Thank you

f
Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Read-Host will treat the input as String, so you need to set it as Integer instead.

so try this instead

[int]$y=Read-Host("enter value: ")

Open in new window

David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
or
Function get-cube([int]$x)

Open in new window

Engineer
CERTIFIED EXPERT
Commented:
It is PowerShell trying to think for you, you're giving it a string instead of a number so it assumes you want to repeat the characters. As a rule of thumb, when working with numbers you should cast your variables (e.g. [int], [double], etc.). This prevents something unexpected occurring. Ryan and David have both shown how to do that.

If you're going to do maths in PowerShell, you might as well use the built in libraries provided by .NET via the [math] accelerator; unless you're trying to learn to build your own libraries or functions as an education piece.

Function Get-PowerOf {
	[CmdletBinding()]
	Param(
		[Parameter(Mandatory=$true,Position=0)]
		[int] $Number,
		[Parameter(Mandatory=$true,Position=1)]
		[int] $Power
	)
	
	[math]::Pow($Number,$Power)
}

Open in new window


.NET has libraries for all of this so you do not have to re-invent the wheel. Because .NET has it, PowerShell has it.

Using this is simple.

Get-PowerOf -Number 3 -Power 3
Get-PowerOf 2 5
Get-PowerOf 5 2
1..10 | % {Get-PowerOf -Number 2 -Power $_}

Open in new window


Or

When casting, you will want to handle an exception from somebody passing it the wrong variable type, e.g. a string. This will prevent someone doing the following an causing the script or functional to terminally fail.

try {
	Get-PowerOf "a" 3
} catch {
	$_.exception.message
}

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Should you wonder what PowerShell did read: It saw
   "3" * "3" * "3"
and since there is no "string * string" operator but a "string * integer", it converts the second string into a number. "string * integer" means "repeat the string n times". So the expression is
  ("3" * 3) * "3"   ==>   "333" * "3"  ==>  "333" * 3   ==>   "333333333"
Raheman M. AbdulMessaging and Directory Services
CERTIFIED EXPERT

Commented:
Write-Host ($result )   ->  You cannot access the value of $result this way since $result is local to that function. It is not available externally. so $result is $null here (ie out of the function) it is displaying $null value so you are not seeing any thing on display.

The output is from this -> Get-cube($y)

Author

Commented:
Thank you Guys!

will get to this topic in the future

Explore More ContentExplore courses, solutions, and other research materials related to this topic.