Powershell for Windows

06.05.2009

Проверка работы службы и экспорт данных в файл *.csv

Работа скрипта:

  1. Задаем параметры
  2. Проверяем путь к папке и если нужно создаем.
  3. Циклом перебираем список указанных ИП-адресов
  4. Проверяем  “жив” ли  компьютер
  5. Проверяем есть ли доступ к компьютеру
  6. Циклом перебираем список указанных служб
  7. Проверяем на компьютере установлена служба
  8. Проверяем тип запуска и если нужно меняем на auto
  9. Проверяем работает ли служба и если нужно запускаем
  10. Экспорт данных в файл *.csv

Скрипт TestService.ps1


param (
[string]$Folder, # Путь к папке. Необязательный параметр.
[string[]]$IPNetwork, # ИП-адрес. Можно указать несколько значений.
[string[]]$ServiceName, # Имя сервиса. Можно указать несколько значений.
$User # Доменная учетная запись (domen\user).
#[int[]]$y, # Номер сети.
#[int[]]$x = 1..254 # Номер ип-адреса.
)

#region Parametrs
#foreach ($a in $y) {
# foreach ($b in $x) {
# $IPNetwork += $(«192.168.$a.$b»)
# }
#}

$Data = get-date -uformat «%d.%m.%Y»

if ($Folder -eq «») {
$LogPath = «C:\Temp\$Data»
}
else {
$LogPath = «$Folder\$Data»
}

if ($User -is [string]) {
$Connection = Get-Credential -Credential $User
}

$Ping = @();`
$NoAccess = @();$ServerRPCUnavailable = @();$ChangeStateOrStartMode = @();$NoService = @()
#endregion Parametrs

#region Check Path Export LOGS
If((Test-Path ($LogPath)) -eq $False) {
New-Item ($LogPath) -Type Directory
}
#endregion Check Path Export LOGS

#region We Write The Received Results in Log
$LogPing = «$LogPath\Ping.csv»
$LogNoService = «$LogPath\Service_No_Install.csv»
$LogChangeStateOrStartMode = «$LogPath\Change_State_Or_StartMode.csv»
$LogRPCUnavailable = «$LogPath\Server_RPC_Unavailable.csv»
$LogNoAccess = «$LogPath\No_Access_Computer.csv»
#endregion We Write The Received Results in Log

#region Function
function Ping ($Name){
$ping = new-object System.Net.NetworkInformation.Ping
if ($ping.send($Name).Status -eq «Success») {$True}
else {$False}
trap {Write-Verbose «Ошибка пинга»; $False; continue}
}
#endregion Function

#region Service Check
if ($Connection -eq $null){
foreach ($IP in $IPNetwork){
$Alive=»»;`
$Comp = «»;$Service = «»;$StartMode = «»;$State = «»

$Alive = Ping $IP
if ($Alive -eq «True»){
$Ping += «» | select @{e={$IP};n=’IP’}

Trap {Continue}
$Comp = gwmi -Class «Win32_computersystem» -ComputerName $IP -ErrorAction SilentlyContinue

if ($Comp -eq «»){
Write-Host «NoAccess $IP» -ForegroundColor Red ;`
$NoAccess += «» | select @{e={$IP};n=’IP’};
}

if ($Comp -eq $Null){
Write-Host «ServerRPCUnavailable $IP» -ForegroundColor Red ;`
$ServerRPCUnavailable += «» | select @{e={$IP};n=’IP’};
}

if ($Comp.Caption.Length -gt 1) {

foreach ( $SN in $ServiceName){
$Service = gwmi -Class «Win32_Service» -ComputerName $IP -Filter «name=’$($SN)'»

if ($Service -ne $Null) {

if ($Service.StartMode -ne «Auto») {
$Service.ChangeStartMode(«automatic»);`
$StartMode = ‘Change’
}

if ($Service.State -ne «Running») {
$Service.startservice();`
$State = ‘Change’
}

$ChangeStateOrStartMode += «» | select @{e={$comp.Name};n=’Computer’},`
@{e={$IP};n=’IP’},@{e={$Service.DisplayName+»(«+$Service.Name+»)»};n=’ServiceName’},@{e={$StartMode};n=’StartMode’},@{e={$State};n=’State’}
}

else {
$NoService += «» | select @{e={$comp.Name};n=’Computer’},`
@{e={$IP};n=’IP’},@{e={$ServiceName};n=’ServiceName’}
}
}
}
}
}

}
else {
foreach ($IP in $IPNetwork){
$Alive=»»;`
$Comp = «»;$Service = «»;$StartMode = «»;$State = «»

$Alive = Ping $IP
if ($Alive -eq «True»){
$Ping += «» | select @{e={$IP};n=’IP’}

Trap {Continue}
$Comp = gwmi -Class «Win32_computersystem» -ComputerName $IP -Credential $Connection -ErrorAction SilentlyContinue

if ($Comp -eq «»){
Write-Host «NoAccess $IP» -ForegroundColor Red ;`
$NoAccess += «» | select @{e={$IP};n=’IP’};
}

if ($Comp -eq $Null){
Write-Host «ServerRPCUnavailable $IP» -ForegroundColor Red ;`
$ServerRPCUnavailable += «» | select @{e={$IP};n=’IP’};
}

if ($Comp.Caption.Length -gt 1) {

foreach ( $SN in $ServiceName){
$Service = gwmi -Class «Win32_Service» -ComputerName $IP -Filter «name=’$($SN)'» -Credential $Connection

if ($Service -ne $Null) {

if ($Service.StartMode -ne «Auto») {
$Service.ChangeStartMode(«automatic»);`
$StartMode = ‘Change’
}

if ($Service.State -ne «Running») {
$Service.startservice();`
$State = ‘Change’
}

$ChangeStateOrStartMode += «» | select @{e={$comp.Name};n=’Computer’},`
@{e={$IP};n=’IP’},@{e={$Service.DisplayName+»(«+$Service.Name+»)»};n=’ServiceName’},@{e={$StartMode};n=’StartMode’},@{e={$State};n=’State’}
}

else {
$NoService += «» | select @{e={$comp.Name};n=’Computer’},`
@{e={$IP};n=’IP’},@{e={$ServiceName};n=’ServiceName’}
}
}
}
}
}
}
#endregion Service Check

#region Save to File
$Ping | export-csv -Encoding OEM -Path $LogPing -NoTypeInformation
$NoAccess | export-csv -Encoding OEM -Path $LogNoAccess -NoTypeInformation
$ServerRPCUnavailable | export-csv -Encoding OEM -Path $LogRPCUnavailable -NoTypeInformation
$ChangeStateOrStartMode | export-csv -Encoding OEM -Path $LogChangeStateOrStartMode -NoTypeInformation
$NoService | export-csv -Encoding OEM -Path $LogNoService -NoTypeInformation
#endregion Save to File

Создайте бесплатный сайт или блог на WordPress.com.