Skip to content

Instantly share code, notes, and snippets.

@pashalvov
Created April 18, 2022 21:52
Show Gist options
  • Save pashalvov/838a1105e6a403061e0a8419b7aff5ec to your computer and use it in GitHub Desktop.
Save pashalvov/838a1105e6a403061e0a8419b7aff5ec to your computer and use it in GitHub Desktop.
Пример RSJob для тех кому лень гуглить
Clear-Host
### Host Console Settings
$ColorsTrue = @{ForegroundColor = "Black"; BackgroundColor = "Green"}
$ColorsFalse = @{ForegroundColor = "White"; BackgroundColor = "DarkRed"}
$ColorsError = @{ForegroundColor = "White"; BackgroundColor = "Red"}
$ColorsInfo = @{ForegroundColor = "DarkGray"}
$ColorsVerbose = @{ForegroundColor = "DarkYellow"}
### Paths to CMDLets
. C:\Powershell\Functions\Test-TCPing.ps1
. C:\Powershell\Functions\ConvertTo-Encoding.ps1
###
###!!! Change beafore Start !!!
$scriptName = 'Поиск_настроек_токенов' ### No spaces Just '_'. Use for paths
$Query = "скрипт для выборки машин"
$FunctionFilesToImport = 'C:\Powershell\Functions\Test-TCPing.ps1','C:\Powershell\Functions\ConvertTo-Encoding.ps1'
$ArgumentList = 'PSScriptRoot'
$Throttle = 16
### Log and temp paths create
$LogsPath = $PSScriptRoot + "\logs\" + (Get-Date -Format "yyyy-MM-dd_HH-mm-ss") + "_" + $scriptName + ".xlsx"
if (!(Test-Path -Path ($PSScriptRoot + '\logs'))) {New-Item -Path ($PSScriptRoot + '\logs') -ItemType Directory -ErrorAction SilentlyContinue | Out-Null}
#if (!(Test-Path -Path ($PSScriptRoot + '\temp'))) {New-Item -Path ($PSScriptRoot + '\temp') -ItemType Directory -ErrorAction SilentlyContinue | Out-Null}
#$TempPath = $PSScriptRoot + "\temp"
### Remove all garbage
#Remove-Item -Path ($PSScriptRoot + "\temp\*.output") -Force -ErrorAction SilentlyContinue | Out-Null
$ARM_List = Invoke-Sqlcmd -ServerInstance Сервака_с_базой_машин -Database Имя -Query $Query
$Comp_List = @()
$TEMP_List = (
'*'
)
foreach ($ARM in $ARM_List)
{
foreach ($t in $TEMP_List)
{
if ($ARM.Name -like $t)
{
$Comp_List += $ARM
}
}
}
###
###
$i = 0
$Total = $Comp_List.Count
#$Total
#Break
$Start = Get-Date
### Write info before start
Write-Host ("Задача с именем: " + $scriptName + ". Количество машин: " + $Total + ". Количество потоков: " + $Throttle) @ColorsVerbose
### Main
$jobsPool = $Comp_List | Start-RSJob -Name {$_.Name} -FunctionFilesToImport $FunctionFilesToImport -ArgumentList $ArgumentList -Throttle $Throttle -ScriptBlock {
$ComputerName = $_.Name
$RemoteComputerName = $ComputerName
$IPAddress = $_.IPAddressFomDescription
$isPing = $false
###
### Test-Network
if (Test-TCPing -IPAddress $ComputerName -Port 445)
{
$isPing = $true
$RemoteComputerName = $ComputerName
}
else
{
if (Test-TCPing -IPAddress $IPAddress -Port 445)
{
$isPing = $true
$RemoteComputerName = $IPAddress
}
}
### /END Test Network
if ($isPing)
{
$TokentType = $null
$RegData = PoshRemoteRegistry\Get-RegValueData -ComputerName $RemoteComputerName -RegistryHive LocalMachine -RegistryKeyPath SOFTWARE\Validata\VDCSP -ValueName KeyModuleNum
switch ($RegData.RegistryValueData)
{
'2' {$TokentType = 'Флешка (2)'}
'7' {$TokentType = 'Токен (7)'}
Default {$TokentType = "Unknown ($_)"}
}
}
### Results object
[pscustomobject]@{
'Имя компьютера' = $ComputerName
'Ping' = $isPing
'Тип токена' = $TokentType
}
### /END Results object
} | Wait-RSJob -ShowProgress | Receive-RSJob
#Clear-Host
Write-Host "Выводим результаты" @ColorsInfo
$jobsPool | ft -AutoSize
[timespan]$TimeFromStart = ((Get-Date) - $Start)
Write-Host ("Всего было хостов: " + $Total + ". Всего отработано: " + $jobsPool.Count + ". Время выполнения: " + $TimeFromStart) @ColorsVerbose
Write-Host "Собираем Job" @ColorsInfo
Write-Host "Выводим лог" @ColorsInfo
Export-Excel -Path $LogsPath -InputObject $jobsPool
Set-Clipboard $LogsPath
Write-Host "Путь к логу скопирован в буфер" @ColorsVerbose
Write-Host $LogsPath @ColorsInfo
Write-Host "Удаляем job" @ColorsInfo
Get-RSJob | Remove-RSJob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment