[英]Send variables from inside Invoke-Command scriptblock back to host
The below is a script that is collecting information about SQL-jobs on remote servers.下面是一个收集远程服务器上 SQL 作业信息的脚本。
However, I want to send the information inside the catch-block back to the host.但是,我想将捕获块内的信息发送回主机。 As the script is written now the script is printing to a logfile on each remote server.
由于现在编写脚本,因此脚本正在打印到每个远程服务器上的日志文件。
How can I send the information back to the host?如何将信息发送回主机?
$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
I have solved it by creating my own properties of the output-variable with New-Object.我已经通过使用 New-Object 创建我自己的输出变量属性来解决它。 There is probably a better way to do it but this was the most convinient.
可能有更好的方法来做到这一点,但这是最方便的。
The Return-method did not work for me in this particular script. Return-method 在这个特定的脚本中对我不起作用。
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.