Arrays
3 TopicsArray and array member methods
The following PowerShell code: class MyClass { } class MyClass1 : MyClass { [void] OutMsg([string] $Str) { Write-Host -Object "MyClass1: $Str" } } class MyClass2 : MyClass { [void] OutMsg([string] $Str) { Write-Host -Object "MyClass2: $Str" } } [MyClass[]] $ClassArray = @([MyClass1]::new(), [MyClass2]::new()) $ClassArray.OutMsg('TestString') outputs: MyClass1: TestString MyClass2: TestString A Get-Member -InputObject $ClassArray shows me that the array object itself has no OutMsg method. Why can I successfully call $ClassArray.OutMsg('TestString') though (looks like this calls the OutMsg method of each array member in turn)?93Views0likes3CommentsTee-Object / -OutVariable into existing array (i.e., add into that array - is this doable?)
Hello, I have some Exchange reporting scripts where I'd like to offer the option to export CSV automatically but always send the output to standard output stream as well. I though about using Tee-Object like this: foreach ($y in $yadayada) { [PSCustomObject]@{ p1 = $y.v1 p2 = $y.v2 } | Tee-Object -Variable preExistingArray } I would later then like to do this: if ($ExportCSVs) { $preExistingArray | Export-Csv ... } ...and by that time, nobody was waiting at the console for all their objects to collect before being output all at once. That is the difference from my usual approach which is more like: 1-Collect everything into variable;2-output variable (all objects) to std output stream; 3-export to CSV. Thought I could use Tee-Object to combine 1 and 2. I don't think it's possible. So, wondering if anyone else has something clever in this area? Please do share.1.6KViews0likes4CommentsPowershell - replace an array / hash table with a file
Hi All, This should be straigtforward (I think), but i cannot get it to work. I have a working script which reads a csv file from an HR app and then creates the user accounts. Dependant on the users site name it will read the array and assign them to the correct OU. As the names of the sites has proven to be inconsistant or at least 3 variants in some cases, I created an array (could it be classed as a dictionary file?). to handle the differences, e.g. $Site2OU = @{ "Miami" = "OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com" "Miami Beach" = "OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com" "Miami Beach Holiday Homes" = "OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com" "Boston" = "OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com" "Boston Bay" = "OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com" "Bston Bay Holiday Homes" = "OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com" "Mississippi" = "OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com" "Mississippi Water" = "OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com" "Mississippi Water Holiday Homes" = "OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com" } then an extract from the script would handle it like this (Imported csv Foreach($NewUser in $Newusers) $Location = $NewUser.Site $OU = $Site2OU.$Location This would then create the $OU variable for a site based on any of the 3 variations. Everytime a new site is added it means editing the script etc. And this script is compiled into a GUI exe file. To save the hassle I would like to replace the array with the contents of a file, which would allow the users to add new sites, as and when they are required. Is there an easy way to do this, I have tried numerous ways and none work. Any pointers would be appreciated.Solved818Views0likes2Comments