我有一个文件,其中包含具有服务器名称及其sqlserver实例名称的记录。我想为每个记录读取配置的作业及其上次运行的状态。
我设置了凭据,然后定义脚本以收集信息,最后调用Invoke-Command。我的问题是,我希望找到收集的信息,我只是得到了属性的名称。
这是我的剧本:
$a = Get-Credential
$scriptBlock={
param($server,$instance) $OFS=","
import-module sqlps
SQLSERVER:
cd SQL\$server\$instance\JobServer\Jobs
foreach($CurJob in (ls)){
write-output "$CurJob.DisplayName`n Last Run: $CurJob.LastRunDate`n Ultima Last Run Otcome:$CurJob.LastRunOutcome`n Next Run: $CurJob.NextRunDate"
}
}
Import-Csv -Header Server, Instance -Delimiter "|" .\servers_with_jobs.txt | %{
write-output "Inovoking with server: $_.Server/$_.Instance"
$JobNumber=Invoke-Command -AsJob -ComputerName $_.Server -Credential $a -ScriptBlock $scriptBlock -ArgumentList $_.Server, $_.Instance
Wait-Job $JobNumber
$JobResult = Receive-Job $JobNumber
$JobResult
}
当我跑步时,我得到以下内容:
Inovoking with server: @{Server=server01.company.net; Instance=INST1}.Server/@{Server=server01.company.net; Instance=INST1}.Instance
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
148 Job148 RemoteJob Completed True server01.e... ...
WARNING: The names of some imported commands from the module 'sqlps' include unapproved verbs that might make them less discoverable. To find the commands with
unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
ReplicationJob1.DisplayName
Last Run: ReplicationJob1.LastRunDate
Ultima Last Run Otcome:ReplicationJob1.LastRunOutcome
Next Run: ReplicationJob1.NextRunDate
ReplicationJob2.DisplayName
Last Run: ReplicationJob2.LastRunDate
Ultima Last Run Otcome:ReplicationJob2.LastRunOutcome
Next Run: ReplicationJob2.NextRunDate
似乎脚本将作业名称解析为ToString方法。即使我尝试打印$ CurJob对象的类型,但它显示类似于运行Get-Member的输出:
PSComputerName : server01.company.net
RunspaceId : 547127ac-ae58-4144-b769-507c6cf18b2e
Module : Microsoft.SqlServer.Smo.dll
Assembly : Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
TypeHandle : System.RuntimeTypeHandle
BaseType : Microsoft.SqlServer.Management.Smo.Agent.AgentObjectBase
UnderlyingSystemType : Microsoft.SqlServer.Management.Smo.Agent.Job
FullName : Microsoft.SqlServer.Management.Smo.Agent.Job
AssemblyQualifiedName : Microsoft.SqlServer.Management.Smo.Agent.Job, Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
Namespace : Microsoft.SqlServer.Management.Smo.Agent
GUID : c556994c-df26-35b1-8b70-78bce85a3e25
答案 0 :(得分:1)
您需要在字符串评估中评估参数调用。我没有对此进行测试,但我认为这个例子可以解决这个问题。
$a = Get-Credential
$scriptBlock={
param($server,$instance) $OFS=","
import-module sqlps
SQLSERVER:
cd SQL\$server\$instance\JobServer\Jobs
foreach($CurJob in (ls)){
write-output "$($CurJob.DisplayName)`n Last Run: $($CurJob.LastRunDate)`n Ultima Last Run Otcome:$($CurJob.LastRunOutcome)`n Next Run: $($CurJob.NextRunDate)"
}
}
Import-Csv -Header Server, Instance -Delimiter "|" .\servers_with_jobs.txt | %{
write-output "Inovoking with server: $($_.Server)/$($_.Instance)"
$JobNumber=Invoke-Command -AsJob -ComputerName $_.Server -Credential $a -ScriptBlock $scriptBlock -ArgumentList $_.Server, $_.Instance
Wait-Job $JobNumber
$JobResult = Receive-Job $JobNumber
$JobResult
}
使用-f
格式化字符串,而不是使用字符串评估。
'Inovoking with server: {0}/{1}' -f $_.Server, $_.Instance