Showing posts with label Get-Credential. Show all posts
Showing posts with label Get-Credential. Show all posts

Wednesday, June 24, 2015

Managing Windows Server 2012 R2 Using PowerShell : Part-8 : The magic of Windows PowerShell PSRemoting.

 

  1. Part 1 : Windows Server 2012 R2 Installation.
  2. Part 2 : Exploring PowerShell Default Settings.
  3. Part 3 : Getting and setting server name.
  4. Part 4 : Getting IP Address of the server.
  5. Part 5 : Setting IP Address of the server.
  6. Part 6 : Checking if Server is member of domain.
  7. Part 7 : Joining Server to the domain.

Part -8 : The magic of Windows PowerShell PSRemoting.

6-24-2015 12-42-00 PM

Welcome back,

In the pervious blog post, we added our Windows 2012 R2 server to the domain by using Windows PowerShell, I think, our most of work is done on the server, now to do everything else on server we are going to use Windows PowerShell PSRemoting to done our jobs.

What is "Windows PowerShell PSRemoting"?

In simple words, by using PowerShell PSRemoting, we can run command on our server remotely, or we can also connect to their PowerShell console locally (  kind of SSH thing Winking smile ).

Do I need to enable PSRemoting on Windows 2012 R2 server?

In the Part-2 of this series, we noticed that PSRemoting is enabled by default on Windows 2012 R2 servers.

How to use it?

It is very simple to use.

In the below screenshot, you can see, two of PowerShell consoles, one is opening on Server ( to show you it's computer name, ) and second one is opened on my laptop.Just to show you that we are going to use client PowerShell console, not the server's.

PowerShell tip :
You can use the $env:COMPUTERNAME to find the computer name.

1

Now on your client machine, create a $credential variable and store our admin account's username and password in to it by using Get-Credential cmdlet (if you remember, we done the same thing in our previous blog post) .

$credential = Get-Credential

2

Now, we need to create another variable to store the PowerShell PSRemoting session information.

In the below command, are are creating a $session variable, and then we are using New-PSSession cmdlet to initiate a session ,in -ComputerName , we have provided the name of our server , and in -Credential we are provided the name of  variable $credential in which we have stored our admin username and password credential information.

$session = New-PSSession -ComputerName 'DEL-2k12' -Credential  $credential

3

No errors! Great!

All heavy lifting is done. Now, we only need to type Enter-PSSession cmdlet , provide the name of session, which is $session  in my case and hit enter. That's all.

Enter-PSSession -Session $session

4

In the above screenshot, you can see,that we are successfully connected to our server and  we are on remote PowerShell console of our windows 2012 R2 Server, you can also see that prompt of PowerShell has also changed too and when I run the ,$env:COMPUTERNAME it give us the name of server.

If you want to exit from your remoting session, just type and run the Exit-PSSession cmdlet.

5

PowerShell Tip:
By default PowerShell use TCO Port, 5985,5986 for PSRemoting.

Basic troubleshooting.

Some tips for PSRemoting trouble shootings are :

  1. Make sure port 5985, 5986 is opened on the server or not blocked by something else.
  2. Make sure WinRm Service is running .
  3. Try to run Enable-PSRemoting -Force on server.

That's all for now and I am hoping that you have enjoyed this post.

tumblr_ma3iifkEXw1r19a45o2_250

Regards.
Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

Thursday, August 2, 2012

Powershell and Active Directory: Maintains the Leavers of your Company using powershell GUI based Application.


HI,

Like every IT Administrator we have to create user accounts when someone joined the company and also delete and reset the account when someone leaves.
From past few days i was trying to write a GUI Application based on powershell.
My Main objective was.
    • Reset Leaving Users Password.
    • Remove him from all groups.
    • Move his user account to particular OU
You can download the script form this link : http://gallery.technet.microsoft.com/scriptcenter/Manage-Leavers-accounts-in-b98d0df2

Before running this script please change this variable as per your need:  $ArchiveOu = 'OU=Archived,DC=localDC,DC=com'

Let me explain about the script.
This script is based on ActiveDirectory Module , so before running the script make sure that you have installed RSAT tools.

When you run the script first it ask for username  and password, Please provide your Domain Admin username and password.
02-08-2012 12-06-23

After you provide the credentials a GUI will open.
On the Number 1 , it is showing that if script is able to detect and imported the ActiveDirectory Module.
Our first task to to find the Users SAMAccount Name.

So provide the users First and last name (2,3) and click on Find (4)
when you click on Find .. It search for user in all AD for matching user.
in number 5 this will show the users SAMAccount Name
in Number 6 Provide a new password for user.
Now on number 7 click on Starts
When you click on start it do .
  • Reset User password
  • remove it from all Groups
  • Move it to a particular OU
02-08-2012 12-04-54

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Manage-Leavers-accounts-in-b98d0df2
Thanks for reading.
Thanks!
Aman Dhally
Buy-More-Twitter-Followers 4fb29548b6adc  linkedin
00488d3a

Friday, March 2, 2012

Remove Computer Account from All Domain controllers using PowerShell


Hi,

If you are using Active Directory environment in you infrastructure then you does the one thing for sure , remove / add computers to the domain.

Some times when you format user computer and add it again to the Domain it gives an error that "Computer Already exists in the domain". Or you can say that you have multiple remote offices and syncing / replication between those  all DC's are slow  and you need to delete that computer from All DC including the local and remote domain Controllers.

I wrote this small script which delete the provided "computer" in to all DC which is on you replicated Server List. Before running this script make sure you have "Active Directory" module installed.

This script works for me and hope this works for you too.

You can Download the script from here : http://gallery.technet.microsoft.com/scriptcenter/Remove-Computer-Account-326f1e22

#########################################################
## make sure you have Active Directory Moudle Installed ##
# Import Module
Import-Module ActiveDirectory
# Variables
       $computer = $env:Computername     # Computername which you want to use
       $localdc = "Dc-XXXX"               # Chnage with ur local DC
       $credentials = Get-Credential   # This should be Admin Credentials
# AD  
       $ADResult = (Get-ADComputer -Filter {cn -like $computer}  -Server "$localdc" -Credential $credentials  ).name -eq $computer
       $dclist = (Get-ADDomain -Server "$localdc" -Credential $credentials).ReplicaDirectoryServers   
       $arrDc = @()
       foreach ($obj in $dclist) {
       $nlist = $obj.Replace("`.XYZ.com","")  # Replace XYZ.com with your Domain Name
       $arrDc += $nlist
       }
      
# If you want to remove it from AD remove -wahtif and un-commnted -confirm:$false
      
       if ($ADResult -eq $true) {      
       Write-Host -ForegroundColor  Red "$computer exists in AD, I am going to remove it"
       foreach ( $dc in $arrdc) {
              Remove-ADComputer -Identity "$computer"  -Server $dc  -Credential $credentials  -whatif  #-confirm:$false
              write-host $([char]7)
              write-Host "$computer is deleted on $dc " -ForegroundColor Green
              }     
              }
                          
## ENd of Script##### a m a n   d h a l l y ________                
02-03-2012 15-35-04

You can Download the script from here : http://gallery.technet.microsoft.com/scriptcenter/Remove-Computer-Account-326f1e22

Thanks for Reading.
New Delhi PowerShell User Group
Aman Dhally