0

Suppose the below output was there. I want to assign to different variables in powershell. Can anyone please explain

Example::

input ::

Get-MailboxDatabase -Server NYRTFG01 | Select servers | select -First 1 | sort servers Servers ------- {NYRTFG01, NYRTFG02, NYNTFG01, NYNTFG01}

$a = Get-MailboxDatabase -Server NYRTFG01 | Select servers | select -First 1 | sort servers $a.servers | select name
Name ---- NYRTFG01 NYRTFG02 NYNTFG01 NYNTFG01

I want to assign to four variables of the four servers like this

$server1 = "NYRTFG01"
$server2 = "NYRTFG02"
$server3 = "NYNTFG01"
$server4 = "NYNTFG01"
5
  • 1
    What is the output when you do $Input.Servers? Commented Dec 4, 2019 at 2:32
  • It's worked. thank you Commented Dec 4, 2019 at 2:39
  • It shouldn't have. Those servers wont be variables, just strings. Commented Dec 4, 2019 at 2:41
  • Yeah, I'll explain you my needs Commented Dec 4, 2019 at 2:45
  • "Get-MailboxDatabase -Server NYRTFG01 | Select servers | select -First 1 | sort servers " --> It'll give this output Servers ------- {NYRTFG01, NYRTFG02, NYNTFG01, NYNTFG01} I want to assign to four variables Commented Dec 4, 2019 at 2:46

1 Answer 1

1

Based on your responses to my comments, I believe the below is what you are after. A little convoluted but it works.

$MailServers = Get-MailboxDatabase -Server NYRTFG01 | Select servers -First 1 | Sort servers
$i = 1
($MailServers.Servers).ForEach({
    New-Variable -Name "Server$i" -Value $PSItem
    $i++
})

This cycles through each item in $MailServers and creates a new variable with the name "Server" followed by an increasing number based on the .Count of $MailServers. The adds the item as the value of the new variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.