【问题标题】:Issues with local variables in a script block脚本块中的局部变量问题
【发布时间】:2021-05-12 07:11:06
【问题描述】:

我有一个函数可以从多个远程服务器中提取集群信息,并将输出记录在每个服务器的单独文本文件中。 日志文件的位置存储在本地变量中。我尝试使用 -ArgumentList 参数将该变量传递到脚本块中,但我收到一条错误消息,提示“找不到路径的一部分” - 但是错误中的路径是预期的路径:

Local vairables:
$LogDir = 'C:\Temp\Physical Tranche1\'
$ServerFile = $Server +".txt"
$ServerLog = $LogDir + $ServerFile

Function ClusterData {

If (!(Get-Service -ComputerName $Server | Where {$_.DisplayName -match "Cluster*"}).Status -eq 
"Running"){

    Write-Warning 'Cluster service is not "Running"'

    }
else {
    Write-Host "Cluster services detected...." -ForegroundColor Yellow


    Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock { 

    Write-Output "Checking cluster resources:"
    Get-clusterresource | select ownernode,iscoreresource,name,resourcetype,state,characteristics | 
Out-File -filepath $args[0] -Append

    Write-Output "Checking Cluster owner nodes:"
    Get-ClusterGroup | select cluster,Iscoregroup,ownernode,state,name | sort state | Out-File - 
filepath $args[0] -Append

    write-output "Checking cluster nodes:"
    Get-ClusterNode | select cluster,name,state,nodeweight | Out-File -filepath $args[0] -Append
    
                                                                         } -ArgumentList $ServerLog

}
                  }

【问题讨论】:

  • 请将错误信息的全文添加到您的问题中。

标签: powershell variables invoke-command


【解决方案1】:

我通过将调用命令放入 $ClusterInfo 解决了这个问题,然后在脚本块之外使用了 out-file: $ClusterInfo | out-File -filepath $ServerLog -Append

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多