SOLVED

nice to have code only once in code scope

%3CLINGO-SUB%20id%3D%22lingo-sub-1084201%22%20slang%3D%22en-US%22%3Enice%20to%20have%20code%20only%20once%20in%20code%20scope%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1084201%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20does%20someone%20see%20a%20chance%20to%20simplify%20the%20following%20script%3F%3C%2FP%3E%3CP%3EI%20would%20like%20to%20place%20the%20code%20only%20once%2C%20not%20twice%20(%24code01%20%26amp%3B%20function%20code01)%20in%20code%20scope%2C%3C%2FP%3E%3CP%3Ethe%20output%20of%20course%20should%20be%20the%20same%20as%20now.%3C%2FP%3E%3CP%3ECurrently%20only%20'section%20code01'%20is%20working.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23beginning%20of%20code%20scope%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%3CP%3E%24code01%3D%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Var1%3D1%3CBR%20%2F%3Edo%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Var1.ToString(%2200%22)%3CBR%20%2F%3Estart-sleep%20-milliseconds%20300%3CBR%20%2F%3E%24Var1%2B%2B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ewhile%20(%24Var1%20-ne%2011)%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%23----------------------%3CBR%20%2F%3Efunction%20code01%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Var1%3D1%3CBR%20%2F%3Edo%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Var1.ToString(%2200%22)%3CBR%20%2F%3Estart-sleep%20-milliseconds%20300%3CBR%20%2F%3E%24Var1%2B%2B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ewhile%20(%24Var1%20-ne%2011)%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3Efunction%20code02%3CBR%20%2F%3E%7B%7D%3CBR%20%2F%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3Efunction%20code03%3CBR%20%2F%3E%7B%7D%3CBR%20%2F%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%3CP%3E%23end%20of%20code%20scope%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20Show-Menu%3CBR%20%2F%3E%7B%3CBR%20%2F%3Ecls%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3EWrite-Host%20-foregroundcolor%20Yellow%20%22%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20Code%20Menu%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%22%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3EWrite-Host%20-NoNewline%20%22%20'01'%20%22%3CBR%20%2F%3EWrite-Host%20-foregroundcolor%20Yellow%20%22%20Code%2001%20%22%3CBR%20%2F%3EWrite-Host%20-NoNewline%20%22%20'02'%20%22%3CBR%20%2F%3EWrite-Host%20-foregroundcolor%20Yellow%20%22%20Code%2002%20%22%3CBR%20%2F%3EWrite-Host%20-NoNewline%20%22%20'03'%20%22%3CBR%20%2F%3EWrite-Host%20-foregroundcolor%20Yellow%20%22%20Code%2003%20%22%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3EWrite-Host%20%22%20'Q'%20to%20quit.%22%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Edo%3CBR%20%2F%3E%7B%3CBR%20%2F%3EShow-Menu%3CBR%20%2F%3E%24input%20%3D%20Read-Host%20%22%20Please%20make%20a%20selection%22%3CBR%20%2F%3Eswitch%20(%24input)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E'01'%20%7B%3CBR%20%2F%3Ecls%3CBR%20%2F%3Ewrite-host%20%22Code%3A%20%22%20-ForegroundColor%20Black%20-BackgroundColor%20Yellow%3CBR%20%2F%3Ewrite-host%20%24code01%20-ForegroundColor%20Black%20-BackgroundColor%20White%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3Ewrite-host%20%22Result%3A%20%22%20-ForegroundColor%20Black%20-BackgroundColor%20Yellow%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3ECode01%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E'02'%20%7B%3CBR%20%2F%3Ecls%3CBR%20%2F%3Ewrite-host%20%22Code%3A%20%22%20-ForegroundColor%20Black%20-BackgroundColor%20Yellow%3CBR%20%2F%3Ewrite-host%20here%20code02%20-ForegroundColor%20Black%20-BackgroundColor%20White%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3Ewrite-host%20%22Result%3A%20%22%20-ForegroundColor%20Black%20-BackgroundColor%20Yellow%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3ECode02%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E'03'%20%7B%3CBR%20%2F%3Ecls%3CBR%20%2F%3Ewrite-host%20%22Code%3A%20%22%20-ForegroundColor%20Black%20-BackgroundColor%20Yellow%3CBR%20%2F%3Ewrite-host%20here%20code03%20-ForegroundColor%20Black%20-BackgroundColor%20White%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3Ewrite-host%20%22Result%3A%20%22%20-ForegroundColor%20Black%20-BackgroundColor%20Yellow%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3ECode03%3CBR%20%2F%3Ewrite-host%20%22%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E'q'%20%7B%3CBR%20%2F%3Ereturn%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Epause%3CBR%20%2F%3E%7D%3CBR%20%2F%3Euntil%20(%24input%20-eq%20'q')%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1084201%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1093766%22%20slang%3D%22en-US%22%3ERe%3A%20nice%20to%20have%20code%20only%20once%20in%20code%20scope%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1093766%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F502629%22%20target%3D%22_blank%22%3E%40hh775624%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%2C%3CBR%20%2F%3Ethere%20are%20two%20possibilities.%3C%2FP%3E%3COL%3E%3CLI%3EJust%20define%20%24code01%20and%20call%20it%20with%20Invoke-Command%20-ScriptBlock%20%24code01%3C%2FLI%3E%3CLI%3EJust%20define%20function%20code01%20and%20get%20the%20definition%20with%20(Get-Item%20function%3Acode01).Definition%3C%2FLI%3E%3C%2FOL%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1093869%22%20slang%3D%22en-US%22%3ERe%3A%20nice%20to%20have%20code%20only%20once%20in%20code%20scope%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1093869%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F194946%22%20target%3D%22_blank%22%3E%40Joachim%20Pichl%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHallo%20Joachim%2C%3C%2FP%3E%3CP%3E1.%26nbsp%3B%20%3CSPAN%3EInvoke-Command%20-ScriptBlock%20%24%3C%2FSPAN%3E.....%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20ist%20genau%20die%20L%C3%B6sung%2C%20nach%20der%20ich%20gesucht%20habe%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDanke%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi, does someone see a chance to simplify the following script?

I would like to place the code only once, not twice ($code01 & function code01) in code scope,

the output of course should be the same as now.

Currently only 'section code01' is working. 

 

#beginning of code scope

#################################################################

$code01=
{
$Var1=1
do
{
$Var1.ToString("00")
start-sleep -milliseconds 300
$Var1++
}
while ($Var1 -ne 11)
}
#----------------------
function code01
{
$Var1=1
do
{
$Var1.ToString("00")
start-sleep -milliseconds 300
$Var1++
}
while ($Var1 -ne 11)
}
#################################################################
function code02
{}
#################################################################
function code03
{}
#################################################################

#end of code scope

 

function Show-Menu
{
cls
Write-Host ""
Write-Host ""
Write-Host -foregroundcolor Yellow " ============= Code Menu ================"
Write-Host ""
Write-Host ""
Write-Host -NoNewline " '01' "
Write-Host -foregroundcolor Yellow " Code 01 "
Write-Host -NoNewline " '02' "
Write-Host -foregroundcolor Yellow " Code 02 "
Write-Host -NoNewline " '03' "
Write-Host -foregroundcolor Yellow " Code 03 "
Write-Host ""
Write-Host " 'Q' to quit."
Write-Host ""
}

do
{
Show-Menu
$input = Read-Host " Please make a selection"
switch ($input)
{
'01' {
cls
write-host "Code: " -ForegroundColor Black -BackgroundColor Yellow
write-host $code01 -ForegroundColor Black -BackgroundColor White
write-host ""
write-host "Result: " -ForegroundColor Black -BackgroundColor Yellow
write-host ""
Code01
write-host ""
}
'02' {
cls
write-host "Code: " -ForegroundColor Black -BackgroundColor Yellow
write-host here code02 -ForegroundColor Black -BackgroundColor White
write-host ""
write-host "Result: " -ForegroundColor Black -BackgroundColor Yellow
write-host ""
Code02
write-host ""
}
'03' {
cls
write-host "Code: " -ForegroundColor Black -BackgroundColor Yellow
write-host here code03 -ForegroundColor Black -BackgroundColor White
write-host ""
write-host "Result: " -ForegroundColor Black -BackgroundColor Yellow
write-host ""
Code03
write-host ""
}
'q' {
return
}
}
pause
}
until ($input -eq 'q')

2 Replies
Highlighted

@hh775624 

Hello,
there are two possibilities.

  1. Just define $code01 and call it with Invoke-Command -ScriptBlock $code01
  2. Just define function code01 and get the definition with (Get-Item function:code01).Definition
Highlighted
Best Response confirmed by hh775624 (New Contributor)
Solution

@Joachim Pichl 

Hallo Joachim,

1.  Invoke-Command -ScriptBlock $.....              ist genau die Lösung, nach der ich gesucht habe :)

 

Danke