Hi! I have a question and was wondering if someone care to explain some more on hashtables and the way i do this in the example, maybe some other ways to acomplish the task. Here goes:
This is the code: $HashTbl = =@{Machine=“PC1”;IP=“10.0.0.1”;User=“iktspesialisten}”}
The output result of this gives the first column Name and the next Value.
I was wondering how i can change this so it say something different, like Name and Information for example.
Hope i got to explain what i am trying to do in a way that you guys understand. I guess i dont have enough background information to quite grasp how this works
Note: Using Format-* cmdlets will convert everything to text and you will loose the power of objects. Format-* cmdlets are used just for printing the output by applying user defined formatting.
If you’re running v2.0, you’re gonna have a lot more troubles than that.
Additionally, always recommending the New-Object PSObject method is still a bad idea.
New-Object is very slow (on average about 8 times slower than ::new() in PS 5, though I think it’s gotten faster across the versions; it’s also much slower than the hashtable construction method of custom objects).
Depending on the exact method you’re employing, PSObject as a type can be very difficult to work with. As I recall, the hashtable construction method is available from v3.0 onwards and should be used unless you specifically have to support PS v2.0 or lower for some reason.
I was able to do New-Object -TypeName PSObject -Property @{Machine=“PC1”;IP=“10.0.0.1”;User=“iktspesialisten}”}
I was just wondering what is the name of the object now? And also, when i pipe this to get-member it say PSCustomObject and not PSObject. Is there any way to convert it to be a PSObject so i can use the properties and maybe hook up the Machine name to ByValue so i can pipe a list of computers in there?
Some clarification – everything in PowerShell is inherently a PSObject. It’s a wrapper type, doesn’t really mean all that much on its own. All objects you’re going to create directly, unless you build your own class to create them from, will be PSCustomObjects.
The latter part of your question doesn’t make a whole lot of sense. If you’re talking about taking pipeline input from a script or function, your framework would look like this:
param(
[Parameter(ValueFromPipeline, Mandatory)]
[Alias('Machine')]
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName
)
# process block required for pipeline input; executes once for every item passed in
process {
# loop allows direct array input as well, if desired
foreach ($PC in $ComputerName) {
[PSCustomObject]@{
Machine = $PC
IP = # however you want to retrieve the IP
User = # whatever you need here
}
}
}