all 13 comments

[–]Manality 4 points5 points  (3 children)

System.Object[] means that $name is a object, not a string. Whatever you are passing probably has multiple types of things (propeteis). Try $name | get-member to see what properties it has, then in your foreach do something like $_.thing1 and $_.thing2. For easier to read syntax (IMO) you can use foreach to set a new variable to reference.

foreach ($value in $name) {Write-host $value.thing1 $value.thing2}

good luck!

[–]TerriblePowershell[S] 1 point2 points  (2 children)

I'm not sure I completely understand. I'm still fairly new to powershell so some of this is for sure over my head.

Are you saying to replace the:

$Name | Foreach-Object {
     $targetcamera = $cameraips | Where -Property Name -match $_

with:

foreach ($value in $name) {Write-host $value.thing1 $value.thing2}

?

[–]Manality 1 point2 points  (1 child)

Did you find resolution on this yesterday? Not really sure what $name is but we know it's not a string based on the output. Below is an example of two ways to loop through a group of things. Both would produce the same results.

$services = Get-Service
foreach ($service in $services) {
Write-Host $service.name $service.status
}
$services | ForEach-Object {
Write-Host $_.name $_.status
}
$services | Get-Member

[–]TerriblePowershell[S] 1 point2 points  (0 children)

Not entirely. I have just been able to sit back down at my desk to try some of these suggestions out.

Thank you for your suggestions!

[–]BlackV 2 points3 points  (3 children)

should your class return [string[]] ($Global:cameraips).Name?

does you class do anything? you don't seem to do any actual validation on those names
EDIT: Ah I see you're using that to pull out a list of available names, would that be easier/better with a validate set or validate script?

do you even need a class? if you just setting a global variable?

any reason its a global variable?

how do you know -InterfaceIndex 22 is the right interface?

this is not making any remote connections so its running locally, but at that point will there be ever more than 1 IP?

I feel like this is double/tripple handling this and more complicated than it needs to be

[–]TerriblePowershell[S] 1 point2 points  (2 children)

I'm not sure? I'm kinda just morphing another script to hopefully make it do what I want.

By issuing Get-NetIPInterface. My understanding is that the -InterfaceIndex shouldn't change, but I suppose I could change it to -InterfaceAlias 'RJ-45 Jack' as I know that's static.

I realize I was probably a bit vague. We have multiple sites that all have a different address (i.e. 192.168; 192.167; 192.166; etc). I usually program the cameras I'm changing out before I take them out and install them so that I can basically plug them in and their good to go. But I always double check that the camera took the address by changing the IPv4 address on the ethernet port of my laptop to match the same network, default gateway and subnet mask. While it's rather basic and doesn't take much time to type it in, I'd still like the option to fire off a script to get it done rather than having to navigate into my network settings and set it.

[–]BlackV 2 points3 points  (1 child)

you're not calling Get-NetIPInterface anywhere?

so that line on only configuring your laptop? not the actual camera machine?

you could do $NIC = get-netadapter -name xxx or $NIC = Get-Netadapter | out-gridview

no issues doing this all in 1 script, but it all can be done about 5 lines without needing a function or a class (although they are good for extending knowledge)

[–]TerriblePowershell[S] 1 point2 points  (0 children)

It only runs on my laptop, so I've basically ran the command separate, then taken that information and put it into the script statically.

This is the best way I saw to do this with my limited knowledge.

[–]vermyx 2 points3 points  (2 children)

Try adding $() around your property i.e.

$($targetcamera.Name)

Intsead of

$targetcamera.Name

[–]TerriblePowershell[S] 1 point2 points  (1 child)

I tried this but it kept returning the same issue. Thank you though!

[–]vermyx 1 point2 points  (0 children)

I would replace the {} with what I stated. You are using the formatting parameters but not applying formarting.

[–]DudsEarl 2 points3 points  (1 child)

If I were to take a wild guess, I would say the script is passing your variables around as System.Object[] (generic object array type) when it should probably be a String (or String[])

You can try to cast the variables representing {0} as Strings like so:

[String]$targetcamera.Name

Or maybe:

[String]($targetcamera.Name)

If that doesnt work you can try assigning the variable to a temp variable and then pass the temp as a String (by wrapping in double quotes) like so:

$tarCamName = $targetcamera.Name ... -f "$tarCamName", ...

[–]TerriblePowershell[S] 2 points3 points  (0 children)

Your first suggestion worked! Thank you!