簡體   English   中英

腳本塊中的局部變量問題

[英]Issues with local variables in a script block

我有一個 function 從多個遠程服務器中提取集群信息,並將 output 記錄在每個服務器的單獨文本文件中。 日志文件的位置存儲在本地變量中。 我嘗試使用 -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

}
                  }

我通過將調用命令放入 $ClusterInfo 解決了這個問題,然后在腳本塊之外使用了 out-file:$ClusterInfo | out-File -filepath $ServerLog -Append

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM