But my question is why you want to do it. It’s not really necessary in Powershell as it will do most of these conversions occur dynamically, you don’t need to implicitly tell Powershell it needs to be an INT. Take this as an example:
You’ll see we define 123 as string, the GetType() will tell us it’s a string, but when we pass it as a parameter that is expecting an integer, it will do the conversion to INT there and return GetType() as an INT.
This is quite close to being one of the better ways to go already, you just need to clean up your code and double check your logic. As Rob mentions, you could also store things neatly in a hashtable or a similar construct, which will make it easier to find your values again. Programmatically generating names of variables is often a bit of a tiring exercise, because you will have to then find them again. Best to keep them all in one place, in one variable.
# Create empty hashtable as a container
$Table = @{}
foreach ($Item in $csv) {
$IntValue = $Item.Value -as [int]
# If the cast fails, $IntValue will be $null, and this statement will be skipped ($null casts to $false)
if ($IntValue) {
$Table.Add($Item.Properties, $Item.Value -as [int]
}
}
Thank you for your responses. I’m looking them over, as to learn.
I believe the root action of import-csv is everything comes in as a string; integer-shaped-objects are thusly not typed as [int]. To meet PoweCLI new-vm parameter requirements, e.g. [-CoresPerSocket Int32], [-NumCpu Int32], I gotta do some type manipulation.
Dynamically creating the variables from the CSV will map to the hash table that splats the parameters; perhaps some CSV configs have E and F drive, some CPU and memory requirements will differ, etc. Programming logic will IF for additional items, and call functions accordingly.
function import-VMParameters {
Import-CSV -path
New-Variable #validate for int, where int is required, create variables
I believe the root action of import-csv is everything comes in as a string; integer-shaped-objects are thusly not typed as [int]. To meet PoweCLI new-vm parameter requirements, e.g. [-CoresPerSocket Int32], [-NumCpu Int32], I gotta do some type manipulation.
You are correct, everything imported from a CSV is a string. Look at the second example I provided, you don’t need to do type conversions. If the input can be converted to the type (e.g. [int]), Powershell will do it for you without explicitly defining the type before it’s passed. It doesn’t hurt anything, but you are doing unnecessary work IMHO as the New-VM function will do the same conversion you are doing manually. In both examples below, NumCpu is passed a string, but the conversion is handled by defining it as [int] in the function params:
You mention “parameter requirements” but if we’re being honest those are somewhat loose in PowerShell functions. Even if your value is a string, as long as it can be parsed as a number PowerShell will automatically convert it to the proper value when you pass into a numeric-typed function argument.
Thank you for clarifying that POSH will do the conversion to the type. That does make it easier. I’ll tuck away the manual “evaluate for int” for future use.