Getting specific network information in PowerShell - powershell

I'm not sure if this is a fault by me, or if my PowerShell isn't operating correctly.
get-netipconfiguration | where {$_.subnetmask}
I'm not really able to find any examples online that work either. I even tried using a WMI command and it still won't work. I have the latest version of PowerShell and have done this before, but my previous method is not working. Currently I'm using this for IPaddress fetching which works wonderfully
$ipaddress = (Get-NetIPConfiguration).IPv4Address.IPAddres
That works great, but I can't seem to figure out how I could use it to display the DNS and Subnet mask as well. I know this is probably a noob question, but they've changed a bit of stuff and I'm trying to find a cut and dry way of doing it without too much code. Any help is appreciated!

You could retrieve this information using WMI:
Get-WmiObject Win32_NetworkAdapterConfiguration |
Where IPEnabled |
Select IPSubnet, DNSServerSearchOrder, IpAddress

Related

Powershell Command for AD export

I have been searching for info, I am not even sure if what I am doing is feasible. All I want to do is export AD all computers. I have the command "Get-ADComputer -Filter "*" | Export-CSV -Path c:\Users\USERNAME\documents\Computers.csv" and this works fine, BUT...
I want to be able to add a pull of serial numbers 1) either through the attribute editor in AD or 2) by using Get-WmiObject Win32_BIOS | Select SerialNumber and having it the serial number displayed in the .csv file.
I have tried so many combinations of commands, but nothing works. I even tried to add in the "ForEach" option.
I have used Get-WmiObject Win32_ComputerSystem | Select Manufacturer,Model to get get the info from my own computer, but if I am able to get it exported into the .csv as well - not required, but would be a bonus for my reports.
If I can get a command that combines basic AD computer properties & at least serial number, I would greatly appreciate it. And if the command can also include what to add in the get the make/model of the computer, I would appreciate it more!
Thank you for your time and assistance!

Get-Recipient to Get-EXORecipient? Powershell and Exchange

Had a quick and hopefully easy question for you guys. I've tried googling and searching but I haven't been able to find much info.
We've been asked to try and streamline and improve some scripts we have. I was trying to convert some Get-Recipient commands to the newer and quicker Get-EXORecipient module.
I'm having some issues with the below though and would appreciate any help or advice:
As part of one of my scripts, I use the below command to find a specific DDL. This works fine
$var1 = Get-DynamicDistributionGroup -Identity "DDLName"
The issue I have is trying to get details from this DDL using the new module
Extract of old code which works but is quite time consuming depending on the size of the DDL
$var2 = Get-Recipient -RecipientPreviewFilter $var1.RecipientFilter
The new code I'm trying which spits out an error
$var2 = Get-ExoRecipient -RecipientPreviewFilter $var1.RecipientFilter
The error I get from the above is "Get-ExoRecipient : RecipientPreviewFilter is not a supported parameter"
I haven't been able to find out how to apply the DDL filter to this command, I tried putting the entire filter in as a string but that didn't work either. That wasn't an ideal solution as the filter may change on the exchange side.
Would appreciate any help on this one!
Thanks
According to the Get-EXORecipient documentation, the -RecipientPreviewFilter parameter has been reserved for 'internal Microsoft use'.
You should be able to use the -Filter parameter instead though like this:
Get-ExoRecipient -Filter "Title -eq 'Teaching Staff'"
However, the format of the filter returned by the dynamic distribution group may differ slightly and require some changes before it can be used with -Filter

Get UpTime from powershell into a usable way, but can't get it to work

I've been making this program where i need to send a command to powershell and in return it gives me the sys UpTime (minutes work better but not mandatory)
As i'm still not used to using powershell, i'm having a lot of problems in getting this intel.
This is what i tryed:
(get-date) - (gcim Win32_OperatingSystem).LastBootUpTime
Gives me the uptime, but i still have no idea how to work with that, so i still need to somehow add something like:
| Select-String -Pattern "TotalMinutes"
But then i need (somehow) to make that powershell gives me that time as return so i can work with it.
maybe to clipboard?
| clip
But if i add all those up, none will work.
Putting in the clipboard is just a way i made to get this info, others might also work.
I'm still very new to this, sorry if i hurt your intellect with stupid questions.
Thanks in advance
By subtracting two [datetime] (System.DateTime) instances, you get a [timespan] (System.TimeSpan) instance, which you can store in a variable:
$timeSpanSinceBoot = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
You can then access its properties as needed, such as .TotalMinutes:
$timeSpanSinceBoot.TotalMinutes
To examine the members of the time-span value's type, use the Get-Member cmdlet:
$timeSpanSinceBoot | Get-Member # lists properties and methods

Most optimal way of retrieving a property in Powershell

I'm just wondering, what is the optimal, fastest way of retrieving a property in powershell?
I'm using the (). right now to identify a remote computer's architecture (x86 or x64):
$arch = (Get-WmiObject –ComputerName XXXXX –Class Win32_OperatingSystem).osarchitecture
sometimes on a very very slow link, the command takes a while to resolve. For this reason, is there a faster way of retrieving a property than the (). method? I know there are different methods, for example:
... | Select-Object -expandproperty osarchitecture
Any suggestion as to which is better amongst all the possibilites? Thank you
Comment from TheIncorrigible1 chosen as answer:
No, there is not a "faster" way to retrieve a powershell object's
property. What is slow is your RPC connection to the PC. As far as the
most supported option? Select-Object -ExpandProperty since the ().
syntax doesn't work on collections before v3 – TheIncorrigible1

How to format output in Posh Server (Powershell web server)?

I am currently trying the Powershell web server PoSH (http://poshserver.net/) for some administration reports. But i don't know how to format ouput.
From the start: i start the console with the default shortcut, with admin rights. I type Import-Module PoSHServer, then Start-PoSHServer. The web server starts, then i create a simple index.ps1 file, with just one line of code in the body section: $(command).
For example, i want to use the Get-Service Mpssvc command, but what i obtain is :
System.ServiceProcess.ServiceController
I try Get-Service MpsSvc | Select Name,Status. Output:
#{Name=MpsSvc; Status=Running}
Same thing for cmdlets Get-Process, i have an output with list of processes but it appears like this: System.Diagnostics.Process (AcroRd32) ...
However, some cmlets just like the Get-Date (used in the Posh demonstration web page) works fine and have a "normal" output.
I read the documentation, but there is no example which can help me for that.
How can i write powershell code to obtain a "clean" and console-like output?
I just downloaded and installed Posh-Server yesterday after reading this post.
If you want output to look like console inside a web-page you are probably looking at this from the wrong angle, you need to think string not console. Your code is supposed to be running inside of a here-string, in the example. So I got the hint here that the standard console formatter does not apply, posh-server will use whatever it wants to to turn your returned object into a STRING!. Your code output will get turned into a string using whatever formatter it deems applies unless you explicitly return a string - which the example script does correctly do. So try this on the console
get-process "power*" | out-string -width 80
And then try it in your posh-server script.
You probably really wanted this:
Get-Service MpsSvc | Select Name,Status | out-string -width 120
Hope that helps - I think the lack of examples in this project is a good thing because this is really a very simplistic web-server; lots of conceptual thinking required before you even start :) .