I am very new to PowerShell scripting and was experimenting with Function. This is the code I wrote:
Function Add
{
$sum = 0;
for($i = 0; $i -le $args.length; ++$i)
{
[int] $num = $args[$i]
$sum += $num
}
Write-Output "Sum is $sum"
}
And I tried to call using Add 1,2,3. However, when I execute I get the following error:
Cannot convert the “System.Object[]”
value of type “System.Object[]” to
type “System.Int32”.
Any idea how to fix this?
There’s a bg trap in Powershell:
,is the array operator. Try this at the command line:You’ll see that it’s an array:
Try to call
Addwithout commas:Don’t forget everything is an object in Powershell. You are playing on top of .NET.
So you have two friends:
gettype()method which gives you the type of an objectGet-MemberCmdLet which helps you on properties and methods of an objectGet-memberhas many parameters that can help.