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 = $

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

if ($Site.enabledProtocols -eq "http") {
elseif($site.enabledProtocols -eq "https") {

#$Date = Get-Date -DisplayHint Time

$dataRow = "<tr>



$Finalreport += $datarow


$Bindingreport = "<html>
{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; }
<h2>Binding Info</h2>




#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