Ive seen code similar to this below but, all the examples just create files.
My question is how do I return the combined $diskinfoC to a single variable?
Thanks!
$Scriptblock = {
param($Computername)
$diskinfoC = Invoke-Command -ComputerName $computername -Credential $creds -scriptblock {Get-PSDrive C} | Select-Object PSComputerName,Used,Free
return $diskinfoC
}
$MaxThreads = 5
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads)
$RunspacePool.Open()
$Jobs = @()
$alldisks=@()
$computers = “computer1”,“computer2”,“computer3”
Foreach($computer in $computers){
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
$PowerShell.AddScript($ScriptBlock).AddArgument($_)
$Jobs += $PowerShell.BeginInvoke()
}
while ($Jobs.IsCompleted -contains $false) {
Start-Sleep 1
}
If I run just the $diskinfoC = Invoke-command -computerName $computername -scriptblock{get-PSDriveC} command it works.
But, this doesnt
$Scriptblock = {
param($Computername)
$diskinfoC = Invoke-Command -ComputerName $computername -scriptblock {Get-PSDrive C} | Select-Object PSComputerName,Used,Free
write-host $diskinfoC
$file = $Computername + “_” + “DiskinfoC.csv”
$diskinfoC | Export-csv .$file -nti
}
$MaxThreads = 5
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads)
$RunspacePool.Open()
$Jobs = @()
$alldisks=@()
$computers = “computer1”,“computer2”,“Computer3”
Foreach($computername in $computers){
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
$PowerShell.AddScript($ScriptBlock).AddArgument($computername)
$Jobs += $PowerShell.BeginInvoke()
}
while ($Jobs.IsCompleted -contains $false) {
Start-Sleep 1
}
I thought it would write files to the local dir but, nothing…
Any guidance appreciated!