将 Invoke-Command 脚本块中的变量发送回主机
Send variables from inside Invoke-Command scriptblock back to host
下面是一个脚本,它正在收集有关远程服务器上 SQL-jobs 的信息。
但是,我想将 catch 块中的信息发送回主机。
现在编写脚本时,脚本正在打印到每个远程服务器上的日志文件中。
如何将信息发回主机?
$sqlServers = @("SERVER1","SERVER2")
$runningHost = "$env:computername"
$filePath = "C:\SQLJobInventory"
$desktopPath = [Environment]::GetFolderPath("Desktop")
$output = Invoke-Command -ComputerName $sqlServers -ArgumentList $filePath,$dateToday,$dateTodayFile -ScriptBlock{
param
(
$filePath,
$dateToday,
$dateTodayFile
)
$runningHostRemote = $env:computername
Try
{
Import-Module sqlserver -ErrorAction Stop
$instances = $runningHostRemote | Foreach-Object {Get-ChildItem -Path "SQLSERVER:\SQL$_"} -ErrorAction Stop
}
Catch
{
Write-Output "$dateToday [ERROR] $runningHostRemote" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
Exit
}
ForEach ($instance in $instances)
{
Try
{
$instanceName = $instance.InstanceName
Get-SqlAgentJob -ServerInstance "$runningHostRemote$instanceName" -ErrorAction Stop |
Where-Object {$_.IsEnabled -eq "True" -and $_.LastRunDate -gt [DateTime]::Today.AddDays(-2) -and $_.OwnerLoginName -match "LKL"} |
Select-Object @{Name=‘Job name‘;Expression={$_.Name}},
@{Name=‘Description‘;Expression={$_.Description}},
@{Name=‘Instance‘;Expression={$_.Parent -Replace '[][]'}},
@{Name=‘Run outcome‘;Expression={$_.LastRunOutcome}},
@{Name=‘Run date‘;Expression={$_.LastRunDate}},
@{Name=‘Run duration‘;Expression={$_.LastRunDuration}},
@{Name=‘Job creator‘;Expression={$_.OwnerLoginName}},
@{Name=‘Runs on a schedule‘;Expression={$_.HasSchedule}},
@{Name='Schedule Type';Expression={$_.JobSchedules -join ','}}
}
Catch
{
Write-Output "$dateToday [ERROR] $runningHostRemote$instanceName" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
Exit
}
}
}
$output | Select-Object -Property * -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName |
Sort-Object "Job name" |
Export-Csv $filePath\SQLJobInvent$dateTodayFile.csv -NoTypeInformation -Delimiter ";" -Encoding UTF8
Write-Output "$dateToday [INFO] $filePath\Log$dateTodayFile.txt" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
将写入输出更改为 return
Catch
{
Return "$dateToday [ERROR] $runningHostRemote$instanceName"
}
Return 将退出脚本块并将您的字符串传回输出变量。
我已经通过使用 New-Object 创建我自己的输出变量属性解决了这个问题。
可能有更好的方法,但这是最方便的。
Return-方法在这个特定脚本中对我不起作用。
$runningHost = "$env:computername"
$filePath = "C:\SQLJobInventory"
$lastResortPath = [Environment]::GetFolderPath("Desktop")
$dateToday = Get-Date -Format “yyMMdd HH:mm"
$dateTodayFile = Get-Date -Format “yyMMdd"
$output = Invoke-Command -ComputerName $sqlServers -ArgumentList $filePath,$dateToday,$dateTodayFile -ScriptBlock{
param
(
$filePath,
$dateToday,
$dateTodayFile
)
$runningHostRemote = $env:computername
Try
{
Import-Module sqlserver -ErrorAction Stop
$instances = $runningHostRemote | Foreach-Object {Get-ChildItem -Path "SQLSERVER:\SQL$_"} -ErrorAction Stop
}
Catch
{
$moduleError = @{moduleError="$dateToday [ERROR] $runningHostRemote"}
New-Object -Type PSObject -Property $moduleError
Exit
}
ForEach ($instance in $instances){
Try
{
$instanceName = $instance.InstanceName
$jobSuccess = @{jobSuccess="$dateToday [INFO]"}
New-Object -Type PSObject -Property $jobSuccess
Get-SqlAgentJob -ServerInstance "$runningHostRemote$instanceName" -ErrorAction Stop |
Where-Object {$_.IsEnabled -eq "True" -and $_.LastRunDate -gt [DateTime]::Today.AddDays(-2) -and $_.OwnerLoginName -match "LKL"} |
Select-Object @{Name=‘Job name‘;Expression={$_.Name}},
@{Name=‘Description‘;Expression={$_.Description}},
@{Name=‘Instance‘;Expression={$_.Parent -Replace '[][]'}},
@{Name=‘Run outcome‘;Expression={$_.LastRunOutcome}},
@{Name=‘Run date‘;Expression={$_.LastRunDate}},
@{Name=‘Run duration‘;Expression={$_.LastRunDuration}},
@{Name=‘Job creator‘;Expression={$_.OwnerLoginName}},
@{Name=‘Runs on a schedule‘;Expression={$_.HasSchedule}},
@{Name='Schedule Type';Expression={$_.JobSchedules -join ','}}
}
Catch
{
$jobError = @{jobError="$dateToday [ERROR] $runningHostRemote$instanceName"}
New-Object -Type PSObject -Property $jobError
Exit
}
}
}
$output | Select-Object -ExpandProperty moduleError -ErrorAction SilentlyContinue | Out-File "$filePath\Log$dateTodayFile.txt" -Append
$output | Select-Object -ExpandProperty Jobsuccess -ErrorAction SilentlyContinue | Out-File "$filePath\Log$dateTodayFile.txt" -Append
$output | Select-Object -ExpandProperty jobError -ErrorAction SilentlyContinue | Out-File "$filePath\Log$dateTodayFile.txt" -Append
$output | Select-Object -Property * -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName |
Sort-Object "Job name" |
Export-Csv $filePath\SQLJobInvent$dateTodayFile.csv -NoTypeInformation -Delimiter ";" -Encoding UTF8
Write-Output "$dateToday [INFO] $filePath\Log$dateTodayFile.txt" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
下面是一个脚本,它正在收集有关远程服务器上 SQL-jobs 的信息。
但是,我想将 catch 块中的信息发送回主机。
现在编写脚本时,脚本正在打印到每个远程服务器上的日志文件中。
如何将信息发回主机?
$sqlServers = @("SERVER1","SERVER2")
$runningHost = "$env:computername"
$filePath = "C:\SQLJobInventory"
$desktopPath = [Environment]::GetFolderPath("Desktop")
$output = Invoke-Command -ComputerName $sqlServers -ArgumentList $filePath,$dateToday,$dateTodayFile -ScriptBlock{
param
(
$filePath,
$dateToday,
$dateTodayFile
)
$runningHostRemote = $env:computername
Try
{
Import-Module sqlserver -ErrorAction Stop
$instances = $runningHostRemote | Foreach-Object {Get-ChildItem -Path "SQLSERVER:\SQL$_"} -ErrorAction Stop
}
Catch
{
Write-Output "$dateToday [ERROR] $runningHostRemote" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
Exit
}
ForEach ($instance in $instances)
{
Try
{
$instanceName = $instance.InstanceName
Get-SqlAgentJob -ServerInstance "$runningHostRemote$instanceName" -ErrorAction Stop |
Where-Object {$_.IsEnabled -eq "True" -and $_.LastRunDate -gt [DateTime]::Today.AddDays(-2) -and $_.OwnerLoginName -match "LKL"} |
Select-Object @{Name=‘Job name‘;Expression={$_.Name}},
@{Name=‘Description‘;Expression={$_.Description}},
@{Name=‘Instance‘;Expression={$_.Parent -Replace '[][]'}},
@{Name=‘Run outcome‘;Expression={$_.LastRunOutcome}},
@{Name=‘Run date‘;Expression={$_.LastRunDate}},
@{Name=‘Run duration‘;Expression={$_.LastRunDuration}},
@{Name=‘Job creator‘;Expression={$_.OwnerLoginName}},
@{Name=‘Runs on a schedule‘;Expression={$_.HasSchedule}},
@{Name='Schedule Type';Expression={$_.JobSchedules -join ','}}
}
Catch
{
Write-Output "$dateToday [ERROR] $runningHostRemote$instanceName" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
Exit
}
}
}
$output | Select-Object -Property * -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName |
Sort-Object "Job name" |
Export-Csv $filePath\SQLJobInvent$dateTodayFile.csv -NoTypeInformation -Delimiter ";" -Encoding UTF8
Write-Output "$dateToday [INFO] $filePath\Log$dateTodayFile.txt" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append
将写入输出更改为 return
Catch
{
Return "$dateToday [ERROR] $runningHostRemote$instanceName"
}
Return 将退出脚本块并将您的字符串传回输出变量。
我已经通过使用 New-Object 创建我自己的输出变量属性解决了这个问题。 可能有更好的方法,但这是最方便的。
Return-方法在这个特定脚本中对我不起作用。
$runningHost = "$env:computername"
$filePath = "C:\SQLJobInventory"
$lastResortPath = [Environment]::GetFolderPath("Desktop")
$dateToday = Get-Date -Format “yyMMdd HH:mm"
$dateTodayFile = Get-Date -Format “yyMMdd"
$output = Invoke-Command -ComputerName $sqlServers -ArgumentList $filePath,$dateToday,$dateTodayFile -ScriptBlock{
param
(
$filePath,
$dateToday,
$dateTodayFile
)
$runningHostRemote = $env:computername
Try
{
Import-Module sqlserver -ErrorAction Stop
$instances = $runningHostRemote | Foreach-Object {Get-ChildItem -Path "SQLSERVER:\SQL$_"} -ErrorAction Stop
}
Catch
{
$moduleError = @{moduleError="$dateToday [ERROR] $runningHostRemote"}
New-Object -Type PSObject -Property $moduleError
Exit
}
ForEach ($instance in $instances){
Try
{
$instanceName = $instance.InstanceName
$jobSuccess = @{jobSuccess="$dateToday [INFO]"}
New-Object -Type PSObject -Property $jobSuccess
Get-SqlAgentJob -ServerInstance "$runningHostRemote$instanceName" -ErrorAction Stop |
Where-Object {$_.IsEnabled -eq "True" -and $_.LastRunDate -gt [DateTime]::Today.AddDays(-2) -and $_.OwnerLoginName -match "LKL"} |
Select-Object @{Name=‘Job name‘;Expression={$_.Name}},
@{Name=‘Description‘;Expression={$_.Description}},
@{Name=‘Instance‘;Expression={$_.Parent -Replace '[][]'}},
@{Name=‘Run outcome‘;Expression={$_.LastRunOutcome}},
@{Name=‘Run date‘;Expression={$_.LastRunDate}},
@{Name=‘Run duration‘;Expression={$_.LastRunDuration}},
@{Name=‘Job creator‘;Expression={$_.OwnerLoginName}},
@{Name=‘Runs on a schedule‘;Expression={$_.HasSchedule}},
@{Name='Schedule Type';Expression={$_.JobSchedules -join ','}}
}
Catch
{
$jobError = @{jobError="$dateToday [ERROR] $runningHostRemote$instanceName"}
New-Object -Type PSObject -Property $jobError
Exit
}
}
}
$output | Select-Object -ExpandProperty moduleError -ErrorAction SilentlyContinue | Out-File "$filePath\Log$dateTodayFile.txt" -Append
$output | Select-Object -ExpandProperty Jobsuccess -ErrorAction SilentlyContinue | Out-File "$filePath\Log$dateTodayFile.txt" -Append
$output | Select-Object -ExpandProperty jobError -ErrorAction SilentlyContinue | Out-File "$filePath\Log$dateTodayFile.txt" -Append
$output | Select-Object -Property * -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName |
Sort-Object "Job name" |
Export-Csv $filePath\SQLJobInvent$dateTodayFile.csv -NoTypeInformation -Delimiter ";" -Encoding UTF8
Write-Output "$dateToday [INFO] $filePath\Log$dateTodayFile.txt" |
Out-File "$filePath\Log$dateTodayFile.txt" -Append