วันพุธที่ 19 มิถุนายน พ.ศ. 2562

Remote edit Group policy another computer


Remote edit Group policy another computer


Start --> run --> gpedit.msc /gpcomputer: IP_Target

Powershell: Close Auto Update Local Group Policy


Powershell: Close Auto Update Local Group Policy


##Install Module for Support Policy##
Install-Module -Name PolicyFileEditor



$RegPath = 'Software\Policies\Microsoft\Windows\WindowsUpdate\AU'

$RegName = 'NoAutoUpdate'
$RegData = '1'
$RegType = 'DWord'


Set-PolicyFileEntry -Path \\IP_Target\C$\Windows\System32\GroupPolicy\Machine\Registry.pol -Key $RegPath -ValueName $RegName -Data $RegData -Type $RegType

##Show Policy on Target##
Get-PolicyFileEntry -Path \\IP_Target\c$\Windows\System32\GroupPolicy\Machine\Registry.pol -all

Error: Sysprep


Error: Sysprep



ตามภาพ เมื่อ Sysprep แล้ว รอ Process พบว่าเกิด Error ขึ้น

สามารถทำการแก้ไข ได้ดังนี้ครับ

1. Shift+F10 (เพื่อเปิด Command Prompt มาใช้งาน)
2. cd oobe
3. msoobe
4. wait repair process
5. reboot & finish

วันพฤหัสบดีที่ 13 มิถุนายน พ.ศ. 2562

Automate Configuration Switch Cisco from Powershell Scripts


Automate Get-Configuration Cisco from Powershell Scripts


###---------------------------------------###
### Author : Yingkamol Prukrattanakul-----###
###---MCP, MCSA, MCSE, MCT, MCST, SEC+----###
###---Email<yingkamol_7@hotmail.com>------###
## Blogger: https://yingkamol.blogspot.com ##
###---------------------------------------###
###//////////////..........\\\\\\\\\\\\\\\###
###/////////////////.....\\\\\\\\\\\\\\\\\###

##powershell for management SW##

param(
    [int] $port = 23,
        [string] $command1 = "terminal length 0",
        [string] $command2 = "en",
        [string] $command4 = "show run",
    [int] $commandDelay = 1000
   )
[string] $output = ""

function GetOutput
{
  ## Create a buffer to receive the response
  $buffer = new-object System.Byte[] 1024
  $encoding = new-object System.Text.AsciiEncoding

  $outputBuffer = ""
  $foundMore = $false

  ## Read all the data available from the stream, writing it to the
  ## output buffer when done.
  do
  {
    ## Allow data to buffer for a bit
    start-sleep -m 1000

    ## Read what data is available
    $foundmore = $false
    $stream.ReadTimeout = 1000

    do
    {
      try
      {
        $read = $stream.Read($buffer, 0, 1024)

        if($read -gt 0)
        {
          $foundmore = $true
          $outputBuffer += ($encoding.GetString($buffer, 0, $read))
        }
      } catch { $foundMore = $false; $read = 0 }
    } while($read -gt 0)
  } while($foundmore)

  $outputBuffer
}


##HQ##
$remoteHost = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip.txt
$username = Get-Content D:\Ying\PowerShell\mgmt_sw\user.txt
$password = Get-Content D:\Ying\PowerShell\mgmt_sw\password.txt
$password1 = Get-Content D:\Ying\PowerShell\mgmt_sw\password-en.txt

##Pak1##
$remoteHost1 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip1.txt

##Pak2##
$remoteHost2 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip2.txt

##Pak3##
$remoteHost3 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip3.txt

##Pak4##
$remoteHost4 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip4.txt

##Pak5##
$remoteHost5 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip5.txt

##Pak6##
$remoteHost6 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip6.txt

##Pak7##
$remoteHost7 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip7.txt

##Pak8##
$remoteHost8 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip8.txt

##Pak9##
$remoteHost9 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip9.txt

##Pak10##
$remoteHost10 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip10.txt

##Pak11##
$remoteHost11 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip11.txt

##Pak12##
$remoteHost12 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip12.txt


function HQ
{
            ForEach ($remoteHosts in $remoteHost)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\HQ-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\HQFailedlogin.txt"
                    }
            }
}

function PAK1
{
            ForEach ($remoteHosts in $remoteHost1)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK1-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK1Failedlogin.txt"
                    }
            }
}

function PAK2
{
            ForEach ($remoteHosts in $remoteHost2)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK2-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK2Failedlogin.txt"
                    }
            }
}

function PAK3
{
            ForEach ($remoteHosts in $remoteHost3)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK3-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK3Failedlogin.txt"
                    }
            }
}

function PAK4
{
            ForEach ($remoteHosts in $remoteHost4)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK4-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK4Failedlogin.txt"
                    }
            }
}

function PAK5
{
            ForEach ($remoteHosts in $remoteHost5)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK1-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK5Failedlogin.txt"
                    }
            }
}

function PAK6
{
            ForEach ($remoteHosts in $remoteHost6)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK6-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK6Failedlogin.txt"
                    }
            }
}

function PAK7
{
            ForEach ($remoteHosts in $remoteHost7)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK7-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK7Failedlogin.txt"
                    }
            }
}

function PAK8
{
            ForEach ($remoteHosts in $remoteHost8)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK8-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK8Failedlogin.txt"
                    }
            }
}

function PAK9
{
            ForEach ($remoteHosts in $remoteHost9)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK9-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK9Failedlogin.txt"
                    }
            }
}

function PAK10
{
            ForEach ($remoteHosts in $remoteHost10)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK10-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK10Failedlogin.txt"
                    }
            }
}

function PAK11
{
            ForEach ($remoteHosts in $remoteHost11)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK11-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK11Failedlogin.txt"
                    }
            }
}

function PAK12
{
            ForEach ($remoteHosts in $remoteHost12)
            {
                try
                {
                write-host "Connecting to $remoteHosts on port $port"     
                trap { Write-Error "Could not connect to remote computer: $_"; exit }
                $socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
                write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
                $stream = $socket.GetStream()
                $writer = new-object System.IO.StreamWriter $stream
                    ## Receive the output that has buffered so far
                            $writer.WriteLine($username)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command2)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($password1)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                            $writer.WriteLine($command4)
                        $writer.Flush()
                        Start-Sleep -m $commandDelay
                        $SCRIPT:output += GetOutput
                ## Close the streams
                $writer.Close()
                $stream.Close()
                $output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK12-$remoteHosts.txt"
                }
                catch
                    {
                    $Failedlogin = "System FailedUser:$remoteHosts"
    echo $Failedlogin > "D:\Ying\PowerShell\mgmt_sw\PAK12Failedlogin.txt"
                    }
            }
}

วันพฤหัสบดีที่ 6 มิถุนายน พ.ศ. 2562

Microsoft Office 365: Change Password From Condition


Microsoft Office 365: Change Password From Condition


##Install Module for Manage Resource in AzureAD##

Install-Module -Name AzureAD -Force

##User Credential##

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential


Connect-MsolService


##Get All Users in Domain##
##Show 
UserPrincipalName, DisplayName, WhenCreated##
##WhenCreated = 6/24/2018##
#
#Force Change Password##

Get-Msoluser -All | Select-Object UserPrincipalName, DisplayName, WhenCreated | FT -AutoSize

Get-Msoluser -All | Where-Object WhenCreated -ge 6/24/2018 | Set-MsolUserPassword -NewPassword 'Contoso$@!678' -ForceChangePassword:$TRUE


Microsoft Office 365: Add License All Domain


Microsoft Office 365: Add License All Domain


##Install Module for Manage Resource in AzureAD##

Install-Module -Name AzureAD -Force

##User Credential##

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential


Connect-MsolService


##Get All Users in Domain##
##Add License##


Get-msoluser -DomainName "test.contoso.co.th" | Set-MsolUserLicense  -AddLicenses contoso:STANDARDWOFFPACK_STUDENT

Microsoft Office 365: Set New Password All Domain


Microsoft Office 365: Set New Password All Domain


##Install Module for Manage Resource in AzureAD##

Install-Module -Name AzureAD -Force

##User Credential##

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential


Connect-MsolService


##Get All Users in Domain##
##Force Set Password##

Get-msoluser -all -DomainName "test.contoso.co.th" | Set-MsolUserPassword -NewPassword 'Contoso$@!345' -ForceChangePassword:$TRUE

วันพุธที่ 5 มิถุนายน พ.ศ. 2562

Powershell Scripts: Get DNS Record


Powershell Scripts: Get DNS Record


Import-Module dnsshell

##FQDN##
$ServerName = "adhq01.contoso.co.th"

##Domain Name##
$ZoneName = "contoso.co.th"

##Date Time##
$ondate = get-date -Format "ddMMyy"

##Get Dns Record##
##Server Name: adhq01.contoso.co.th##
##Zone Name: contoso.co.th## 
##Record Type: A Record##
##Select: String 10.xxx.xxx.xxx##

$results = Get-DnsRecord -Server $ServerName -ZoneName $ZoneName  -RecordType A | out-string -stream | Select-String "10."


##Out Put to File##
$results >"D:\DNSRecord\DNSRecords$ondate.csv"

Powershell Scripts: Scan Patch


Powershell Scripts: Scan Patch


function Hotfixreport {
$computers = Get-Content D:\Ying\Powershell\MS17010\scanlist.csv 
$MS17010= 'KB4012598', #Windows XP, Vista, Server 2003, 2008
               'KB4018466', #Server 2008
               'KB4012212', 'KB4012215', 'KB4015549', 'KB4019264', #Windows 7, Server 2008 R2
               'KB4012214', 'KB4012217', 'KB4015551', 'KB4019216', #Server 2012
               'KB4012213', 'KB4012216', 'KB4015550', 'KB4019215', #Windows 8.1, Server 2012 R2
               'KB4012606', 'KB4015221', 'KB4016637', 'KB4019474', #Windows 10
               'KB4013198', 'KB4015219', 'KB4016636', 'KB4019473', 'KB4016871', #Windows 10 1511
               'KB4013429', 'KB4015217', 'KB4015438', 'KB4016635', 'KB4019472' #Windows 10 1607, Server 2016$ErrorActionPreference = 'Stop' 
ForEach ($computer in $computers) {

  try
    {
Get-HotFix -cn $computer  | out-string -stream | select-string -pattern $MS17010

    }

catch

    {
            $NOPATCH = "System NOPATCH:$computer"
            $NOPATCH | Write-Warning
            $NOPATCH >> "$env:USERPROFILE\Desktop\NOPATCH.txt"

    }
}

}
Hotfixreport > "$env:USERPROFILE\Desktop\Hotfixreport.txt"

Powershell Scripts: Scan Port


Powershell Scripts: Scan Port


function CheckPort
   {
            # Define every server IP you need to test:
            $servers = Get-Content D:\ying\Powershell\MS17010\scanlist.csv 
            # Define the port number you need to test (eg: 3389 for RDP):
            $portToCheck = '49154'
            $ErrorActionPreference = 'Stop' 
                foreach ($server in $servers)
                    {
                        If ( Test-Connection $server -Count 1 -Quiet)
                        {   
                        try
                            {     
                            $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
                            $props = @{
                            Server = $server
                            PortOpen = 'Yes'
                                      }
                            }                             
                        catch
                            {
                            $props = @{
                            Server = $server
                            PortOpen = 'No'
                                      }
                            }
                        }
    Else
        {       
            $props = @{
            Server = $server
            PortOpen = 'Server did not respond to ping'         
                      }
        }
    New-Object PsObject -Property $props
        }
   }
CheckPort > "$env:USERPROFILE\Desktop\CheckPort.csv"