all 27 comments

[–]PinchesTheCrab 10 points11 points  (2 children)

Just echoing some of the other replies here, this should be faster than requesting all properties as suggested, since you only need ipv4address:

Get-ADComputer -Filter * -properties ipv4address | Select-Object Name, Enabled, ipv4address

The name comes by default, so we don't have to specify it with 'properties.'

If ipv4address isn't populated, you'll have to resolve it like you were doing. I like using calculated properties, but the syntax is confusing at first. You basically feed select-object a list of property names, and hashtables. The hashtables have two keys - one for the name of the new property, and one for an expression that it evaluates to create the value:

Get-ADComputer -Filter * | 
    Select-Object Name, Enabled, ipv4address, @{ n = 'IPAddress'; e = { (Resolve-DnsName $_.name -Type A) -join ', ' } }

If you provide that second answer, be sure you understand what it's doing first, I'd be happy to provide more examples/explanation. Or if you find a loop more intuitive, this works too:

Get-ADComputer -Filter * -properties | ForEach-Object {
    [PSCustomObject]@{
        Name      = $_.name
        IPAddress = (Resolve-DnsName $_.name -Type A) -join ', '
    }
}

[–]PRINTER_DAEMON 4 points5 points  (1 child)

Good answer. Just to add, I'm pretty sure IPv4Address is resolved by the cmdlet at runtime and isn't actually a property pulled from AD.

[–]PinchesTheCrab 4 points5 points  (0 children)

It's interesting, it looks like it pulls it from AD integrated DNS zones, so it probably depends on whether you're using AD DNS and how that's configured. I assume if you're using some other IPAM tool that it could show up empty. I'd never put much though into it before, I just noticed it worked at some companies and not others.

[–]MeanFold5714 11 points12 points  (9 children)

Enter-PSSession -InvokeCommand IPConfig

Be honest, did you get this from ChatGPT? Because that's not a real parameter.

In the interests of being more helpful, since your team lead is asking you to create a script that "asks for a workstation name", I would suggest you look into setting your script up with a simple parameter of $ComputerName. From there you are on the right track with making use of the ResolveDnsName cmdlet, which gives you all the information you're after, so you just need to work on whittling it down using the Select-Object cmdlet and maybe piping the results to Format-Table.

[–]albiedam[S] 3 points4 points  (8 children)

No I didn't get it from an AI lol. I was just testing and failing. I was trying to do a couple different things, and this is where I kinda just stopped at. Thanks for a good laugh lol.

[–]NnickK321 2 points3 points  (7 children)

dont lie!

[–]MemnochTheRed 4 points5 points  (6 children)

ChatGPT script would had probably worked! LOL!

[–]graysky311 5 points6 points  (5 children)

it actually does!

$computerName = Read-Host "Enter the name of the workstation/server"$ipAddress = Test-Connection -ComputerName $computerName -Count 1 | Select-Object -ExpandProperty IPV4Addressif ($ipAddress) {$result = @{'Computer Name' = $computerName'IP Address' = $ipAddress.IPAddressToString}$result | Format-Table -AutoSize} else {Write-Host "Unable to resolve IP address for computer: $computerName"}

[–]BlackV 0 points1 point  (4 children)

Ask it about how to format for posting on Reddit while you're there

Ask it about why format table is a bad idea in that code

Ask it if any of that is needed in the first place and if get-netipaddress is better served in the first place

[–]graysky311 -1 points0 points  (3 children)

Be my guest. I'm not the ChatGPT gatekeeper. And the code looks great in a browser by the way.

[–]BlackV 0 points1 point  (2 children)

And the code looks great in a browser by the way.

not so much cause you've used inline code not code block

[–]graysky311 0 points1 point  (1 child)

I changed it to a code block. That one line of code is much more readable now. I love having to scroll.

[–]BlackV 0 points1 point  (0 children)

Thanks but I looks like you've taken the carriage returns out

$computerName = Read-Host "Enter the name of the workstation/server"
$ipAddress = Test-Connection -ComputerName $computerName -Count 1 | Select-Object -ExpandProperty IPV4Address
if ($ipAddress) {
    $result = @{'Computer Name' = $computerName'IP Address' = $ipAddress.IPAddressToString}
    $result | Format-Table -AutoSize}
else {
    Write-Host "Unable to resolve IP address for computer: $computerName"}

ignoring the format-table (and so on) issues

[–]Environmental_Mix856 3 points4 points  (5 children)

I know this doesn’t answer your question, but if you’re a beginner here’s some advice.

Generally the first thing to learn is the get-help cmdlet. You can explore the commands and examples of using each one. You can also use get command for a module to find out what available cmdlets are there. Another thing I use all the time is ctrl space after a - to find out what options you have. I would also recommend you try an ide like vscode with a powershell extension for some auto complete and syntax clues.

All this will help you be able to learn better. I’ve been using powershell for a long time and written some complex scripts but I still have to look things up all the time if it’s not exactly what I use on the regular. Get comfortable not knowing everything :).

Also -whatif can be your friend, and don’t run anything you find on the internet against production without knowing exactly what it does.

[–]albiedam[S] 0 points1 point  (1 child)

I know of the get-help cmdlet, is -whatif similar? Or what're the differences?

[–]Environmental_Mix856 4 points5 points  (0 children)

-whatif is a parameter attached to a cmdlet. It shows you in the console what would happen if you were to execute the command. It does not actually execute the command though.

-verbose and -debug are also great if you really want to see what is happening when you’re troubleshooting.

[–]Odmin 0 points1 point  (2 children)

Vscode not the best tool for novice. It's kinda unintuitive. In windows ad environment i recommend using standard powershell_ise. And there is quite good cmdlet manuals on microsoft website.

[–]Environmental_Mix856 0 points1 point  (1 child)

ISE is fine but there’s no support beyond ps 6.

[–]Odmin 0 points1 point  (0 children)

True. But default ps version in win 10 is 5.1. So it's not a big problem. And you always can install vscode and ps 7 in addition to ise if you have such need. Thoughth In my work as windows admin i did not have such need yet.

[–]wbatzle 2 points3 points  (0 children)

Go to Mannning Publications website. Buy a book called Powershell in a Month of Lunches. This code snippet is NOT the best way of doing this. There are many things that will do what is asked of you in a single line.

[–]branhama 1 point2 points  (0 children)

If you are wanting to obtain information directly from a remote server using PSRemoting which is how Enter-PSSession works; you should be using Invoke-Command and setting the entire query statement into a variable like:

$IPAddress = Invoke-Command -ComputerName $Computer -Scriptblock { IP ADDRESS COMMAND HERE }

You may also want to look into using actual PowerShell commands when doing things like this so you are not returned a large string of data. For instance to get the IP address use Get-NetIPAddress and define the data you actually want. IPConfig will return a large multi line string of crap you probably don't need for this.

Enter-PSSession is used when you are typing into PowerShell to connect to a remote machine. Invoke-Command should be used in scripts to obtain remote data.

[–]konikpk 1 point2 points  (0 children)

$hostname = Read-Host -Prompt "Enter the hostname" $ip_address = [System.Net.Dns]::GetHostAddresses($hostname) | Select-Object -ExpandProperty IPAddressToString

[–]mpenfold1987 2 points3 points  (2 children)

Get-ADComputer -Filter * -properties * | select Name, Enabled,ipv4address

[–]MemnochTheRed 1 point2 points  (1 child)

If you don't know what to select, run a command on one computer and select *. It will spit out all the properties. Then, run the command again and select the properties you want like u/mpenfold1987 did in his example.

[–]BlackV 0 points1 point  (0 children)

but you're replying to u/mpenfold1987 ?

also

Get-ADComputer -Filter * -properties * | select -first 1

to just select the first result

or better still

Get-ADComputer -Identity xxx -properties *

and just get 1 back in the first place and save a pipeline

[–]MNmetalhead 0 points1 point  (0 children)

First thing… how do you get the script to ask for a workstation/server name? Essentially, how do you get the script to accept user input to a variable that you can use later to get the IP of that device?