AutoCreate Lab VMs Script- Help

Hi all,
First post, so be gentle please.
I am learning Power shell. I am trying to compile a single script which will auto create VMs, configure using powershell direct. But I am currently stuck at two aspects in the script

  1. Assigning IPs and adding servers to Domain
    Could not send variables with in the invoke command and I get Variable empty error mesage.
    Need help on how variables can be send across Invoke -scripblock multiple times (as server need to restart and then execute actions)

  2. How the script would take correct variable and execute the actions on the correct server.
    For example in my csv, I have two servers (one AD and other File server) when the object comes through the pipe,
    How the script would identify on first loop refers to AD server and execute invoke-command relates ony
    AD server and when it comes for the second loop, is Fileserver and execute invoke-command relatest only
    to File server install tasks and so on…

I have included the script below

Please provide assistance to make the script work

I’ve set IP addresses like this

$newmachine = 'W16ND01'
$staticIP = ''

$cred = Get-Credential "$newmachine\Administrator"
$s = New-PSSession -VMName $newmachine -Credential $cred

$sb = {
param ($ip)
 Get-NetAdapter -Name Ethernet | Rename-NetAdapter -NewName 'LAN'
 $index = Get-NetAdapter -Name LAN | select -ExpandProperty ifIndex

 Set-DnsClientServerAddress -InterfaceIndex $index -ServerAddresses ""
 Set-DnsClient -InterfaceIndex $index -ConnectionSpecificSuffix ""
 New-NetIPAddress -InterfaceIndex $index -AddressFamily IPv4 -IPAddress $ip -PrefixLength 24
Invoke-Command -Session $s -ScriptBlock $sb -ArgumentList $staticIP

Hi Thanks for your reply. I don’t want to manually specify the IP address into a variable because If I have 5 machines in my CSV, I don’t want to be assigning those 5 IPs manually into a variable. Rather I would like to have that variable taken from IP address field from CSV.

So all you have to do is pass the appropriate field into the scriptblock.

-ArgumentList $_.IPAddress

or you set a variable from the field and use that in the argument list

ok thanks. Will try that and update you later.