Access Variable from -ScriptBlock

%3CLINGO-SUB%20id%3D%22lingo-sub-1612012%22%20slang%3D%22en-US%22%3EAccess%20Variable%20from%20-ScriptBlock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1612012%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20just%20trying%20to%20access%20the%20variable%20globally%20whose%20scope%20is%20limited%20to%20-ScriptBlock%20as%20below.%20Some%20how%20variable%26nbsp%3BNewReport%20is%20not%20accessible%20out%20the%20block%20to%20print%20it%20in%20my%20local.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENew-Variable%20-Name%20NewReport%20-Scope%20global%20-Force%3C%2FP%3E%3CP%3EInvoke-Command%20-ComputerName%20Server1-ScriptBlock%20%7B%3C%2FP%3E%3CP%3EImport-Module%20WebAdministration%3CBR%20%2F%3E%24Websites%20%3D%20Get-ChildItem%20IIS%3A%5CSites%3CBR%20%2F%3Eforeach%20(%24Site%20in%20%24Websites)%20%7B%3C%2FP%3E%3CP%3E%24Binding%20%3D%20%24Site.bindings%3CBR%20%2F%3E%5Bstring%5D%24BindingInfo%20%3D%20%24Binding.Collection%3CBR%20%2F%3E%5Bstring%5D%24IP%20%3D%20%24BindingInfo.SubString(%24BindingInfo.IndexOf(%22%20%22)%2C%24BindingInfo.IndexOf(%22%3A%22)-%24BindingInfo.IndexOf(%22%20%22))%3CBR%20%2F%3E%5Bstring%5D%24Port%20%3D%20%24BindingInfo.SubString(%24BindingInfo.IndexOf(%22%3A%22)%2B1%2C%24BindingInfo.LastIndexOf(%22%3A%22)-%24BindingInfo.IndexOf(%22%3A%22)-1)%3CBR%20%2F%3E%24SiteName%20%3D%20%24Site.name%3C%2FP%3E%3CP%3E%23Write-Host%20%22Binding%20info%20for%22%20%24Site.name%20%22%20-%20IP%3A%22%24IP%22%2C%20Port%3A%22%24Port%3C%2FP%3E%3CP%3Eif%20(%24Site.enabledProtocols%20-eq%20%22http%22)%20%7B%3CBR%20%2F%3E%23DO%20CHECKS%20HERE%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelseif(%24site.enabledProtocols%20-eq%20%22https%22)%20%7B%3CBR%20%2F%3E%23DO%20CHECKS%20HERE%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%23%24Date%20%3D%20Get-Date%20-DisplayHint%20Time%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%24dataRow%20%3D%20%22%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%24SiteName%3CBR%20%2F%3E%24IP%3CBR%20%2F%3E%24Port%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%22%3CBR%20%2F%3E%24Finalreport%20%2B%3D%20%24datarow%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%24Bindingreport%20%3D%20%22%3CBR%20%2F%3E%3CSTYLE%3E%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BBR%20%2F%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%7Bfont-family%3A%20Arial%3B%20font-size%3A%2013pt%3B%7D%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BBR%20%2F%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BTABLE%7Bborder%3A%201px%20solid%20black%3B%20border-collapse%3A%20collapse%3B%20font-size%3A13pt%3B%7D%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BBR%20%2F%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BTH%7Bborder%3A%201px%20solid%20black%3B%20background%3A%20%23dddddd%3B%20padding%3A%205px%3B%20color%3A%20%23000000%3B%7D%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BBR%20%2F%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BTD%7Bborder%3A%201px%20solid%20black%3B%20padding%3A%205px%3B%20%7D%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BBR%20%2F%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%3C%2FSTYLE%3E%3CBR%20%2F%3E%3C%2FP%3E%3CH2%20id%3D%22toc-hId--1272337457%22%3EBinding%20Info%3C%2FH2%3E%3CBR%20%2F%3E%3CTABLE%3E%3CBR%20%2F%3E%3CTBODY%3E%3CTR%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CTH%3ESite%3C%2FTH%3E%3CBR%20%2F%3E%3CTH%3EIP%3C%2FTH%3E%3CBR%20%2F%3E%3CTH%3EPort%3C%2FTH%3E%3C%2FP%3E%3CP%3E%3C%2FP%3E%3C%2FTR%3E%3CBR%20%2F%3E%24Finalreport%3CBR%20%2F%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%22%3CP%3E%3C%2FP%3E%3CP%3E%24Global%3ANewReport%3D%24Bindingreport%3C%2FP%3E%3CP%3E%23Write-Output%20%24NewReport%3C%2FP%3E%3CP%3EWrite-Output%20%24Global%3ANewReport%20%7C%20out-file%20%22C%3A%5Ctemp%5Creportbind.html%22%26nbsp%3B%20-----%3CSTRONG%3EWorking%20inside%20server%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3EWrite-Output%20%24Global%3ANewReport%20%7C%20out-file%20%22C%3A%5Ctemp%5Creportbind.html%22%20%3CSTRONG%3ENot%20accessible%20from%20where%20I%20am%20running%3C%2FSTRONG%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Visitor

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