-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#Requires and Advanced Function #131
Comments
I'm not one of the "contributors" to this repo, but I might be able to help you out and I may submit a PR for this in the future... |
#Requires isn't an Advanced Function directive. It's a script directive. If you've got each function in a script then the point is of course blurred but it's important to point this out. |
Normally I work with functions and I would like that specific function are executed as administrator. |
@Schwitzd The #Requires directive applies to everything in the file (ie. script) that it's in. If you want to test a function is being run from an Administrator / elevated shell then you need to write the code around that inside the function. The #Requires directive won't help you here. Something like this might help. |
@pauby thx for the link you share |
@Schwitzd If you have two functions in the same file with the script.ps1: function function1 {
#Requires -RunAsAdministrator
Write-Host "function1"
}
function function2 {
Write-Host "function2"
} Dot-sourcing: > . .\script.ps1
. : The script 'script.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.
At line:1 char:3
+ . .\script.ps1
+ ~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (script.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation Running the script: > .\script.ps1
.\script.ps1 : The script 'script.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.
At line:1 char:1
+ .\script.ps1
+ ~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (script.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation The |
Ok! This is really good to know. Normally I have one function for each file What you think about to write this on the style guide? |
For modules I would say not recommended or in functions that get dot sourced in to a psm1 for loading. For a control script one would write do not see an issue. Still feel that code inside a function to handle the check for permission would be best since it allows the function to be moved from control script to module as one refactors.
|
@darkoperator It's handy for putting at the top of the Module Script file. That and the Manifest are the two important module files so they will likely be opened and the It really depends on where your functions came from. If they are all only for a module and the module really needs them to do most of what it does, then I would suggest that a So it's really a matter of taste I would suggest. I don't think wither way is right or wrong. Just different ways of doing the same thing. |
@Schwitzd I'm not sure what you mean by that? |
@pauby |
Dear all,
can you please add a chapter for #Requires in the advanced function?
I don't really understood where place it.
Regards
Schwitzd
The text was updated successfully, but these errors were encountered: