SOLVED

nice to have code only once in code scope

Copper 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

@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
best response confirmed by hh775624 (Copper Contributor)
Solution

@Joachim Pichl 

Hallo Joachim,

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

 

Danke

 

1 best response

Accepted Solutions
best response confirmed by hh775624 (Copper Contributor)
Solution

@Joachim Pichl 

Hallo Joachim,

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

 

Danke

 

View solution in original post