About Me

My photo
HANUMANGARH, RAJASTHAN, India

Thursday, 17 December 2020

Enable winrm and psremoting in remote computer

 Winrm.ps1

winrm enumerate winrm/config/listener

Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse #removes all listeners in place (http and https)

Enable-PSRemoting -SkipNetworkProfileCheck -Force #creates the http listener and enables winrm for remote management

$Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName $env:computername #creates the cert for https listener

New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint –Force

winrm enumerate winrm/config/listener

 

 

 

copy.ps1

#use recurse only if you want to copy entire content

# if you want to copy particular file Copy-Item "\\servername\c$\" -Destination "\\$server\c$\temp\"

$serverlist = Get-Content c:\PS\computers.txt

 

$count = 1

 

ForEach ($server in $Serverlist)

 

{

 

 

try

{

 

#Copy-Item -path "\\mspcsc12sccmw12\c$\Richika\31.28.8" -Destination "\\$server\c$\temp\" -Recurse

Copy-Item -path "\\IGLC00PF1LA6CW\c$\PS\winrm.ps1" -Destination "\\$server\c$\temp\"

 

write-host " $count copied for $server " -ForegroundColor Green

}

 

catch

 

{

 

 

write-host " $count failed for $server " -ForegroundColor Red

 

 

}

$count = $count + 1

}

 

 

 

Configure_winrm.ps1

$computers = Get-Content  c:\PS\computers.txt

 

foreach($computer in $computers)

{

write-host " working for $computer " -ForegroundColor Green

 

Invoke-Command -ComputerName $computer -ScriptBlock {

 

 

 

#Start-Process c:\temp\winrm.ps1

 

& "c:\temp\winrm.ps1"

#& "c:\temp\winrm_configure.ps1"

 

}

}

 

 

 

 

 

 

STEPS:

  1. Run copy script to push winrm.ps1 on servers.
  2. Then 

Thursday, 19 November 2020

Tuesday, 10 November 2020

Export all users with groups and nested groups

 #Get all recursive groups a user belongs.

Function Get-ADUserNestedGroups


{

    Param

    (

        [string]$DistinguishedName,

        [array]$Groups = @()

    )

 

    #Get the AD object, and get group membership.

    $ADObject = Get-ADObject -Filter "DistinguishedName -eq '$DistinguishedName'" -Properties memberOf, DistinguishedName;

    

    #If object exists.

    If($ADObject)

    {

        #Enummurate through each of the groups.

        Foreach($GroupDistinguishedName in $ADObject.memberOf)

        {

            #Get member of groups from the enummerated group.

            $CurrentGroup = Get-ADObject -Filter "DistinguishedName -eq '$GroupDistinguishedName'" -Properties memberOf, DistinguishedName;

       

            #Check if the group is already in the array.

            If(($Groups | Where-Object {$_.DistinguishedName -eq $GroupDistinguishedName}).Count -eq 0)

            {

                #Add group to array.

                $Groups +=  $CurrentGroup;

 

                #Get recursive groups.      

                $Groups = Get-ADUserNestedGroups -DistinguishedName $GroupDistinguishedName -Groups $Groups;

            }

        }

    }

 

    #Return groups.

    Return $Groups;

}

 

#The user to check.

#$User = "o981729";

 

#Get all groups.

$Groups = Get-ADUserNestedGroups -DistinguishedName (Get-ADUser -Filter * ).DistinguishedName;

 

#Output all groups.

$Groups | Select-Object Name | Sort-Object -Property Name; 


Extract Disk Info from Remote host

 # Define the remote hostname or IP address $remoteHost = Get-Content "C:\temp\RemoteServer.txt" # Define the output CSV file path...