How to find ip addresses assigned to virtual machines in an esxi server? - vsphere

I tried to find ip addresses assigned to vms in an esxi server? either from terminal or vSphere Client.

If you have installed VMware Tools in the VMs, then you can use the following one-liner to get the IP Address from command line:
for i in `vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`; do vim-cmd vmsvc/get.guest $i | grep -i 'ipaddress = "'; done
For non-shell, you have tons of powercli snippets, but you still need VMware tools.

Check the last box on the following link for a couple options: https://kb.vmware.com/kb/2012964
Depending on which version of ESXi you're using, you should be able to right click within the GUI and add a column for IP address.
Note: in order for the IP addresses of guests to be presented up through the ESXi host, VMware Tools will need to be installed and running.

v = Get-View -Viewtype VirtualMachine -Property name, guest.ipaddress
$report = #()
foreach($vm in $v)
{
$list = '' | select name, ipaddress
$list.name = $vm.name
$list.ipaddress = $vm.guest.ipaddress
$report += $list
}
$report | export-csv c:\temp\listallnamesandipaddresses.csv

Related

Unable to get IPV4 address into WPF (Using Runspace)

I've been working on transitioning a tool I'm creating for work from a traditional WPF, to a WPF that utilizes runspaces. The learning curve was a bit steep, at first, but I'm starting to get the hang of it.
I'm having some trouble getting certain powershell command results to pass through to the runspace.
The most recent issue:
I have a working powershell script that will do the following
Resolve the IP of a computer name
$resolve = resolve-dnsname $computername | select IPAddress
$IP = $resolve -replace ".*-" -replace ".{1}$"
$GetIP will show me the IPV4 address in the console
Perform nbtstat on the IP and grab the first
$NBT = IF($IP -ne "") {nbtstat -a $IP}
foreach ($line in $NBT) {
if($line -match '^\s**([^<\s]+)\s*<00>\s*UNIQUE')
{
$NBTName = $Matches[1]
break
}}
Return TRUE if the name associated with the IP address does not match the $computername or FALSE if they do match.
$BadDNS = IF($computername -ne $NBTName -and $IP -notmatch ""){"TRUE"} ELSE {"FALSE"}
This works well in a standard powershell environment. However, I cannot get this to work in my WPF form with runspaces. I have gotten so far as discovering the issue I'm running into is the IPV4 address will not populate correctly. When I run this command and output the $IP variable to a textbox, I'm getting something similar to the following
fe80:xxxx:xxxx:xxxx:xxxx %19 192.168.1.1
The resulting IP will of course fail any checks. I've tried multiple methods of pulling the IPV4 address over to the WPF GUI with varying results. None of which were acceptable.
For example:
$IP = test-netconnection -computername $computername
$IP.RemoteAddress.IPAddressToString
Which, again, gives me the IPV4 address in my ISE console. However the results are unusable in the WPF, for some reason.
Does anyone have any idea of how to get this information into my GUI?
Thank you in advance!

Exchange Powershell Script consumes all system resources on local pc

I'm back!
Anyway, I'm running an exchange script to find emails that contain a specific list of keywords for a specific set of users, defined as users and terms in the script below, and it works. However after about an hour or so of running, it's consuming obnoxious amounts of memory, 12 - 14 GB. and running very slowly.
It does flux between 3 GB and 14 GB, so I don't know if this is simply normal, expected behavior or if its' something wrong with my script. I am aware that I'm using a sorta(?) depreciated commandlet in the search-mailbox function, but I'm only searching about 300 users and 21 terms, so I don't think I need to use the new-mailboxsearch commandlet.
Script for Reference
$users = Get-Content x:\xxx\xxx\users.txt
$terms = Get-Content x:\xxx\xxx\Terms.txt
ForEach ($term in $Terms) {
ForEach ($line in $users) {
$Results = Search-Mailbox -Identity $line -SearchQuery $term -TargetMailbox SearchResults2 -TargetFolder $term -LogLevel Full | Select Identity,TargetFolder,ResultItemsCount
Add-Content -Path x:\xxx\xxx\outputfile.txt -Value "$($term);$($line);$($Results.TargetFolder);$($Results.ResultItemsCount)"
}
}
Anyway, any help is, as always, greatly appreciated.
Thanks!
Does foreach-object fair better?
$terms | ForEach { $term = $_
$users | ForEach { $line = $_
..
}
}
The problem wasn’t with the script itself, it was the environment we were running it in.
For some reason running the script inside of the integrated scripting environment Powershell ISE, was causing the script to suck up crazy amounts of memory, eventually halting the system. By simply launching it outside of the ISE we were able to get the script to behave normally:
Thanks to everyone who replied!

using mac address in filepath - file not found?

$test = #(gwmi win32_networkadapterconfiguration | select macaddress )
$test | ForEach-Object {
Write-Host $_.macaddress
$mac = $_.macaddress -replace ":", ""
$mac.Trim()
If (Test-Path "x:\$Mac") { $computer = $mac }
$Logfile = "x:\$Computer\$Computer.Log"
$File = "x:\$computer\$computer.ini"
$computer
$CompName = Get-Content $File | Select-Object -index 0
}
So the above script will not find the $file even though it is present. The x:\64006A849B90\64006A849B90.ini is present but i get this
ERROR: Get-Content : Cannot find path 'X:\64006A849B90\64006A849B90.ini' because it does not exist.
Anyone know why i cant use this - i know its something to do with the $mac value and making sure its a string but i have tried $mac.ToString() [String]$mac and trimming it and it will not see the path - any ideas? thanks
The strange thing is the value is being picked up hence the mac address being in the path but it wont find the path if that makes sense.
I think you might have other issues but assuming your files are named and exist where you expect the only problem you would have to deal with is potential nulls.
Do you have any adapters that do no have MAC Addresses? I have 3 right now. Using your code it will attempt to process those. If you were not aware of those I could see that being an issue. Easy to fix will a small code update
# Get the populated macs from all network adapters
$macs = Get-WmiObject win32_networkadapterconfiguration | Select-Object -ExpandProperty macaddress
ForEach($mac in $macs){
$mac = $mac.replace(":","")
$macFile = "x:\$mac\$mac.ini"
if(Test-Path $macFile){
# The ini file exists
$computer = Get-Content $macFile | Select-Object -Index 0
} else {
# Cant find the file
}
$computer
}
This could be simplified even further but I didn't want to do too much at once.
By using Select-Object -ExpandProperty macaddress we still get nulls but they are dropped by the pipeline so $macs would only contain strings of actual MACs.
The whole $computer = $mac should have worked but it was redundant so I removed that logic from your code.

Powershell from SQL Server Agent Job doesn't recognize ConvertTo-CSV

UPDATE: Modified the script to work within the bounds of PS1 as required by SQLPS.
Changed:
IF($property.Value -match $regex){
$currentBadLine = (ConvertTo-Csv $_ -NoTypeInformation -Delimiter $delimiter);
$badLines += $currentBadLine[1,2]
};
To:
IF($property.Value -match $regex){
$badLines += $_ | Select-Object | ft -autoSize;
};
Prints a new header for each bad line, but it's not the end of the world and not worth the effort to prevent.
I have a Powershell script that pre-processes CSV files before they have a chance to screw up my data imports.
On two servers in a row now I have confirmed that the PS Major Version at least 2, and that the following code snippet runs fine in Powershell ISE. The purpose of the code is to read in each line of the CSV, and then loop through the columns looking for the regex pattern in the $regex variable. When it finds one I want it to keep track of the error before fixing it so I can write an error log later before outputting a cleaned up file ready for import.
%{
Foreach($Property in $_.PSObject.Properties){
IF($property.Value -match $regex){
$currentBadLine = (ConvertTo-Csv $_ -NoTypeInformation -Delimiter $delimiter);
$badLines += $currentBadLine[1,2]
};
$property.Value = $property.Value -replace $regex;
}
$_
}
But once I put that code into an agent job the agent complains:
'The term 'ConvertTo-Csv' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again. '
The question then is this: Is the Agents Powershell subsystem using a different version of Powershell than the rest of the system? If so, how do I find out which version the Subsystem is using and if possible upgrade it so I can fix this.
The server is running:
Windows Server 2008 R2 Enterprise
PS Major version 2, Minor 0 Build -1 revision -1
SQL Server 2008 R2 SP1 10.5.2500.0 64 bit
Thanks!
Yes, proper PowerShell support isn't really implemented until SQL Server 2012 (an even that is a bit flakey as to what cmdlets it supports)
In 2008 and R2 the agent's powershell implementation is actually a minishell, created by the now (thankfully) deprecated make-shell.exe utility, which only allows v1 cmdlets to run, and disallows the use of Add-PSSnapin so you can't add anymore cmdlets.
To get proper powershell support, you either need to shell out and call an external script, or run the job as a windows scheduled task rather than an agent job.
The following article explains a lot about why powershell support in 2008 R2 doesn't work like you think it should:
The Truth about SQLPS and PowerShell V2
One work-around: Export-CSV to a file, then Get-Content from the file.
$rows = ,(New-Object PSObject -prop #{a=7; b='stuff';});
$rows +=,(New-Object PSObject -prop #{a=77; b='more';});
#To run from SQL Job, change from this:
$csvrows = $rows | ConvertTo-CSV -NoType | % {$_.Replace('"','')};
write-output $csvrows;
#to this:
$rows | Export-CSV -NoType "C:\Temp\T.csv"
$csvrows = (Get-Content "C:\Temp\T.csv") | % {$_.Replace('"','')};
write-output $csvrows;

Request user input based on an unknown number of mac addresses in bash

I need to write a quick bash script that asks the user which mac address should be used as a variable in the rest of the script. I can get the mac addresses using the following
ip addr | grep ether | cut -d ' ' -f6
After this, I'm not sure how to make an array of the given data, since this will depend on each machine, and then ask the user which one to choose based on an integer as input.
You can use select
select mac in `ip addr | grep ether | cut -d ' ' -f6`
do
if [[ -n $mac ]]
then
# put your command here
echo $mac
break
fi
done

Resources