Pages

Wednesday, February 4, 2015

Get Member of Local Administrators Group on remote Windows machines and Export to CSV

Create a text file named servers.txt and put all servers in the file (one server at one line)
Copy and paste the below code (in Blue) in a text file and  save it as LocalAdmins.Ps1. Put both Servers.txt and LocalAdmins.PS1 at the same location and execute the Script from Powershell console as .\LocalAdmins.Ps1



#Read Server Names from the list Servers.txt
Get-Content "Servers.txt" |foreach-object {
$Content = $_
# Loop only executed when ping is successful
if (test-connection -computername $Content -count 1 -quiet)
{
#Fetching details from servers
([ADSI]"WinNT://$_/Administrators").Members() | Select-Object `
@{n='Computername';e={ $Content }},
@{n='User/Group Name';e={ $_.GetType().InvokeMember('Name', 'GetProperty', $Null, $_, $Null) }},
@{n='ADSPath';e={ $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) }},
@{n='Type';e={ $_.GetType().InvokeMember('class', 'GetProperty', $Null, $_, $Null) }}
}}| export-csv Local_Admins.csv –notypeinformation



Wait for the script to finis and a CSV file will be ready once it is done.

Fetch IP Address, Subnet Mask, Default Gateway, Plateform, Hardware Vendor from Multiple computers adn export in CSV

Fetch Networking Information from remote Windows Computers

Create a text file named servers.txt and put all servers in the file (one server at one line)
Copy and paste the below code (in Blue) in a text file and  save it as IP_Details.PS1 and and put both servers.txt and IP_Details.ps1 at the same location.

Execute the script from powershell console as .\IP_Details.ps1


(Get-Content .\Servers.txt)| ForEach {
$SrvName = $_

        If (Test-Connection -ComputerName $SRVName -Count 1 -Quiet) {

            $csp = Get-WmiObject Win32_ComputerSystemproduct -Computername $SrvName
            $cs = Get-WmiObject Win32_ComputerSystem -ComputerName $SRVName
              $Nic = Get-WmiObject -computername $SRVName -Class win32_networkadapterconfiguration -Filter ipenabled='true'|select DHCPEnabled,IPAddress,IPSubnet,DefaultIPGateway
            $props = @{
                ComputerName = $SRVName
                Domain = $cs.Domain
        IPAdd = $NIC.IpAddress
        Subnet = $NIC.IPSubnet
        Gateway = $Nic.DefaultiPGateway
        Platform = $Csp.Name
        Vendor = $CSp.Vendor
            }
            New-Object PsObject -Property $props
        } Else {
            Write-Warning "$SRVName cannot be reached, skipping."
        }
    } | Sort ComputerName |
            Select ComputerName,Platform,Vendor,Domain,@{N='IPAddress';E={[string]::join(".",($_.IPAdd))}},@{N='SubnetMask';E={[string]::join(".",($_.subnet))}},@{N='DefaultGateway';E={[string]::join(".",($_.Gateway))}} |
    Export-csv -notypeinformation ComputerDetails.csv


Wait for the script to finish and a csv file will be ready with all the information at the same location where script is located.

HBA Information from Remote Windows Machines

Create a text file named servers.txt and put all servers in the file (one server at one line)

Copy and paste the below code (in Blue) in a text file and  save it as HBAInfo.Ps1 and put both servers.txt and HBAInfo.ps1 at the same location.

Execute the script from powershell console as .\HBAInfo.ps1


Get-Content .\Servers.txt | ForEach-object {

$SrvName = $_

# Loop only executed when ping is successful
if (test-connection -computername $SRVName -count 1 -quiet)
{
Get-WmiObject -computername $SRVName -class MSFC_FCAdapterHBAAttributes -namespace "root\WMI" |select __SERVER,@{Name='Node WWN';Expression={(($_.NodeWWN | % {"{0:x2}" -f $_}) -join ":").ToUpper()}},Active,DriverName,HBAStatus,MAnufacturer,NumberofPorts
}Else { Write-Warning "$SRVName cannot be reached, skipping."

}} |export-Csv -NoTypeInformation hbaDetails.csv



Wait for the script to finish and a csv file will be ready with all the information at the same location where script is located.

Find RDP Port number specified on a Remote Windows machine via Powershell


# Script to know the RDP port numbers used on remote servers

clear-host
Write-host **************************************************************************
Write-host -foregroundcolor Yellow This script can be used to know the RDP port number specified on a server:
Write-host **************************************************************************
$servername = Read-host    "Enter ServerName to know its RDP port number"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ServerName)
$RegKey= $Reg.OpenSubKey("system\\currentcontrolset\\control\\terminal server\\winstations\\rdp-tcp")
$PortNumber = $RegKey.GetValue("PortNumber")
write-host
write-host ===========================================
Write-Host -ForegroundColor Green RDP Port Number for $servername is $PortNumber
write-host ===========================================


Save above code (in blue) as get-rdpport.ps1 and execute it from Powershell window.
It will prompt for Server name and once supplied it will read the registry value and return RDP port number specified in there.