简体   繁体   中英

Issues with local variables in a script block

I have a function which pulls cluster info from a number of remote servers and logs the output in separate text file for each server. The location of the log file is store in a local variable. I've attempted to pass that variable into the scriptblock using the -ArgumentList parameter but im getting an error saying 'Could not find part of the path' - However the path in the error is the expected path:

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

}
                  }

I've resolved this issue by placing the invoke-command into $ClusterInfo, then used out-file outside of the script block: $ClusterInfo | out-File -filepath $ServerLog -Append

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM