Wednesday, October 12, 2016

Misc PowerShell One-liners used in Day to Day operations

Here are some PowerShell commands we can to use to perform our day to day operations as Windows System Administrators:

  • Disable Server Manager from running at logon** :
Invoke-Expression 'REG ADD HKLM\SOFTWARE\MICROSOFT\SERVERMANAGER\ /T REG_DWORD /V DoNotOpenServerManagerAtLogon /D 1 /f' 
  • Enable Server Manager from running at logon**:
Invoke-Expression 'REG ADD HKLM\SOFTWARE\MICROSOFT\SERVERMANAGER\ /T REG_DWORD /V DoNotOpenServerManagerAtLogon /D 0 /f' 
  • Read text from file and perform any operation which is inside the {}:
Get-Content C:\Temp\servers.txt |foreach {Write-Host $_ -ForegroundColor RED} 
  • Invoke a command remotely on any remote server {net user is the command below} :
Invoke-Command -ComputerName Server1 {net user}

  • List out users who have ever logged on to a remote server ** :

Get-WmiObject -ComputerName Server1 -ClassName Win32_NetworkLoginProfile| select Name, FullName, LastLogon
  •  List out users who have ever logged on to a server **:

Get-WmiObject -ClassName Win32_NetworkLoginProfile| select Name, FullName, LastLogon

  •  Loop something to run until "n" number of times after every "nn" seconds :

1..9|foreach {Get-Date -format G ; Get-Hotfix |Group Installedon; Start-Sleep 60; Write-Host " `n"}




**The command needs to be run from an elevated PowerShell console (Start PowerShell as Admin).
 --------Stay tuned for more commands:

Thursday, September 8, 2016

Misc PowerCLI One-liners used in Day to Day operations

Here are some OneLiner commands which we can use to perform our day to day operations dealing with Virtual Machines hosted on VMWare platform.

Please note that the commands mentioned below takes input from a text file called VMList.txt which should contain list of VMs and based on the requirement it writes the output to a .CSV file. If you want the output to be displayed on screen/console then just remove "| Export-Csv -Notypeinformation OutputFileName.csv" from the very end of every command below:


#Get list of VMs and the ESX cluster names on which the VMs are hosted:
Get-Content VMList.txt | foreach {$Vmname = $_; get-vm $Vmname | get-cluster |select @{n='VMName';e={"$VMName"}},name} | Export-Csv -Notypeinformation VM-Clusters.csv

#Get list of VMs and attached Harddisks including RDM and Virtual Disks on VMs:

Get-Content VMList.txt | foreach {$VMName = $_; get-vm $VMName |Get-HardDisk |select @{n='VMName';e={"$VMName"}}, DiskType, CapacityGB, Name} | Export-Csv -Notypeinformation VM-DiskInforamtion.csv

#Get list of VMs, ClusterNames and attached Harddisks including Disk Type, CapacityGB RDM etc on VMs:
Get-Content VMList.txt | foreach {$VMName = $_; $Cluster= (get-vm $VMName |get-cluster).name; get-vm $VMName |Get-HardDisk |select @{n='VMName';e={"$VMName"}}, @{n='ClusterName';e={"$Cluster"}}, DiskType, CapacityGB, Name } | Export-Csv -Notypeinformation VM-Details.csv

#Get RDM Details from a VM along with .Naa, .VML, DiskType,Capacity etc..
get-content VMList.txt | foreach {get-vm $_ |get-harddisk | where "disktype" -Match "RAW" |select Parent, filename, Name, DeviceName, ScsiCanonicalName, DiskType, Persistence, capacityGB}| Export-Csv -Notypeinformation VM-RDMInfo.csv

#Get RDM Details from a VM along with VMname, DiskName, .Naa, DiskType.. (On Screen)
get-content VMList.txt | foreach {get-vm $_ |get-harddisk | where "disktype" -Match "RAW" |select Parent, Name, ScsiCanonicalName, DiskType} |ft -AutoSize

#Get or list Snapshots on VMs listed in Servers.txt:
gc .\Servers.txt |foreach {$VMname = $_; Get-VM $VMName |Get-Snapshot |select @{n="VM";e={$VMname}},Name,PowerState,Description}
#Take Snapshots of VMs listed in a tile named Servers.txt :
gc .\Servers.txt |foreach {$VMname = $_; Get-VM $VMName |New-Snapshot -Memory -Quiesce -Name "Initial" -Verbose}






.................Stay Tuned on this page for more useful commands.


Wednesday, March 30, 2016

Get Saved Wi-Fi Password from OS

After Microsoft has launched Windows 8 and above Desktop Operating Systems, It has not been that easier to retrieve saved Wi-Fi password from the system using GUI. 
Thanks to command line utility that it is still possible to retrieve it with minimal efforts and without using Graphical User Interface.
So let’s get started:
  • Open a PowerShell Window or a Command Prompt Window.
  • Enter following command and hit enter to list all saved SSIDs (Wi-Fi Profiles)
 
           netsh wlan show profiles

 
  • Lets select one of the SSID for which we want to retrieve the Password.
  • Enter following command and hit enter to see the detail about our selected SSID.
 
netsh wlan show profiles w32-Redlof

Note : The SSID/Profile name can also be surrounded in double quotes
  • We see that we can see almost every useful detail about our selected SSID but the password is still not visible.
  • Enter following command and hit enter to reveal password from our selected SSID.
 
netsh wlan show profiles w32-Redlof key=clear


In the above Screenshot we see that the password is being shown in clear text which can be reused as necessary and as and when required.

This has been tested  and found working on following OSes:
Microsoft Windows 7
Microsoft Windows 8
Microsoft Windows 8.1
Microsoft Windows 10

Disclaimer: Please use this method only for constructive purposes. The purpose of this blog is just to let you know the way to retrieve information from within the system and should not be used for any kind of exploitation, illegal or unwelcomed activities.


Tuesday, February 2, 2016

Get Version of installed PowerShell

Open Powershell and type

$PSVersionTable

The Results will be something like :

The value under  PSVersion is the the Version of Powershell installed on the system.