2016-04-03 47 views
0

我的PowerShell腳本具有以下結構PowerShell的新手打破了腳本具有多種功能

[CmdletBinding()]param() 
process{ 
# call function a 
# call function b 
} 
end{} 
Function a{ 
[CmdletBinding()]param() 
process{} 
} 
Function b{ 
[CmdletBinding()]param() 
process{} 
} 

上述結構拋出一個錯誤,告訴我,

Unexpected token 'Function' in expression or statement. 
    + CategoryInfo   : ParserError: (:) [], ParseException 
    + FullyQualifiedErrorId : UnexpectedToken 

這是我的第一個PowerShell腳本

任何幫助真誠讚賞

謝謝

+1

當你執行' Begin-Process-End'在一個函數中,你不能在外面放置代碼* *這三個塊。將你的內部函數放在'begin'塊中 –

回答

1

聲明beginProcess段裏你的職責

執行代碼的Process科內

清理的End科內

見例子:

[CmdletBinding()]param() ## BTW: You don't need this line if you are not using args 

begin{ 

"Declare Functions in begin`n" 
Function Get-Number{ 
"Number: " + (Get-Random) 
} 

Function Get-Time{ 
Get-Date 
} 

} 

process{ 

"Execute Function in process" 
Get-Number 
Get-Time 

} 
end{ 
"`nEnd of code" 
}