New-NetLbFoTeam: Unknown or Random InterfaceAlias names - powershell

I'm trying to automate the create of a NIC team during an unattended Windows Server 2012 R2 install.
I've got the following PowerShell code:
New-NetLbFoTeam -Name "LANTeam" -TeamMembers "Ethernet", "Ethernet 2" -TeamNicName "LAN" -TeamingMode SwitchIndependent -LoadBalancingAlgorithm TransportPorts -Confirm:$false -ErrorAction SilentlyContinue
That works well for my Dell servers, but the HP servers Windows randomly gives InterfaceAliases to. One install Ethernet 2 could be the Broadcom, the next it could be the NC373i card.
What I'm trying to accomplish is set the -TeamMembers parameter to be the two NICs that match "HP NC373i*" wildcard for the InterfaceDescription, or have a valid DHCP address. The other team I'll do something similar, but don't retrieve a valid IP address.
I've tried setting a hash table, but not getting it to stick in there correctly.
Any assistance would be greatly appreciated!

I was able to figure it out on my own. I output the get-netadapter output to a variable, and added that:
$adapters = Get-netAdapter –InterfaceDescription “HP NC*”
$nicList = #()
Foreach ($nic in $adapters) {$nicList += $nic.Name}
$team = New-NetLbfoTeam -Name “LANTeam” -TeamNicName “LAN” -TeamMembers ($nicList) -TeamingMode SwitchIndependent -LoadBalancingAlgorithm HyperVPort -Confirm:$false

Related

How do I permanently set a static IP Address in Windows 10 using PowerShell for my automated OS build process?

When I started learning how to do this by watching videos, I learned about IP address InterfaceIndexes. I learned that I can't set an IP Address without knowing the IP Address's InterfaceIndex first. That seemed to be different on every computer I look at and seemed to be random. Then, I learned that you can use an IP Address Alias. On a fresh Windows install, it seemed that the IP Address Alias Ethernet0 was going to be a constant that I could rely on. So, with my primitive PowerShell skills, I thought I could reach out and grab the PC's IP Address and check to see if it matched what it was supposed to be. If it wasn't, wipe out whatever Ethernet0 is, and create a new IP Address with the alias of Ethernet0. Of course the first time I did this it worked perfectly and I moved on. I had a static IP address called Ethernet0 with the specified IP address. I just went in and looked and I have 2 available NICs. One is called "Ethernet" and the other is "Ethernet2". Nether of them have a static IP Address. I'm assuming the PC wants to set random alias names upon a restart? I'm a little confused on what happened. Of course when I run my method again, I get an error that says "No InterfaceAlias equal to ethernet0", so my code doesn't work. I'm obviously way off on how I should be approaching this. I want to do a fresh install and run a configuration script to set everything up without any GUI interaction. I don't work in an enterprise domain type of environment, and have to create my own process. Please help further my PowerShell wisdom, as only a Padawan I am.
function SetIp {
$ipv4 = (Test-Connection -ComputerName $env:COMPUTERNAME -Count 1) .IPV4Address.IpAddressToString
if($ipv4 -eq "my.ip.add.res") {
Write-Host "Do Nothing as the IP has already been set"
}
else {
Remove-NetIPAddress -InterfaceAlias ethernet0
New-NetIPAddress -IPAddress my.ip.add.res -InterfaceAlias ethernet0 -PrefixLength 16
Write-Host "IP Address has been set"
}
}
You can see all the available info about the NetIPInterface objects with Get-NetIPInterface | Select * -First 1. Some useful properties are ConnectionState,AddressFamily, or Dhcp. For example, try something like this to find valid ones:
Get-NetIPInterface | Where {
$_.ConnectionState -eq 'Connected' -and
$_.AddressFamily -eq 'IPV4' -and
$_.InterfaceAlias -like 'Eth*' -and
# check if in your local subnet
($_.DHCP -eq 'Enabled' -or ($_|Get-NetIPAddress).IPv4Address -like '1.2.3.*')
}
You can run into all sorts of things though, so it depends on how controlled your environment is:
Wifi and/or Ethernet are connected
Second Ethernet, or laptop dock creates new net interfaces
VPN software creates a new 'Ethernet' interface
If you need to ask a user for input, you could do it with gridview:
$Selected = Get-NetIPInterface |
? {$_.AddressFamily -eq 'IPV4' -and $_.InterfaceAlias -notlike 'Loopback*'} |
Select IFIndex,InterfaceAlias,ConnectionState |
# Ask the user to select the interface
Out-GridView -Title 'Please select the correct interface' -OutputMode Single
Set-NetIPInterface -InterfaceIndex $Selected.ifIndex -AddressFamily IPv4 ## etc...
That said, just setting up DHCP to handle this will almost always be easier. DHCP reservations are generally just as good as static IPs for user machines

How to run VMware commands from remote scripts on windows

Have a local basic Powershell form for searching and creating VMware virtual machines.
Using new powershell powerCLI module, as described in link
Let's take Get-VM for example:
LOGIC: Type a certain string in TextBox > click search > prints VM's status/parameters in the form
The problem is, I can't execute Get-VM straight away from the script, but first have to connect using Connect-VIServer command and only than Get-VM will work
Is there any way to do it from the script? Something similar to -m flag of commands plink or putty.
Like: Connect-VIServer -server testvc -flagForExample "commands_list.txt"
Yes, you can. Before providing an immediate answer I'd like to explain what is actually happening.
When you call Connect-VIServer the command sets the value of the variable $DefaultVIServer behind the scenes, which is later used by other cmdlets (such as Get-VM).
However, the Get-VM documentation states that there is a Server parameter available. Which means that you can store your server connection in a variable and then pass it to the Get-VM cmdlet.
Here's a pseudo-code example:
$server = Connect-VIServer -server testvc
Get-VM -Server $server
Furthermore, the Get-VM supports an array of servers, so theoretically you can run the cmdlet on multiple servers at once. For example:
$server1 = Connect-VIServer -server testvc
$server2 = Connect-VIServer -server testvc2
Get-VM -Server #($server1, $server2)

using powershell to check active IP's or MAC's connected to router

I want to run a Powershell script that talks to the router/AP and figures out what IP (I have Reserved IPS) or MAC address is currently connected to the Router/AP. The script would output what is connected so that I could see "who's home".
At first I used IE though powershell logging into the router and trying to capture data of the wifi client page but I don't think that is the way to go. Is there another way to do this? A way to scan the network without worrying about logging into the router?
If you have DNS resolution on the names of your PCs, you can try this out.
$Computer = "value or foreach loop of values"
$IPAddress = ([System.Net.Dns]::GetHostByName($Computer).AddressList[0]).IpAddressToString
$IPMAC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer
$MACAddress = ($IPMAC | where { $_.IpAddress -eq $IPAddress}).MACAddress
I have tested this with a couple individual names and a foreach loop getting the names from a txt or csv file, and I tested using
write-host $IPAddress $MACAddress
at the end for a sanity check.
If you want to verify an up/down state of the computer before querying, try using the 'test-connection' powershell command (basically a ping that grabs the results)

How to check if a server is running windows 2003 or Windows 2008 by checking its RDP screen, through script?

We have recently acquired a small firm having 1500 servers on which our team doesn't has access as of now although they are in domain. We need to find out how many servers are running Windows 2k3 and how many are Windows 2k8.
I know the RDP screen of both of these versions are different , for example: if we RDP a Win2k3 machine, it gives a warning notice first and once we click Ok, it takes us to the credentials screen , but in case of Win2k8, it directly takes us to Crendentials which is a proof of the OS on the server. Doing this manually for 1500 servers is a time consuming task.
Can we implement this RDP screen logic using a script to find out the Windows OS version.
I can imagine an Algorithm something like that:
Enter server name.
Invoke mstsc for that server
Verify if the dialogue box is a direct prompt for credentials or not?
If so, print Windows 2k8, else 2k3/2k.
If this logic successful on one server, I can use it in a foreach loop for all servers and export in in Excel.
With 1500 servers I'm going to assume that you have an Active Directory in place. In that case you should be able to simply run a query against AD to retrieve the desired information:
Import-Module ActiveDirectory
$server = 'somehostname'
$dc = '...' # domain controller of trusted domain
$fltr = "OperatingSystem -like '*server*'"
Get-ADComputer -Filter $fltr -Property OperatingSystem -Server $dc |
Where-Object { $_.Enabled } |
Select-Object Name, OperatingSystem |
Sort-Object OperatingSystem, Name
Pipe the result into Export-Csv to create a CSV file that you can import into Excel.

How to Wait till a DHCP server assign's IP to a first time boot VM with a Sysprepped vhd attached to it using PowerShell in Hyper-V version 3.0

I'm going through a scenario where i boot a Newly built VM with a sysprepped vhd attached, once the setup process completes with installing devices etc and the OS gets loaded i wanted to wait till this entire process finishes and the VM is assigned an IP address via DHCP.
PS C:\Users\Administrator> Start-VM -Name dv.VMWIN2K8R2-3.Hng
PS C:\Users\Administrator> while ((Get-VM -Name dv.VMWIN2K8R2-3.Hng | select -ExpandProperty networkadapters).ipaddress[0] -match $null)
{
Start-Sleep 3
Write-Host "Waiting to Acquire IP Address" -ForegroundColor green
}
$ipaddress=(Get-VM -Name dv.VMWIN2K8R2-2.Hng | select -ExpandProperty networkadapters).ipaddresses[0]
Write-Host "VM has acquired an IPAddress of $ipaddress"
I Tried the above snippet but the while loop never run's, i have noticed that while VM is shutdown the ip address parameter is blank so i thought to match it till it shows Null and when i boot up the VM and once the server gets an IP address by DHCP it should exit the while loop and print the IP address on console.
The IP is probably not null, so it's doing exactly what it should. The IP is probably 169.254.x.x, or at least that's what happens to me before I pick up DHCP. How about choosing the first octet of the address it should have after it picks up an address and using that?
EDIT: Maybe it's not 169 as I thought, after re-reading your post. My advice still applies. Try using something like this: While (!($ip -like "10.*")
If there is no network stack there are no addresses and I think you'll find the array is not there. Chris is on the right track. I think you'll need to not do what you're doing but first test to see if ipaddress is a property, is an array, and has at least 1 item in it before checking the value of that item.
Hi Guys i was able to solve the issue, i used the Get-VMNetworkAdapter Cmdlet and it solved the issue
while (((Get-VMNetworkAdapter $vmname | select -ExpandProperty ipaddresses) -eq $null -or ((Get-VMNetworkAdapter $vmname | select -ExpandProperty ipaddresses) -match "169.")))
{
Write-Progress -Activity "Waiting for VM to Aquire an IPAddress"
}