티스토리 뷰

운영체제

Windows Powershell 로 배포하기

warpmemory 2016. 11. 24. 17:42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$ScriptBlock = {
    param(
        [Parameter(Position=0, Mandatory=$false)][int]$threadNum=$(throw "threadNum param is not null"),
        [Parameter(Position=0, Mandatory=$false)][string]$targetServer=$(throw "targetServer param is not null"),
        [Parameter(Position=0, Mandatory=$false)][string]$command=$(throw "command param is not null"),
        [Parameter(Position=0, Mandatory=$false)][string]$password
    )
    try {
        $pw = convertto-securestring -AsPlainText -Force -String "$password"
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$targetServer\Administrator",$pw
        $session = new-pssession -computername "$targetServer" -credential $cred
 
        $cmd = [scriptblock]::Create($command)
        $CmdResult = Invoke-Command -Session $session -ScriptBlock $cmd
    } catch [Exception] {
        $CmdResult = $_.Exception.Message 
    }
    $RunResult = New-Object PSObject -Property @{
        Num = $threadNum
        Server = $targetServer
        Result = $CmdResult
    }
    Get-PSSession|Remove-PSSession
    Return $RunResult
}
 
function TheradRemotesCmd{
    param(
        [Parameter(Position=0, Mandatory=$false)][string]$serverFile=$(throw "serverFile param is not null"),
        [Parameter(Position=0, Mandatory=$false)][string]$command=$(throw "command param is not null"),
        [Parameter(Position=0, Mandatory=$false)][string]$password
    )
    $Throttle = 10 #threads
    $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle)
    $RunspacePool.Open()
    $Jobs = @()
    $threadNum = 1
    foreach ($targetServer in get-content $serverFile){
        $Job = [powershell]::Create().AddScript($ScriptBlock).AddParameter("threadNum", $threadNum).AddParameter("targetServer", $targetServer).AddParameter("command", $command).AddParameter("password", $password)
        $Job.RunspacePool = $RunspacePool
        $Jobs += New-Object PSObject -Property @{
            Server = $targetServer
            Pipe = $Job
            Result = $Job.BeginInvoke()
        }
        $threadNum += 1
    }
    Write-Host "Waiting.." -NoNewline
    Do {
        Write-Host "." -NoNewline
        Start-Sleep -Seconds 1
    } While ( $Jobs.Result.IsCompleted -contains $false)
    Write-Host "All jobs completed!"
 
    $Results = @()
    ForEach ($Job in $Jobs)
    {
        $Results += $Job.Pipe.EndInvoke($Job.Result)
    }
    $Results | Out-GridView
}
 
TheradRemotesCmd -serverFile server_list.txt -command "hostname"
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
페이지
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함