AutoCreate Lab VMs Script- Help

This topic contains 4 replies, has 2 voices, and was last updated by  AbdulMatheen Labeeb 3 months, 3 weeks ago.

  • Author
    Posts
  • #73657

    AbdulMatheen Labeeb
    Participant

    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 VM.name comes through the pipe,
    How the script would identify VM.name on first loop refers to AD server and execute invoke-command relates ony
    AD server and when it comes for the second loop, VM.name 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

  • #73682

    Richard Siddaway
    Moderator

    I've set IP addresses like this

    $newmachine = 'W16ND01'
    $staticIP = '10.10.54.72'
    
    $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 "10.10.54.10"
     Set-DnsClient -InterfaceIndex $index -ConnectionSpecificSuffix "manticore.org"
     New-NetIPAddress -InterfaceIndex $index -AddressFamily IPv4 -IPAddress $ip -PrefixLength 24
    }
    Invoke-Command -Session $s -ScriptBlock $sb -ArgumentList $staticIP
    
  • #73685

    AbdulMatheen Labeeb
    Participant

    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.

  • #73712

    Richard Siddaway
    Moderator

    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

  • #73766

    AbdulMatheen Labeeb
    Participant

    ok thanks. Will try that and update you later.

You must be logged in to reply to this topic.