Thursday, November 24, 2011

Finding Network Adapter Data Based On Connection Name

 

PowerTip of the Day, from PowerShell.com

Sometimes it would be nice to be able to access network adapter configuration based on the name of that adapter as it appears in your Network and Sharing Center. To find the network configuration data for any network card with a "LAN" in its name, use this code:

Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionID like "%LAN%"' |   ForEach-Object { $_.GetRelated('Win32_NetworkAdapterConfiguration') }

If you want to further narrow this down and cover only NICs that are currently connected to the network, extend the WMI filter:

Get-WmiObject Win32_NetworkAdapter -Filter '(NetConnectionID like "%LAN%") and (NetConnectionStatus=2)' |   ForEach-Object { $_.GetRelated('Win32_NetworkAdapterConfiguration') }


Tuesday, November 22, 2011

How to find “Stopped” windows Services, whose start up mode is set to “Automatic” using PowerShell.


Hi,
I called these services as “Problematic services”. The start-up mode of these services are “Automatically” so that whenever windows starts or reboot these services should start automatically.
So logically if any of windows services start mode is set to “Automatic” then the Window Service should be running not “stopped”
we are using WMI query language to find these “Problematic Services”
Cmdlet use: Get-WmiObject, Where-Object , Select-Object, Format-List
we are using Win32_Service WMI call to retrieve the list  and status of the services.
   1: Get-WmiObject win32_Service

above command will show the list of all windows services.

Service_1

Let’s check the all properties of a single “Service”

Get-WmiObject win32_Service  | select -First 1 | fl *

we are piping the result of “Get-WmiObject win32_service” to select-Object cmdlet and choosing -first 1 single Service and then piping further to “Format-List” cmdlet and using * wildcard to show everything.

In below properties we are interested in Name,Status, StartMode, and State property of services.

Service_2
Get-WmiObject win32_Service  | where {$_.StartMode -eq "Auto" -and $_.State -eq "stopped"}


we are piping the previous command to Where-Object cmdlet and filtering to show  only those services whose “StartUp” mode is “Automatic” and “Status” is “Stopped”

oh!!! we have three problematic services. Okkk!!!

Service_3

Let’s filter a little bit more
Get-WmiObject win32_Service  | where {$_.StartMode -eq "Auto" -and $_.State -eq "stopped"} |  Select Name,StartMode,State


now we are piping all the previous command to Select-Object cmdlet and choose to show only Name, StartMode, and State property.

Now it seems little bit formatted.

Now you can start these service manually .

Service_4

Thanks

Aman Dhally

Outputting Text Reports without Truncating

 

PowerTip of the Day, from PowerShell.com:

If you want to capture PowerShell results in a text file, you can redirect the results or pipe them to Out-File. In any case, what you capture is the exact representation of what would have been displayed in your PowerShell console. So, depending on the amount of data, columns may be missing or truncated.
Here is a function called Out-Report. You can pipe any result to it to create a text file that does not truncate columns. In addition, with the –Open switch parameter, you can open the resulting text file, too:
   1: function Out-Report {
   2:  
   3:  param(
   4:  
   5:   $Path = "$env:temp\report.txt",
   6:  
   7:   [switch]$Open
   8:  
   9:  )
  10:  
  11:  
  12:  
  13:  $Input |
  14:  
  15:   Format-Table -AutoSize |
  16:  
  17:   Out-File $Path -Width 10000
  18:  
  19:  
  20:  
  21:   if($Open) { Invoke-Item $Path }
  22:  
  23: }

Try it:

Get-Process | Out-Report -Open

Thanks

Aman

Monday, November 21, 2011

I know its funny :)

 

total Page views are: 420  :D

 

Funny-420 visitors

Find the List of Installed software using WMI and PowerShell.

 

Hi,

how you find the which software is installed on your computer, most of users answered  in “Control Panel” , yes it is right . but, if you are creating an Inventory of which system have which software then it’s not so useful for admins.

lets use WMI to retrieve the list of Install list of Softwares:

Cmdlet used: Get-WmiObject, Select-Object, Sort-Object, Export-CSV

Command is:

   1: Get-WmiObject win32_Product  | Select Name,Version,PackageName,Installdate,Vendor | Sort InstallDate -Descending

using Get-WmiObject cmdlet we query the win32_Product class which have the information of list of all software installations. Then we piped the command to Select cmdlet and we are selecting Name, Version, PackageName and installation date of the softwares and then pipe the command to Sort-Object cmdlet and sorting the result to descending order which mean latest first.


Software-1


let export the result to CSV for future reference using Export-CSV cmdlet.



   1: Get-WmiObject win32_Product  | Select Name,Version,PackageName,Installdate,Vendor | Sort InstallDate -Descending| Export-Csv d:\report.csv

 


all command is same but we piped the command further to Export-CSV cmdlet and saving the file to D:\ with name of Report.CSV


Software-2 





let see if this created a csv file .


Software-3


yes, file is created lets open it!!!


Done !!! seems good isn’t.


Software-4


 



Thanks for viewing


Aman Dhally

Sunday, November 20, 2011

Turning SIDs into Real Names using PowerShell

 

PowerTip of the Day, from PowerShell.com:

Sometimes, you'd like to turn security identifiers (SIDs) into real names. Here is a function that can do this for you:

   1: function SID2Name($sid){
   2:  
   3:   $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
   4:  
   5:   try {
   6:  
   7:   $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
   8:  
   9:   $objUser.Value
  10:  
  11:   } catch { $sid }
  12:  
  13: }

And, here is a show case for the function: to enumerate all profiles on your computer, you can read them from the Registry. However, all profiles are stored with SIDs only. Thanks to your new function, you can now display the real user names of everyone who has a profile on your machine:



   1: function Get-Profile {
   2:  
   3: $key = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
   4:  
   5: dir $key -Name | ForEach-Object { SID2Name $_ }
   6:  
   7: }


Thanks to www.Powershell.com

Friday, November 18, 2011

Converting User Names to SIDs using PowerShell

 

PowerTip of the Day, from PowerShell.com: 

Converting User Names to SIDs

If you want to translate a valid user name to its security identifier (SID), here is a function to do that for you:

 

Converting User Names to SIDs

If you want to translate a valid user name to its security identifier (SID), here is a function to do that for you:

function Name2SID($name, $domain=$env:userdomain) {

$objUser = New-Object System.Security.Principal.NTAccount($domain,$name)

$strSID =$objUser.Translate([System.Security.Principal.SecurityIdentifier])

$strSID.Value

}

Try it with your current user name:

 

Name2SID $env:username

 

Note: Tip copied from PowerShell.com