Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Dynamic Variable Names

$
0
0

Looking to create a script intended to run for extended period of time and track data for individual users.

$WatchList contains an array of SamAccountNames

ForEach($Person in $WatchList)
{
$A = Get-ADUser $Person -Properties * | Select DisplayName,etc......
$Date = (Get-Date).ToString("MMM-dd-yyyy")
$Time = (Get-Date).ToString("HH:mm:ss")
$UserInfoVariable = $Person + "Info"
New-Variable -Name $UserInfoVariable -Value @{}

#Get some stats for user
#Store stats in a PSObject
$Obj = New-Object PSObject
$Obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $A.DisplayName
$Obj | Add-Member -MemberType NoteProperty -Name "Stat1" -Value ""
$Obj | Add-Member -MemberType NoteProperty -Name "Stat2" -Value ""
$Obj | Add-Member -MemberType NoteProperty -Name "Stat3" -Value ""

#All good to this point

>>$UserInfoVariable.Add($Person,$Obj)
#The above line fails with the following error:
#Method invocation failed because [System.String] does not contain a method named 'Add'.
#At Script.ps1:xx char:x
#+             "$UserInfoVariable".Add($Person,$Obj)
#+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
#+ FullyQualifiedErrorId : MethodNotFound

}

Is there any way to have the $UserInfoVariable.Add command see as the created variable as the actual value "$SamAccountNameInfo" which was properly created in the New-Variable command earlier?

I could hard code the variable names but I think I would need duplicate the info gathering loop for each user. Plus if I change any of the names in the $WatchList I would then have to change the associated variables throughout the entire script.

Thank you,

Steve


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>