PowerCLI

 View Only
  • 1.  ps1 script read from txt

    Posted Dec 11, 2012 11:52 AM

    Hello there,

    im experiencing first steps with scripting here.. I have a txt list of hosts, from which i need to run a command for each of them, to extract mac address of each virtual machine running.  I tried using the "GET-Content" or "foreach", however, its not going anywhere. I cant find proper way of "telling" the script to read the txt file for host IP.

    Please help....

    This is where i get so far...

    #$VCServer = "x.x.x.x"
    $VCUser = "user"
    $VCPasswd = "passwd"
    $File = "C:\API\list.txt"

    Get-Content $File | foreach-Object
    #foreach ($VCServer in $(cat C:\API\list.txt))

    $VC = Connect-VIServer $File -User $VCUser -Password $VCPasswd
    $Connection = @()
    foreach($vm in get-networkadapter -vm "*"){
        $vm.Guest.Nics | %{
            $row = "" | Select macaddress, parent, parentid, uid, connectionstate
            $row.parent = $vm.parent
            $row.parentid = $vm.parentid
            $row.uid = $vm.uid
            $row.macaddress = $vm.MacAddress
            $row.connectionstate = $vm.connectionstate
            $Connection += $row
    }}
    $Connection | Export-Csv "C:\Temp\vmreport.csv" -NoTypeInformation -UseCulture



  • 2.  RE: ps1 script read from txt

    Posted Dec 11, 2012 12:32 PM

    Hi,

    foreach ( $i in Get-Content "C:\Temp\File.txt" ){ echo $i }

    works fine for me.

    Regards



  • 3.  RE: ps1 script read from txt

    Posted Dec 11, 2012 01:08 PM

    Thx for your reply.. the only problem i have is that the export file contains results from the last esx only.. i guess because all the previous ones are overwritten. How can i fix this.. so in one csv i can see data from each esx..?

    Thx



  • 4.  RE: ps1 script read from txt

    Posted Dec 11, 2012 03:08 PM

    Try this:


    $VCUser = "user"
    $VCPasswd = "passwd"
    $File = "C:\API\list.txt"


    foreach ($VCServer in Get-Content $File) {

    $VC = Connect-VIServer -Server $VCServer -User $VCUser -Password $VCPasswd
    $Connection = @()
    foreach($vm in get-networkadapter -vm "*"){
        $vm.Guest.Nics | %{
            $row = "" | Select macaddress, parent, parentid, uid, connectionstate
            $row.parent = $vm.parent
            $row.parentid = $vm.parentid
            $row.uid = $vm.uid
            $row.macaddress = $vm.MacAddress
            $row.connectionstate = $vm.connectionstate
            $Connection += $row
        }

    }

    }
    $Connection | Export-Csv "C:\Temp\vmreport.csv" -NoTypeInformation -UseCulture

    It's just a slightly modification from your first post. Works on my system.

    The list.txt contains one IP of an ESXi server per line.

    Regards



  • 5.  RE: ps1 script read from txt

    Posted Dec 12, 2012 05:58 AM

    Thx for the reply. I tried the suggested script, however, im still getting data only the last esx (last IP in the list). I also tried to use "-append" and export it into txt, since csv doesnt support -append parameter. Not sure why im getting only last esx data....its a bit mistery to me. Attached is a script i created as a act of desperation.. :smileyhappy:

    Please let me know if you have any suggestions.

    Thx



  • 6.  RE: ps1 script read from txt

    Posted Dec 14, 2012 08:22 AM

    What are you doing? No wonder you only get one result. Your Test.ps1 has the code three times in it, so it overwrites the output file every time.

    Please just use the Test2.ps1 I'm attaching, fill in your user and your password. Fill your C:\API\list.txt with your server IPs, like:

    x.x.x.1

    x.x.x.2

    x.x.x.3

    x.x.x.4

    And the foreach-loop will execute the code for every IP in the list.

    Regards



  • 7.  RE: ps1 script read from txt

    Posted Dec 13, 2012 10:03 PM

    I am going to move your post to the PowerCLI forum, that way more PowerCLI experts will get to see it