• Hi, Joe.
    Unfortunately, I know from experience the feeling of clients with old stuff. Win7 is not even the worst offender. If I throw a stick I’d easily hit a couple of Win2003 Servers. I don’t have to maintain them (nobody is maintaining them, actually), but fortunately they’re for some other project, (physically) disconnected from mine.
    Anyway. The command should work in PS5.1 (or higher, if possible on Win7). I renamed a NIC to Local Area Network, and ran
    [powershell]Get-NetAdapter | where InterfaceAlias -Like “Local*” | Format-List [/powershell]
    and it worked fine.

    Name : Local Area Network
    InterfaceDescription : Intel(R) Ethernet Connection (4) I219-LM
    InterfaceIndex : 17

  • Hi, Joe. You could try to install PowerShell 5.1 on Windows 7. Then you should have the cmdlet available. I say “should”, because… I haven’t tested on Win7. To be honest, I try to keep away from older OSes as much as possible…

    Some info is available here (including download links):
    https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/wmf/setup/install-configure

    I hope this helps. Cheers!

  • Well, I also introduced you to filtering. And I’d definitely use Where-Object instead of more convoluted syntax and arrays for that purpose.
    Those are great tools and just as essential for other purposed, but not for this. I mentioned them here just to show there are more ways to accomplish something. And I also mentioned there are more ways, and that you should use the most suitable one for each case. 🙂

  • Good, finally! :))
    You could have also done
    Get-NetAdapter | where InterfaceAlias -like “Ethernet 2”
    or whatever the NIC Alias was.
    Or
    (Get-NetAdapter)[0].InterfaceAlias
    This one is less nice and a bit unpredictable, but it does the same thing.

    There are many ways to achieve something. In the long run, I’d suggest you pick an option that’s reasonably fast and easy to understand for a colleague that’s looking at a script (or you in 6 months, like Mr. Jeff Hicks always says).

  • Well, if you have more than one NIC, then you have an array. To be honest, I’m not very sure about what you’re doing (like what is $InterfaceIndex4Set).

    I think before you get into deeper stuff it would help you greatly to get more comfortable with the basic workings of PowerShell. Otherwise you’d try to drive a Formula 1 car after you just got your driver’s license.

    Until then, even if you get something right (or it would seem to be right), then the result of your script might not be what it seems, and it could come back to haunt you. Usually that happens right before you plan to go on vacation, or immediately after. 🙂

  • Check out the difference between
    [powershell]Get-NetAdapter | Select-Object -ExpandProperty InterfaceAlias[/powershell]
    and
    [powershell]Get-NetAdapter | Select-Object InterfaceAlias[/powershell]

    Do you catch the difference? That’s PowerShell-related, not Get-Netadapter related. 🙂

    You may also use [powershell](Get-NetAdapter).InterfaceAlias[/powershell]

    Cheers

  • Hi.
    You can do it in several ways:

    (Get-NetAdapter).InterfaceIndex
    or save everything in a variable and then use it in a similar way.
    $Nics = Get-Netadapter
    $Nics.InterfaceIndex

    or

    Get-NetAdapter | Select-Object -ExpandProperty InterfaceIndex

    These approaches are applicable to pretty much all PowerShell commands, not really to this one in particular. If you’re new with PowerShell, getting comfortable with these concepts will be of great help. There are quite a few PowerShell courses on Pluralsight that could be a boost for you at this stage (pretty much anything from Jeff Hicks would be an excellent choice, but start with the “beginner” courses). Pluralsight is not free, but it’s completely worth the investment, and it’s roughly the cost of a Netflix subscription. 🙂
    (I’m not affiliated in any way with them, but it helped me and I learned a lot).
    Alternately, there are tons of free resources on YouTube and elsewhere. The only limit is your time. And your curiosity. 🙂

  • Hi, Lee Roy (and Anthony).

    You could do something like
    $info = Invoke-Command -ComputerName Dc01,FS03 -ScriptBlock {Get-NetAdapter}.
    to store the info in the variable $info.

    However, for Get-Netadapter you don’t need Invoke-Command. Get-Netadapter can get info from remote systems .
    $info = Get-NetAdapter -CimSession Dc01,FS03

  • In this case, PowerShell Direct is your friend. The VM needs to be on, but otherwise you can run the commands from the host. If the VMs are not in the same domain as the hosts, you can specify different credentials.

    https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/powershell-direct#run-a-script-or-command-with-invoke-command

  • Hi, Lee, Roy, and Anthony. 🙂

    I’m glad you find this useful. You can run this command just as well inside VMs, there’s no actual difference. Or is it something specific to VMs that you had in mind?

  • Hi, David.
    You may quickly modify to retrieve the current Default Gateway for the NIC, and then add the switch -DefaultGateway to the command New-NetIpAddress to set the same Gateway as before
    https://docs.microsoft.com/en-us/powershell/module/nettcpip/new-netipaddress

    Cheers. Emanuel

  • My PowerShell function Compress-Vhdx allows you to compress multiple VHDX files with a single command to reclaim space from a folder (and its nested subfolders). The function uses the Optimize-VHD PowerShell cmdlet, which comes with the Hyper-V role. You no longer need to provide the full syntax separately for each VHD. When the operation is complete, you get a nice summary of the amount of space saved for each separate VHDX.

  • Hi, Mike.
    I didn’t quite forget it; it’s just a function, not a module. If you save it as a PSM1 file, then you can install/ reference it. But I wouldn’t create a dedicated module for such a small function. My vanity doesn’t go that far. 🙂

    Cheers! Emanuel

  • Hi, Jonathan.
    Well, before it’s recognised, you need to copy/paste the code from this article in your PowerShell window. To always have it available, you can save it as a script and reference it, you can copy it in your PowerShell Profile, or you can save it in a (new or existing) PowerShell module.
    I hope this helps. Emanuel

  • The Show-Progress function provides a nice, compact way to display the progress of longer-running tasks in PowerShell. You can use it as a replacement for Write-Progress. While this has the advantage of being a “native” cmdlet with a few options to customize the progress of tasks, it occupies a bit of real estate in the PowerShell window (the upper portion of the console), sometimes hiding interesting information. The Show-Progress function is only a single line of text, at the current cursor position, and does not hide any output or status messages from other commands.

  • Hi, Olivier.

    Thanks for the clarification. Don't worry, English is not my native language, either. And yes, I do like to use descriptions. I find them as important as comments in scripts. 

    For groups you can use a prefix or suffix like U-Something, Something-DL, etc.

    Fortunately, I have been blessed with working in single-domain environments. But I still favour/use group suffixes like -RO and -RW for read-only or read-write access. It just makes life a bit easier for me. 

    Cheers. Emanuel

  • Hi Oliver.

    Wow, thanks for the comments and feedback. There's a lot of it, and it's much appreciated.

    Let me try to answer to some bits:

    • Splatting is a very good suggestion, and I've started to use it more and more often. I always preach about it to others, but… I'm still guilty of not doing it myself from time to time.
    • I'm aware that servers usually have more than one NIC (usually the front-end ones), and yes, this would certainly be an area of improvement. However, I still care about DNS everywhere. If anything else, for Kerberos authentication. You can't have that using a server's IP instead of hostname/FQDN. That aside, I might change the script at some point to pick a specific NIC. And yes, having an IP address is a good requirement for creating a firewall rule, although there are many ways to select a certain NIC when you have more than one.
    • I'd say creating the Excel file is more time-consuming, saving it as CSV is not that difficult. It takes a bit longer to create lists of all the groups, but after that it requires little maintenance, though. And the traffic rules would be filled in by other than you, ideally (my whole idea is that the application owner or architect should fill it in, not the sysadmin).
    • I understand your preference for GPOs, and I agree with it. However, there are cases (few, admittedly) where the servers are not domain members, and (and this is my main objection, really) the GPOs creation is not as easy to automate. 
    • I don't understand the last bullet. If it was appreciation for using descriptions, thank you. 🙂 I prefer to use them whenever possible (AD Groups, special user accounts, computer accounts). I also use them heavily in GPOs, for trickier settings and also I put a description for the GPO itself (it's not very intuitive, but lovely to do and have). If it wasn't a compliment but a criticism/ objection, please clarify. I developed the habit to take criticism and complaints as compliments, it makes corporate life so much easier. However, sometimes it backfires. smiley

    Once again, thank you for the comments and suggestions/hints for improvement. Cheers! Emanuel

  • With my PowerShell script, you can easily import rules from an Excel sheet to the Microsoft Defender Firewall in Windows 10. The script reads the ports that need to be opened or blocked from a simple CSV file.

  • Load More
© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account