powershell - What is wrong with my array? -


i cannot figure out need fix script run.

the code:

$clusternodenames = (get-clusternode -cluster clu05).name $allvmsincluster = get-vm -computername $clusternodenames  $allvms = $allvmsincluster | select -expandproperty name  $allvmsincluster[1] | set-vm -snapshotfilelocation c:\clusterstorage\volume1\$allvms[1] 

what happening creating 2 arrays

$allvmsincluster  $allvms  

output of:

ps c:\windows\system32> $allvmsincluster[1]  name     state cpuusage(%) memoryassigned(m) uptime   status             version ----     ----- ----------- ----------------- ------   ------             ------- acd-pv06 saved 0           0                 00:00:00 operating 8.0  ps c:\windows\system32> $allvms[1] acd-pv06 

my end game put last line in loop @ end keep getting whole array.

when run last line change snapshotfilelocation acd-pv06 output looks like:

c:\clusterstorage\volume1\acd-pv02 acd-pv06 acd-sql01-ag acd-sql02-ag[1] 

how can like:

c:\clusterstorage\volume1\acd-pv06\ 

you don't need 2 arrays. have .name property available within objects in $allvmsincluster array. use directly in loop or pipeline. wrap object.property in sub-expression $() ensure correct variable expansion.

$allvmsincluster |     foreach-object {         write-host "changing snapshot file location $($_.name)."         set-vm -vm $_ -snapshotfilelocation "c:\clusterstorage\volume1\$($_.name)" -whatif     } 

remove -whatif when you're ready perform changes real.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -