Access Variable from -ScriptBlock

Copper Contributor

Hi All,

 

I am just trying to access the variable globally whose scope is limited to -ScriptBlock as below. Some how variable NewReport is not accessible out the block to print it in my local.

 

New-Variable -Name NewReport -Scope global -Force

Invoke-Command -ComputerName Server1-ScriptBlock {

Import-Module WebAdministration
$Websites = Get-ChildItem IIS:\Sites
foreach ($Site in $Websites) {

$Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string]$IP = $BindingInfo.SubString($BindingInfo.IndexOf(" "),$BindingInfo.IndexOf(":")-$BindingInfo.IndexOf(" "))
[string]$Port = $BindingInfo.SubString($BindingInfo.IndexOf(":")+1,$BindingInfo.LastIndexOf(":")-$BindingInfo.IndexOf(":")-1)
$SiteName = $Site.name

#Write-Host "Binding info for" $Site.name " - IP:"$IP", Port:"$Port

if ($Site.enabledProtocols -eq "http") {
#DO CHECKS HERE
}
elseif($site.enabledProtocols -eq "https") {
#DO CHECKS HERE
}

#$Date = Get-Date -DisplayHint Time


$dataRow = "<tr>


<td>$SiteName</td>
<td>$IP</td>
<td>$Port</td>

 

</tr>
"
$Finalreport += $datarow

}

$Bindingreport = "<html>
<style>
{font-family: Arial; font-size: 13pt;}
TABLE{border: 1px solid black; border-collapse: collapse; font-size:13pt;}
TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}
TD{border: 1px solid black; padding: 5px; }
</style>
<h2>Binding Info</h2>
<table>
<tr>

<th>Site</th>
<th>IP</th>
<th>Port</th>

</tr>
$Finalreport
</table>
</html>"

$Global:NewReport=$Bindingreport

#Write-Output $NewReport

Write-Output $Global:NewReport | out-file "C:\temp\reportbind.html"  -----Working inside server

}

Write-Output $Global:NewReport | out-file "C:\temp\reportbind.html" Not accessible from where I am running 

 

0 Replies